Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

OBJECT ORIENTED PROGRAMING

LAB NO:05
By Muhammad Mudasir
C++ Constructors

A constructor in C++ is a special method that is automatically called


when an object of a class is created.
To create a constructor, use the same name as the class, followed by
parentheses ():
C++ Constructors
Constructor Parameters
Constructors can also take parameters (just like regular functions),
which can be useful for setting initial values for attributes.
The following class have brand, model and year attributes, and a
constructor with different parameters. Inside the constructor we set the
attributes equal to the constructor parameters (brand=x, etc). When we
call the constructor (by creating an object of the class), we pass
parameters to the constructor, which will set the value of the
corresponding attributes to the same:
C++ Access Specifiers

• By now, you are quite familiar with the public keyword that appears in


all of our class examples:
In C++, there are three access specifiers:

•public - members are accessible from outside the class


•private - members cannot be accessed (or viewed) from outside the
class
•protected - members cannot be accessed from outside the class,
however, they can be accessed in inherited classes. You will learn more
about Inheritance later.
public and private members
• In the following example, we demonstrate the differences
between public and private members:
Note: By default, all members of a class are private if you don't specify an access specifier:
Java Constructor

• A constructor in Java is a special method that is used to


initialize objects. The constructor is called when an object of a
class is created. It can be used to set initial values for object
attributes:
Java Constructors
Constructor Parameters

Constructors can also take parameters, which is used to initialize


attributes.
The following example adds an int y parameter to the constructor.
Inside the constructor we set x to y (x=y). When we call the
constructor, we pass a parameter to the constructor (5), which will set
the value of x to 5:
Constructor Parameters
Java Modifiers

By now, you are quite familiar with the public keyword that appears in


almost all of our examples:
Java Modifiers
The public keyword is an access modifier, meaning that it is
used to set the access level for classes, attributes, methods
and constructors.
We divide modifiers into two groups:
•Access Modifiers - controls the access level
•Non-Access Modifiers - do not control access level, but
provides other functionality
Non-Access Modifiers

Final
If you don't want the ability to override existing attribute
values, declare attributes as final:
Abstract

An abstract method belongs to an abstract class, and it does not have a


body. The body is provided by the subclass:
Java Constructors and Access Modifiers
1. Create a Java class Student with private data members name and
age. Write a constructor to initialize these values. Provide public
methods to get and set these values.
2. Implement a Java class Book with private attributes for title,
author, and year. Write a constructor to initialize these attributes
and a public method to display the book's information.
C++ Constructors and Access Modifiers
1. Develop a C++ class Person with private data members name,
age, and address. Write constructors to initialize these values and
provide public methods for accessing and updating them.
2. Design a C++ class Car with private attributes for brand, model,
and year. Write constructors to initialize these attributes.
Implement a method to display the car's information

You might also like