Constructor and Destructor: Kalinga Institute of Industrial Technology School of Computer Engineering
Constructor and Destructor: Kalinga Institute of Industrial Technology School of Computer Engineering
Constructor and Destructor: Kalinga Institute of Industrial Technology School of Computer Engineering
Constructor
Default Constructor
Parameterized Constructor
Copy- Constructor
Destructor
What is constructor?
3
Features of Constructor
4
Types of Constructor
5
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
#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
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
#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