Constructor and Destructor
Malik Jahan Khan
1
Constructor
• Sometimes, it may be convenient if an object can
initialize itself when it’s first created, without
requiring a separate call to a member function
• Automatic initialization is carried out using a
special member function called constructor
• A constructor is a member function which is
executed whenever an object is created
• Constructor must have the same name as that of
class
2
#include <iostream>
using namespace std;
class Counter
{
private:
unsigned int count;
//count
public:
Counter() : count(0)
//constructor
{ /*empty body*/ }
void inc_count()
//increment count
{ count++; }
int get_count()
//return count
{ return count; }
};
int main()
{
Counter c1, c2;
//define and initialize
cout << "\nc1=" << c1.get_count(); //display
cout << "\nc2=" << c2.get_count();
c1.inc_count();
//increment c1
c2.inc_count();
//increment c2
c2.inc_count();
//increment c2
cout << "\nc1=" << c1.get_count(); //display again
cout << "\nc2=" << c2.get_count();
cout << endl;
return 0;
}
Counter Example
Output
C1=0
C2=0
C1=1
C2=2
3
An Alternate Way of Initializing
• count()
{ count =0;}
OR
count():count(0) //Initializer List
{}
• Members initialized in the initializer list are
given value before the execution of the
constructor
4
Initializer List
• Constructor is used to initialize data members
at the time of creation of an object
• Initializer List: When more than one data
members need to be initialized
– someClass() : m1(7), m2(33), m3(4) //Initializer List
{/*Empty Body*/}
5
Destructor
• Constructor is a member function of a class which
is automatically called when an object is created
of the that class
• Destructor is a member function of a class which
does the opposite of a constructor
– It is automatically called when an object is destroyed
from memory
• Destructor also has the same name as that of a
class but a tilde sign before name, like
~className(){…}
6
#include<iostream>
using namespace std;
class Person
{
private:
int id;
public:
void setID(int d)
{
id=d;
}
void showID()
{
cout<<"\n My ID is "<<id;
}
~Person()
{
cout<<"\n ID No. "<<id<<" says you bye bye!";
}
};
int main()
{
Person p1,p2,p3;
p1.setID(10);
p2.setID(20);
p3.setID(30);
p1.showID();
p2.showID();
p3.showID();
}
Example
Output
My ID is 10
My ID is 20
My ID is 30
ID No. 30 says you bye bye!
ID No. 20 says you bye bye!
ID No. 10 says you bye bye!
7
#include<iostream>
using namespace std;
class Person
{
private:
int id;
public:
Person() : id(0)
{
cout<<"\n ID No. "<<id<<" says welcome!";
}
void setID(int d)
{
id=d;
}
void showID()
{
cout<<"\n My ID is "<<id;
}
~Person()
{
cout<<"\n ID No. "<<id<<" says you bye bye!";
}
};
int main()
{
Person p2,p1,p3;
p1.setID(10);
p2.setID(20);
p3.setID(30);
p1.showID();
p2.showID();
p3.showID();
}
Another Example
Output
ID No. 0 says welcome!
ID No. 0 says welcome!
ID No. 0 says welcome!
My ID is 10
My ID is 20
My ID is 30
ID No. 30 says you bye bye!
ID No. 10 says you bye bye!
ID No. 20 says you bye bye!
8
Homework Exercises
• Chapter 6
– Exercise No. 1, 2, 3
9
© Copyright 2026 Paperzz