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
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; }