Professional Documents
Culture Documents
What is constructor?
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()
{
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;
}