Constructor and Destructor
Constructor
• In C++, constructor is a special method which is invoked automatically
at the time of object creation.
• It is used to initialize the data members of new object generally.
• The constructor in C++ has the same name as class or structure.
• There can be two types of constructors in C++.
1. Default constructor
2. Parameterized constructor
Default Constructor
• A constructor which has no argument is known as default constructor.
• It is invoked at the time of creating object.
Default Constructor
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2;
return 0;
}
Parameterized Constructor
• A constructor which has parameters is called parameterized
constructor.
• It is used to provide different values to distinct objects.
Parameterized Constructor
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
int main()
{
int a=10,*c;
int *p;
p=&a;
c=p;
cout<<a<<endl;
cout<<p<<endl;
cout<<c<<endl;
cout<<*c<<endl;
}
Pointer Basic
Copy Constructor
• A Copy constructor is an overloaded constructor used to declare and
initialize an object from another object.
Copy Constructor is of two types:
• Default Copy constructor: The compiler defines the default copy
constructor. If the user defines no copy constructor, compiler supplies
its constructor.
• User Defined constructor: The programmer defines the user-defined
constructor.
Syntax of User-defined Copy Constructor:
Class_name(const class_name &old_object);
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;
return 0;
}
When Copy Constructor is called
• Copy Constructor is called in the following scenarios:
• 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.
• Two types of copies are produced by the constructor:
❖Shallow copy
❖Deep copy
https://www.javatpoint.com/cpp-copy-constructor
Copy constructor and Assignment operator(=)
• https://www.javatpoint.com/cpp-copy-constructor
Destructor
• 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 (~).
Constructor and Destructor Example
class Employee
{
public:
Employee()
{
cout<<"Constructor Invoked"<<endl;
}
~Employee()
{
cout<<"Destructor Invoked"<<endl;
}
};
int main(void)
{
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
return 0;
}
Output:
Constructor Invoked
Constructor Invoked
Destructor Invoked
Destructor Invoked

Constructor and Destructor.pdf

  • 1.
  • 2.
    Constructor • In C++,constructor is a special method which is invoked automatically at the time of object creation. • It is used to initialize the data members of new object generally. • The constructor in C++ has the same name as class or structure. • There can be two types of constructors in C++. 1. Default constructor 2. Parameterized constructor
  • 3.
    Default Constructor • Aconstructor which has no argument is known as default constructor. • It is invoked at the time of creating object.
  • 4.
    Default Constructor class Employee { public: Employee() { cout<<"DefaultConstructor Invoked"<<endl; } }; int main(void) { Employee e1; //creating an object of Employee Employee e2; return 0; }
  • 5.
    Parameterized Constructor • Aconstructor which has parameters is called parameterized constructor. • It is used to provide different values to distinct objects.
  • 6.
    Parameterized Constructor class Employee{ public: int id;//data member (also instance variable) string name;//data member(also instance variable) float salary; Employee(int i, string n, float s) { id = i; name = n; salary = s; } void display() { cout<<id<<" "<<name<<" "<<salary<<endl; } }; int main(void) { Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee Employee e2=Employee(102, "Nakul", 59000); e1.display(); e2.display(); return 0; }
  • 7.
    int main() { int a=10,*c; int*p; p=&a; c=p; cout<<a<<endl; cout<<p<<endl; cout<<c<<endl; cout<<*c<<endl; } Pointer Basic
  • 8.
    Copy Constructor • ACopy constructor is an overloaded constructor used to declare and initialize an object from another object. Copy Constructor is of two types: • Default Copy constructor: The compiler defines the default copy constructor. If the user defines no copy constructor, compiler supplies its constructor. • User Defined constructor: The programmer defines the user-defined constructor.
  • 9.
    Syntax of User-definedCopy Constructor: Class_name(const class_name &old_object);
  • 10.
    class A { public: int x; A(inta) // 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; return 0; }
  • 11.
    When Copy Constructoris called • Copy Constructor is called in the following scenarios: • 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. • Two types of copies are produced by the constructor: ❖Shallow copy ❖Deep copy https://www.javatpoint.com/cpp-copy-constructor
  • 12.
    Copy constructor andAssignment operator(=) • https://www.javatpoint.com/cpp-copy-constructor
  • 13.
    Destructor • A destructorworks 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 (~).
  • 14.
    Constructor and DestructorExample class Employee { public: Employee() { cout<<"Constructor Invoked"<<endl; } ~Employee() { cout<<"Destructor Invoked"<<endl; } }; int main(void) { Employee e1; //creating an object of Employee Employee e2; //creating an object of Employee return 0; } Output: Constructor Invoked Constructor Invoked Destructor Invoked Destructor Invoked