Chap 0205
Chap 0205
• Constructor
• Destructor
Constructor
The process of initializing and starting up a class is called construction.
A constructor is a special member function of the class which has the same name as that of the
class. It is automatically invoked when we declare/create new objects of the class. It is called
constructor, because it constructs the value of data members of the class.
Construction Rules :
• The rule for constructing an object of a simple class is:
1. Call the constructor/initializer for each data member, in sequence.
2. Call the constructor for the class.
1. Call the constructor for the base class (which recursively calls the constructors needed to
Completely initialize the base class object.)
2. Call the constructor/initializer for each data member of the derived class, in sequence.
3. Call the constructor for the derived class
Syntax :
class class_name
{
public: class_name()
{
// Constructor code
}
//... other Variables & Functions
};
Types Of Constructor
Default Constructor
Parameterized Constructor
Copy Constructor
1. Default Constructor
A default constructor does not have any parameters or if it has parameters, all
the parameters have default values.
Syntax:
class class_name
{
public:
// Default constructor with no arguments
class_name();
// Other Members Declaration
};
2. Parameterized Constructor
If a Constructor has parameters, it is called a Parameterized Constructor.
Parameterized Constructors assist in initializing values when an object is
created.
Example Program for Parameterized Constructor In C++
#include<iostream>
using namespace std;
class Employee
{
public:
int id; string name;
Output :
Employee(int i, string n) { 101 Sonoo
id = i; 102 Nakul
name = n;
}
void display() {
cout<<id<<" "<<name<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo");
e2=Employee(102, "Nakul");
e1.display();
e2.display();
return 0;
}
Copy Constructor
• A copy constructor is a like a normal parameterized Constructor, but which parameter is the
same class object.
• Copy constructor uses to initialize an object using another object of the same class.
A copy constructor is a member function which initializes an object using another object of the
same class.
ClassName (constClassName&old_obj);
Following is a simple example of copy constructor.
#include<iostream>
using namespace std;
class Point {
int x, y;
public:
Point(int x1, int y1) {
x = x1; y = y1;
} Output :
P1.x=10, p1.y=15
Point(const Point &p2) {x = p2.x; y = p2.y; } P2.x=10, px.y=15
int getX() { return x; }
int getY() { return y; }
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
cout<< "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout<< "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}
Destructor
Destructor is a member function which destructs or deletes an object. The destructor function has the
same as the constructor, but it is preceded by a (tilde sign).
1. Destructors are special member functions of the class required to free the memory of the
object whenever it goes out of scope.
2. Destructors are parameter less functions.
3. Name of the Destructor should be exactly same as that of name of the class. But preceded by ‘~’ (tilde).
4. Destructor does not have any return type. Not even void.
5. The Destructor of class is automatically called when object goes out of scope.
Destruction Rules:
When an object is deleted, the destructors are called in the opposite order.
The rule for an object of a derived class is:
1. Call the destructor for the derived class.
2. Call the destructor for each data member object of the derived class in reverse sequence.
3. Call the destructor for the base class.
Example
};
#include<iostream>
usingnamespace std; int main( )
class Marks {
{ Marks m1;
public: Marks m2;
int maths , science; return 0;
}
Marks() //constructor
{
Output
cout<< "Inside Constructor"<<endl; Inside Constructor
cout<< "C++ Object created"<<endl; C++ Object created
} Inside Constructor
C++ Object created
~Marks() //destructor
{ Inside Destructor
cout<< "Inside Destructor"<<endl; C++ Object destructed
cout<< "C++ Object destructed"<<endl; Inside Destructor
} C++ Object destructed
Do Subscribe