You are on page 1of 20

Constructor and Destructor

KALINGA INSTITUTE OF INDUSTRIAL


TECHNOLOGY

School Of Computer Engineering

Mr. Abhaya Kumar Sahoo


Assistant Professor [II]
School of Computer Engineering,
Kalinga Institute of Industrial Technology (KIIT),
Deemed to be University,Odisha

3 Credit Lecture Note 10


Chapter Contents
2

 Constructor
 Default Constructor
 Parameterized Constructor
 Copy- Constructor
 Destructor
What is constructor?
3
Features of Constructor
4
Types of Constructor
5

There can be three types of constructors in C++.

1. Default constructor
2. Parameterized constructor
3. Copy constructor
C++ Default Constructor
6
 A constructor which has no argument is known as default constructor.
 It is invoked at the time of creating object.
 If no such constructor is defined, the compiler supplies a default constructor

int main(void)
#include <iostream> {
using namespace std; Employee e1; //creating an
class Employee object of Employee
{ Employee e2;
public: return 0;
Employee() }
{
cout<<"Default Constructor Invoked"<<endl;
}
};
Example: Default Constructor
7
C++ Parameterized Constructor
8

 A constructor which has parameters is called parameterized constructor.


 It is used to provide different values to distinct objects.
 It is possible to pass arguments to constructors.
 Typically, these arguments help initialize an object when it is created.
 To create a parameterized constructor, simply add parameters to it the way you
would to any other function.
 When you define the constructor’s body, use the parameters to initialize the
object.
C++ Parameterized Constructor
9
Example 1: Parameterized Constructor
10
#include <iostream>
using namespace std; int main(void)
class Employee { {
public: Employee e1 =Employee(101, "Sonoo",
int id;//data member (also instance variable) 890000); //creating an object of
Employee
string name;//data member(also instance variable)
float salary; Employee e2=Employee(102, "Nakul",
Employee(int i, string n, float s) 59000);
{ e1.display();
id = i; e2.display();
return 0;
name = n; }
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
Example 2: Parameterized Constructor
11

#include <iostream>
int getY()
using namespace std;
{
class Point {
return y;
private:
}
int x, y;
};
public:
int main()
Point(int x1, int y1)
{
{
Point p1(10, 15);
x = x1;
cout << "p1.x = " << p1.getX() << ", p1.y = "
y = y1; << p1.getY();
} return 0;
int getX() }
{
return x;
}
Uses of Parameterized Constructor
12

 It is used to initialize the various data elements of different objects with


different values when they are created.
 It is used to overload constructors.
Copy Constructor
13

 A copy constructor is a member function which initializes an object using


another object of the same class.
 A copy constructor takes a reference to an object of the same class as itself as an
argument.

 Syntax:
class-name (class-name &)
{
...
}
Copy Constructor
14
Example
15

#include <iostream>
using namespace std;
class A int main()
{ {
public: A a1(20); // Calling the parameterized
constructor.
int x;
A(int a) // parameterized constructor.
A a2(a1); // Calling the copy
{ constructor.
x=a; cout<<a2.x;
} return 0;
A(A &i) // copy constructor }
{
x = i.x; O/P: 20
}
};
Example
16

#include <iostream>
code ( code &x)
using namespace std; {
class code { id=x.id;
}
private:
void display()
int id; {
public: cout<<id;
}
code() };
{ int main()
cout<<“Default Constructor”; {
code A(100);
} code B(A); //copy constructor called
code(int a) code C=A; //copy constructor called
{ code D;
D=A; //copy constructor notcalled
id=a; A.display();
} B.display();
C.display();
D.display();
}
Copy constructor:
17

#include<iostream>
int main()
using namespace std;
{
class Point
Point p1(10, 15); // Normal
{ constructor is called here
private: Point p2 = p1; // Copy constructor
int x, y; is called here
public: // Let us access values assigned by
Point(int x1, int y1) constructors
{ x = x1; y = y1; } cout << "p1.x = " << p1.getX() <<
// Copy constructor ", p1.y = " << p1.getY();
Point(const Point &p2)
{x = p2.x; y = p2.y; } cout << "\np2.x = " << p2.getX()
<< ", p2.y = " << p2.getY();
int getX() { return x; }
int getY() { return y; }
return 0;
};
}
Destructor
18

 A destructor works opposite to constructor; it destructs the objects of classes. It


can be defined only once in a class.
 Like constructors, it is invoked automatically.
 A destructor is defined like constructor. It must have same name as class. But it
is prefixed with a tilde sign (~).
Destructor
19

#include <iostream>
using namespace std; int main(void)
class Employee {
{ Employee e1; //creating
public: an object of Employee
Employee() Employee e2; //creating
an object of Employee
{
return 0;
cout<<"Constructor Invoked"<<endl;
}
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
20

You might also like