OOP Lecture 3
OOP Lecture 3
[CS1420]
L E C T U R E 3 : I N F O R M AT I O N H I D I N G U S I N G
E N C A P S U L AT I O N
Constructors, Encapsulation, Information Hiding, Access
Specifiers, Properties (getter Setter)
• Setters: These are the methods used in OOPS feature which helps to set the value to
private attributes in a class.
void setSalary(int s) {
salary = s;
}
EXAMPLE OF C++
class Person { void setAge(int age) { cout << "Name: " <<
private: this->age = age;} person.getName() << endl;
string name; int getAge() { cout << "Age: " << person.getAge()
<< endl;
int age; return age;}};
return 0;}
public:
void setName(string name) { int main() {
this->name = name;} Person person;
string getName() { person.setName("Jane Doe");
In abstraction, problems are solved at the design or While in encapsulation, problems are solved at the
1.
interface level. implementation level.
Simplifies complexity by exposing only essential Protects data by restricting direct access and enforcing controlled
3. details while hiding internal workings. access via methods.
We can implement abstraction using abstract class Whereas encapsulation can be implemented using by access
4.
and interfaces. modifier i.e. private, protected and public.
In abstraction, implementation complexities are While in encapsulation, the data is hidden using methods of
5.
hidden using abstract classes and interfaces. getters and setters.
The objects that help to perform abstraction are Whereas the objects that result in encapsulation need not be
6.
encapsulated. abstracted.
CONSTRUCTORS
• Constructor is a member function of a class, whose name is same as the class
name.
• Constructor is a special type of member function that is used to initialize the data
members for an object of a class automatically, when an object of the same class
is created.
• Constructor is invoked at the time of object creation.
• The only restriction that applies to the constructor is that it must not have a
return type or void. It is because the constructor is automatically called by the
compiler and it is normally used to INITIALIZE VALUES.
CONSTRUCTOR
Syntax of Constructor
class CLASSNAME{
public :
CLASSNAME() // constructor definition
{
......
}
........
};
CONSTRUCTOR EXAMPLE
class Car { main() {
public:
string brand; Car carObj1("BMW", "X5", 1999);
string model; Car carObj2("Ford", "Mustang", 1969);
int year;
cout << carObj1.brand << carObj1.model <<
Car(string x, string y, int z) {
brand = x; carObj1.year << "\n";
model = y;
year = z; cout << carObj2.brand << carObj2.model <<
} carObj2.year << "\n";
}; return 0;
int }
TYPES OF CONSTRUCTORS
Default Class_name()
Parameteri
Class_name(parameters)
zed
Constructor
s
Class_name(const class_name
Copy
old_object)
creature(){
cout<<“Contructor called";
}
PARAMETERIZED CONSTRUCTORS:
◻A parameterized constructor is just one that has parameters specified in
it.
◻We can pass the arguments to constructor function when object are
created.
LECTURE 4
C O P Y CONSTRUCTOR
◻A copy constructor is a member function that initializes an object using another object of the
same class.
◻Copy constructor takes a reference to an object of the same class as an argument.
◻For example the statement:
• abc c2(c1);
• would define the object c2 and at the same time initialize it to
the value ofc1.
◻The process of initializing through a copy constructor is known as copy initialization.
EXAMPLE
class A { int main() {
public: A a1;
int x; a1.x = 10;
// Copy Constructor definition cout << "a1's x = " << a1.x << endl;
A (A& t) {
x = t.x; A a2(a1);
cout << "Calling copy constructor" << endl; cout << "a2's x = " << a2.x;
}}; return 0;}
MOVE CONSTRUCTOR
• The move constructor is a recent addition to the family of constructors in C++.
• It is like a copy constructor that constructs the object from the already existing objects., but
instead of copying the object in the new memory, it makes use of move semantics to transfer the
ownership of the already created object to the new object without creating extra copies.
• It can be seen as stealing the resources from other objects.
Student(string n) { << ", Age: " << age << endl;} student2.displayInfo();
student3.displayInfo();
name = n;
age = 0;} private: return 0;}
string name;
int age;};
CHARACTERISTICS OF CONSTRUCTORS
• The name of the constructor is the same as its class name.
• Constructors are mostly declared as public member of the class though they can be declared as
private.
• Constructors do not return values, hence they do not have a return type.
• A constructor gets called automatically when we create the object of the class.
• Multiple constructors can be declared in a single class (it is even recommended – Rule Of Three,
The Rule of Five).
• In case of multiple constructors, the one with matching function signature will be called.
EXAMPLE
class abc{ void showdata(){
cout <<a <<" " <<b <<endl;}
Private:
};
int a, b;
public: int main(){
abc(int x, int y){
abc c1(10, 20);
a =x;
abc c2(c1);
b =y;}
c1.showdata();
abc(abc &p){ c2.showdata();
a = p.a; return 0;
b =p.b;} }
CONSTRUCTOR AND ENCAPSULATION
• Constructors play a key role in enforcing encapsulation by ensuring proper initialization of private data
members.
• Constructors support encapsulation by providing Data hiding, Automatic Initialization, Preventing
Uninitialized Data, and enforce rules on object creation
class BankAccount { balance = (initialBalance >= 0) ? initialBalance : 0;}
private:
void display() {
string accountHolder;
cout << "Account Holder: " << accountHolder ;
double balance;
cout << "Balance: $" << balance << endl; }};
public:
BankAccount(string name, double initialBalance) {
accountHolder = name;
CONSTRUCTOR CALL IN INHERITANCE
• When the Object of child class created the constructor of child class either
implicitly or explicitly called the base class constructor and when the base class
constructor is called, base class members are also created and Initialized.
• This is the reason using the derived class object we can access both base class
and derived class members.
ORDER OF CONSTRUCTOR CALL FOR
INHERITANCE
• A parent class object only calls the parents class constructor
• Child class object implicitly calls parent class default constructor first before
calling its own constructor
• Compiler implicitly create default constructors only when there is no constructor
explicitly defined.
• If a single parameterized constructor is defined, it is necessary to explicitly
defined the default constructor. For the child class object to use it
EXAMPLE: ORDER OF CONSTRUCTOR CALL FOR
INHERITANCE
class Parent {
public: // main function
// base class constructor int main() {
Parent() {
cout << "Inside base class" << endl; // creating object of sub class
} Child obj;
}; Parent pobj;
This code will through error, because child class object implicitly calls the parent class default
constructor. And in this case, class A have no default constructor.
CONSTRUCTOR CALL USING INITIALIZER
LIST
• To make the child class constructor to call the parent class’s parameterized constructor, a new
constructors needs to be defined in the child class which explicitly calls the parent class’s
parameterized constructor using initializer List
• Example:
X(int w, int e) : A(w){ Colon (:) followed by the Parent class
constructor call is the initializer list.
c=e;
cout<<“Param of child Class X is ”<<c; }
• This calls the constructor of parent Class (A) with argument w, before the body of the child
constructor executes
ORDER OF CONSTRUCTOR CALL FOR
MULTIPLE INHERITANCE
• For multiple inheritance order of constructor call is, the base class’s constructors are called in the
order of inheritance and then the derived class’s constructor.
class Parent1 { public:
public: Child() {
Parent1() cout << "Inside child class" << endl; } };
{
cout << "Inside first base class" << endl; }}; int main() {
Child obj1;
class Parent2 { return 0;
public: }
Parent2() {
cout << "Inside second base class" << endl; } }; Output:
Inside first base class
Inside second base class
Inside child class
class Child : public Parent1, public Parent2 {
DEFAULT ARGUMENTS
Default argument is an argument to a function that a programmer is not required
to specify.
C++ allow the programmer to specify default arguments that always have a
int c=12);
DEFAULT ARGUMENTS
◻The programmer may call this function in two ways:
◻In the first case the value for the argument called c is specified as normal. In
the second one, the argument is omitted, and the default value of 12will be
used instead.
◻If an object is copied from another object then the copy constructor is called.
DESTRUCTORS
◻Destructors are special member functions.
~classname();
Some Important Points About
Destructors:
◻Take the same name as class name.