Oop 4
Oop 4
Bipasha Majumder
Lecturer, Dept of CSE
Dhaka International University
1
Constructor
Constructor in C++ is a special method that is invoked
automatically at the time of object creation.
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. It
constructs the values i.e. provides data for the object that is
why it is known as constructor.
Constructor do not return value, hence they do not have a
return type.
2
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
public:
int id; // Member of Class
double gpa; // Member of class
void display() // Function of the class
{
cout<<id<<" "<<gpa<<endl;
}
student(int x,double y) // Constructor_Special Function :
{ // To set value into variable id and GPA
id=x;
gpa=y;
}
}; 3
int main()
{
student Alim(101,3.50);
Alim.display();
student Karim(102, 3.60);
Karim.display();
getch();
}
4
5
class student
{
public:
int id; // Member of Class
double gpa; // Member of class
void display() // Function of the class
{
cout<<id<<" "<<gpa<<endl;
}
student(int x,double y)
{
id=x;
gpa=y;
}
};
student() // Default Constructor_Special Function
{
cout<< “ Defaulf constructor”;
} 6
int main()
{
//No value passed
student Rahim; //It’ll call devalufault constructor
student Alim(101,3.50);
Alim.display();
7
Destructor
Destructor is an instance member function that is invoked
automatically whenever an object is going to be destroyed.
A destructor is also a special member function like a
constructor. Destructor destroys the class objects created by
the constructor.
Destructor has the same name as their class name
preceded by a tilde (~) symbol.
It is not possible to define more than one destructor.
The destructor is only one way to destroy the object created
by the constructor. Hence destructor can-not be overloaded.
Destructor neither requires any argument nor returns any
value.
It is automatically called when an object goes out of scope.
Destructor release memory space occupied by the objects
created by the constructor.
In destructor, objects are destroyed in the reverse of an object
creation. 8
#include <iostream>
using namespace std;
class Test
{
public:
// User-Defined Constructor
Test() { cout << "\n Constructor executed";
}
// User-Defined Destructor
~Test() { cout << "\nDestructor executed";
}
};
main()
{ Output -
Test t; Constructor executed
Destructor executed
return 0;
9
}
Encapsulation
Encapsulation in C++ is defined as the wrapping up of data and
information in a single unit. In Object Oriented Programming,
Encapsulation is defined as binding together the data and the
functions that manipulate them.
Two Important property of Encapsulation
Data Protection: Encapsulation protects the internal state of an
object by keeping its data members private.
Information Hiding: Encapsulation hides the internal
implementation details of a class from external code.
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.
10
// C++ program to demonstrate
// Encapsulation
#include <iostream>
using namespace std;
class Encapsulation
{
private:
// Data hidden from outside world
int x;
public:
// Function to set value of Output-
// variable x 5
void set(int a) { x = a; }
// Function to return value of
// variable x
int get() { return x; }
}; 11
int main()
{
Encapsulation obj;
obj.set(5);
cout << obj.get();
return 0;
}
12