Constructor & Destructors in C++
• Constructor Definition
• Types of Constructor
• Default Constructor
• Parameterized Constructor
• Copy Constructor
By
Prof. Sandeep Vishwakarma
Types of Constructors
• Default Constructor
• Parameterized Constructor
• Copy Constructor
#include <iostream.h>
#include <conio.h>
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
getch();
}
#include<iostream.h>
#include<conio.h>
class Employee
{
public:
int id;
string name;
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
Parameterized Constructor Example
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
} };
int main()
{
Employee e1 =Employee(101, "Sandeep",12000);
Employee e2=Employee(102, "Amit", 15000);
e1.display();
e2.display();
getch();
}
When Copy Constructor is called
• When we initialize the object with another
existing object of the same class type. For
example, Student s1 = s2, where Student is
the class.
• When the object of the same class type is
passed by value as an argument.
• When the function returns the object of the
same class type by value.
#include<iostream.h> Copy Constructor Example
#include<conio.h>
class A
{
public: int x;
A(int a) // parameterized constructor.
{
x=a;
}
A(A &i) // copy constructor
{
x = i.x;
} };
int main()
{
A a1(20); // Calling the parameterized constructor.
A a2(a1); // Calling the copy constructor.
cout<<a2.x;
getch(); }
Thank You
Constructor in C++
• Destructor in C++
• Constructor Overloading
• Constructor Overloading Example
• This Keyword
• Hands on Example
By
Prof. Sandeep Vishwakarma
Rules for Destructors
• Destructor is a type of special member function of a
class.
• It is used to destroy the memory allocated by the
constructor.
• It has the same name as the class prefixed with a tilde
(~) sign.
• Destructor does not take any arguments and cannot
return or accept any value.
• It is called automatically by the compiler when the
object goes out of scope.
• Compiler calls the destructor implicitly when the
program execution is exited.
Example - 1 Constructor Overloading
class Student
{
public:
int rollno;
string name;
Student(int x) // first constructor
{
rollno = x;
name = "None";
}
Student(int x, string str) // second constructor
{
rollno = x;
name = str;
}};
int main()
{
Student A(10);
Student B(11, "John");
getch();
}
Example - 2 Constructor Overloading
#include <iostream.h>
#include <conio.h>
class ABC
{
private:
int x,y;
public:
ABC () //constructor 1 with no arguments
{
x = y = 0;
}
ABC(int a) //constructor 2 with one argument
{
x = y = a;
}
ABC(int a, int b) //constructor 3 with two argument
{
x = a;
y = b;
}
void display()
{
cout << "x = " << x << " and " << "y = " << y << endl;
} };
int main()
{
ABC cc1; //constructor 1
ABC cc2(10); //constructor 2
ABC cc3(10,20); //constructor 3
cc1.display();
cc2.display();
cc3.display();
getch(); }
this Keyword
this is a keyword that refers to the current
instance of the class. There can be 3 main
usage of this keyword in C++.
• It can be used to pass current object as a
parameter to another method.
• It can be used to refer current class
instance variable.
• It can be used to declare indexers.
Example: this Keyword
#include <iostream.h>
#include <conio.h>
class Employee
{
public:
int id; //data member (also instance variable)
string name; //data member(also instance variable)
float salary;
Employee(int id, string name, float salary)
{
this->id = id;
this->name = name;
this->salary = salary;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
} };
int main(void)
{
Employee e1 =Employee(101, “Amit", 12000);
Employee e2=Employee(102, "Nakul", 15000);
e1.display();
e2.display();
getch();
}
Thank You

Constructor and Destructors in C++

  • 1.
    Constructor & Destructorsin C++ • Constructor Definition • Types of Constructor • Default Constructor • Parameterized Constructor • Copy Constructor By Prof. Sandeep Vishwakarma
  • 8.
    Types of Constructors •Default Constructor • Parameterized Constructor • Copy Constructor
  • 10.
    #include <iostream.h> #include <conio.h> classEmployee { public: Employee() { cout<<"Default Constructor Invoked"<<endl; } }; int main(void) { Employee e1; //creating an object of Employee Employee e2; getch(); }
  • 17.
    #include<iostream.h> #include<conio.h> class Employee { public: int id; stringname; float salary; Employee(int i, string n, float s) { id = i; name = n; salary = s; } Parameterized Constructor Example
  • 18.
    void display() { cout<<id<<" "<<name<<""<<salary<<endl; } }; int main() { Employee e1 =Employee(101, "Sandeep",12000); Employee e2=Employee(102, "Amit", 15000); e1.display(); e2.display(); getch(); }
  • 20.
    When Copy Constructoris called • When we initialize the object with another existing object of the same class type. For example, Student s1 = s2, where Student is the class. • When the object of the same class type is passed by value as an argument. • When the function returns the object of the same class type by value.
  • 21.
    #include<iostream.h> Copy ConstructorExample #include<conio.h> class A { public: int x; A(int a) // parameterized constructor. { x=a; } A(A &i) // copy constructor { x = i.x; } }; int main() { A a1(20); // Calling the parameterized constructor. A a2(a1); // Calling the copy constructor. cout<<a2.x; getch(); }
  • 24.
  • 25.
    Constructor in C++ •Destructor in C++ • Constructor Overloading • Constructor Overloading Example • This Keyword • Hands on Example By Prof. Sandeep Vishwakarma
  • 27.
    Rules for Destructors •Destructor is a type of special member function of a class. • It is used to destroy the memory allocated by the constructor. • It has the same name as the class prefixed with a tilde (~) sign. • Destructor does not take any arguments and cannot return or accept any value. • It is called automatically by the compiler when the object goes out of scope. • Compiler calls the destructor implicitly when the program execution is exited.
  • 33.
    Example - 1Constructor Overloading class Student { public: int rollno; string name; Student(int x) // first constructor { rollno = x; name = "None"; }
  • 34.
    Student(int x, stringstr) // second constructor { rollno = x; name = str; }}; int main() { Student A(10); Student B(11, "John"); getch(); }
  • 35.
    Example - 2Constructor Overloading #include <iostream.h> #include <conio.h> class ABC { private: int x,y; public: ABC () //constructor 1 with no arguments { x = y = 0; } ABC(int a) //constructor 2 with one argument { x = y = a; }
  • 36.
    ABC(int a, intb) //constructor 3 with two argument { x = a; y = b; } void display() { cout << "x = " << x << " and " << "y = " << y << endl; } }; int main() { ABC cc1; //constructor 1 ABC cc2(10); //constructor 2 ABC cc3(10,20); //constructor 3 cc1.display(); cc2.display(); cc3.display(); getch(); }
  • 37.
    this Keyword this isa keyword that refers to the current instance of the class. There can be 3 main usage of this keyword in C++. • It can be used to pass current object as a parameter to another method. • It can be used to refer current class instance variable. • It can be used to declare indexers.
  • 38.
    Example: this Keyword #include<iostream.h> #include <conio.h> class Employee { public: int id; //data member (also instance variable) string name; //data member(also instance variable) float salary; Employee(int id, string name, float salary) { this->id = id; this->name = name; this->salary = salary; }
  • 39.
    void display() { cout<<id<<" "<<name<<""<<salary<<endl; } }; int main(void) { Employee e1 =Employee(101, “Amit", 12000); Employee e2=Employee(102, "Nakul", 15000); e1.display(); e2.display(); getch(); }
  • 40.