You are on page 1of 26

Constructors and Destructors

What is constructor?

A constructor is a member function of a class


which initializes objects of a class.
In C++, Constructor is automatically called when
object(instance of class) create. It is special member
function of the class.
How constructors are different from a normal
member function?
• Constructor has same name as the class itself
• Constructors don’t have return type
• A constructor is automatically called when an object
is created.
• If we do not specify a constructor, C++ compiler
generates a default constructor for us (expects no
parameters and has an empty body).
Types of Constructors
• Default Constructors
• Parameterized Constructors
• Copy Constructor
Default Constructors:
Default constructor is the constructor which
doesn’t take any argument. It has no parameters
Parameterized Constructors:
– 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.

Copy Constructor:
A copy constructor is a member function which
initializes an object using another object of the same
class.
What is Destructor?
Destructor is a member function which destructs
or deletes an object.
When is destructor called?
– The function ends.
– The program ends.
– A block containing local variables ends.
– A delete operator is called.
How destructors are different from a
normal member function?
• Destructors have same name as the class preceded
by a tilde (~).
• Destructors don’t take any argument and don’t
return anything(not even void).
Can there be more than one
destructor in a class?
• No, there can only one destructor in a class
with classname preceded by ~, no parameters
and no return type
#include<iostream.h>
#include<string.h>
//class declaration and definition
class Cars
{
private:
//data members
string company_name;
string model_name;
string fuel_type;
float mileage;
double price;
public:
// default constructor
Cars()
{
cout<<"Default constructor called"<<endl;
}
//parameterized constructor

Cars(string cname, string mname, string ftype, float m, double p)


{
cout<<"Parameterized constructor called"<<endl;
company_name=cname;
model_name=mname;
fuel_type=ftype;
mileage=m;
price=p;
}
//copy constructor
Cars(Cars &obj)
{
cout<<"Copy constructor called"<<endl;
company_name=obj.company_name;
model_name=obj.model_name;
fuel_type=obj.fuel_type;
mileage=obj.mileage;
price=obj.price;
}
//member functions
void setData(string cname,string mname,string
ftype,float m,double p)
{
company_name=cname;
model_name=mname;
fuel_type=ftype;
mileage=m;
price=p;
}
void displayData()
{
cout<<"Car Properties"<<endl;
cout<<"Car Company Name:"<<company_name<<endl;
cout<<"Car Company Model : "<<model_name<<endl;
cout<<"Car Fuel Type : "<<fuel_type<<endl;
cout<<"car Mileage : "<<mileage<<endl;
cout<<"Car Price : "<<price<<endl<<endl;
}
//destructor

~Cars()
{
cout<<"Destructor called"<<endl;
}
};
int main()
{
Cars
car1,car2("Toyota","fortuner","Diesel",10,350000);
car1.setData("Toyota","Innova","petrol",15.5,150000);
car1.displayData();
car2.displayData();
Cars car3=car1; //copy constructor is called
car3.displayData();
return 0;
}
Output:
Default constructor called
Parameterized constructor called
Car Properties
Car Company Name : Toyota
Car Company Model : Innova
Car Fuel Type : petrol
car Mileage : 15.5
Car Price : 150000

Car Properties
Car Company Name : Toyota
Car Company Model : fortuner
Car Fuel Type : Diesel
car Mileage : 10
Car Price : 350000
Copy constructor called
Car Properties
Car Company Name : Toyota
Car Company Model : Innova
Car Fuel Type : petrol
car Mileage : 15.5
Car Price : 150000

Destructor called
Destructor called
Destructor called
Default Constructor
#include <iostream> int main()
class construct {
// Default constructor called
{ automatically
public: // when the object is created

int a, b; construct c;
// Default Constructor cout << "a: " << c.a <<
construct() endl
{ << "b: " << c.b;
a = 10; return 1;
b = 20; }
}
};
Parameterized Constructor
#include <iostream> int getX()
class Point { {
private: return x;
int x, y; }
public: int getY()
// Parameterized Constructor {
Point(int x1, int y1) return y;
{ }
x = x1; };
y = y1;
}
int main()
{
// Constructor called
Point p1(10, 15);
// Access values assigned by constructor
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
return 0;
}
Copy constructor
#include<iostream> // Copy constructor
class Point Point(const Point &p2)
{
{ x = p2.x;
private: y = p2.y;
int x, y; }
public: int getX()
Point(int x1, int y1) {
return x;
{ }
x = x1; int getY()
y = y1; {
} return y;
}
};
int main()
{
Point p1(10, 15); // Normal constructor is called here
Point p2 = p1; // Copy constructor is called here
// Let us access values assigned by constructors
cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY();
cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY();
return 0;
}

You might also like