Constructor and Destructors in C++

923 views 40 slides Mar 21, 2022
Slide 1
Slide 1 of 40
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40

About This Presentation

Constructor and Destructors in C++


Slide Content

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