SlideShare a Scribd company logo
1 of 40
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

More Related Content

What's hot

What's hot (20)

class and objects
class and objectsclass and objects
class and objects
 
Linked list
Linked listLinked list
Linked list
 
Static Data Members and Member Functions
Static Data Members and Member FunctionsStatic Data Members and Member Functions
Static Data Members and Member Functions
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
Inline Functions and Default arguments
Inline Functions and Default argumentsInline Functions and Default arguments
Inline Functions and Default arguments
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
C++ classes tutorials
C++ classes tutorialsC++ classes tutorials
C++ classes tutorials
 
Constructors and Destructor in C++
Constructors and Destructor in C++Constructors and Destructor in C++
Constructors and Destructor in C++
 
Constructor and Destructor
Constructor and DestructorConstructor and Destructor
Constructor and Destructor
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
Abstract class
Abstract classAbstract class
Abstract class
 
Friend functions
Friend functions Friend functions
Friend functions
 
Late and Early binding in c++
Late and Early binding in c++Late and Early binding in c++
Late and Early binding in c++
 
Pointers, virtual function and polymorphism
Pointers, virtual function and polymorphismPointers, virtual function and polymorphism
Pointers, virtual function and polymorphism
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 

Similar to Constructor and Destructors in C++

CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxDeepasCSE
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Abu Saleh
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdfMadnessKnight
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructorsAkshaya Parida
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Tutconstructordes
TutconstructordesTutconstructordes
TutconstructordesNiti Arora
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&deleteShehzad Rizwan
 
21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptxAnantjain234527
 

Similar to Constructor and Destructors in C++ (20)

CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptxCONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
 
Tut Constructor
Tut ConstructorTut Constructor
Tut Constructor
 
Constructor
ConstructorConstructor
Constructor
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13Lecture 3, c++(complete reference,herbet sheidt)chapter-13
Lecture 3, c++(complete reference,herbet sheidt)chapter-13
 
Constructor and Destructor.pdf
Constructor and Destructor.pdfConstructor and Destructor.pdf
Constructor and Destructor.pdf
 
constructors and destructors
constructors and destructorsconstructors and destructors
constructors and destructors
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
Pointers
PointersPointers
Pointers
 
P1
P1P1
P1
 
Tutconstructordes
TutconstructordesTutconstructordes
Tutconstructordes
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
Friend this-new&delete
Friend this-new&deleteFriend this-new&delete
Friend this-new&delete
 
21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx21CSC101T best ppt ever OODP UNIT-2.pptx
21CSC101T best ppt ever OODP UNIT-2.pptx
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 

More from sandeep54552

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxsandeep54552
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptxsandeep54552
 
Dijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxDijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxsandeep54552
 
YCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.pptYCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.pptsandeep54552
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptxsandeep54552
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptxsandeep54552
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxsandeep54552
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptxsandeep54552
 
File handling in c++
File handling in c++File handling in c++
File handling in c++sandeep54552
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++sandeep54552
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++ sandeep54552
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligencesandeep54552
 

More from sandeep54552 (20)

Dijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptxDijkstra Searching Algorithms Shortest.pptx
Dijkstra Searching Algorithms Shortest.pptx
 
E_R-Diagram (2).pptx
E_R-Diagram (2).pptxE_R-Diagram (2).pptx
E_R-Diagram (2).pptx
 
Dijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptxDijkstra Searching Algorithms.pptx
Dijkstra Searching Algorithms.pptx
 
DFS_New.pptx
DFS_New.pptxDFS_New.pptx
DFS_New.pptx
 
Agents_AI.ppt
Agents_AI.pptAgents_AI.ppt
Agents_AI.ppt
 
YCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.pptYCMOU_FYBCA_DS_Unit-7.ppt
YCMOU_FYBCA_DS_Unit-7.ppt
 
Queue_Data_Structure.pptx
Queue_Data_Structure.pptxQueue_Data_Structure.pptx
Queue_Data_Structure.pptx
 
Tree_Definition.pptx
Tree_Definition.pptxTree_Definition.pptx
Tree_Definition.pptx
 
Stack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptxStack_Application_Infix_Prefix.pptx
Stack_Application_Infix_Prefix.pptx
 
Stack_Data_Structure.pptx
Stack_Data_Structure.pptxStack_Data_Structure.pptx
Stack_Data_Structure.pptx
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
 
Quick_sort1.pptx
Quick_sort1.pptxQuick_sort1.pptx
Quick_sort1.pptx
 
Link_List.pptx
Link_List.pptxLink_List.pptx
Link_List.pptx
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
File handling in c++
File handling in c++File handling in c++
File handling in c++
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Inheritance in c++
Inheritance in c++ Inheritance in c++
Inheritance in c++
 
Jsp tag library
Jsp tag libraryJsp tag library
Jsp tag library
 
Hill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligenceHill climbing algorithm in artificial intelligence
Hill climbing algorithm in artificial intelligence
 
Session bean
Session beanSession bean
Session bean
 

Recently uploaded

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Recently uploaded (20)

Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

Constructor and Destructors in C++

  • 1. Constructor & Destructors in C++ • Constructor Definition • Types of Constructor • Default Constructor • Parameterized Constructor • Copy Constructor By Prof. Sandeep Vishwakarma
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8. Types of Constructors • Default Constructor • Parameterized Constructor • Copy Constructor
  • 9.
  • 10. #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(); }
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17. #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
  • 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(); }
  • 19.
  • 20. 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.
  • 21. #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(); }
  • 22.
  • 23.
  • 25. Constructor in C++ • Destructor in C++ • Constructor Overloading • Constructor Overloading Example • This Keyword • Hands on Example By Prof. Sandeep Vishwakarma
  • 26.
  • 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.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33. Example - 1 Constructor Overloading class Student { public: int rollno; string name; Student(int x) // first constructor { rollno = x; name = "None"; }
  • 34. Student(int x, string str) // second constructor { rollno = x; name = str; }}; int main() { Student A(10); Student B(11, "John"); getch(); }
  • 35. 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; }
  • 36. 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(); }
  • 37. 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.
  • 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(); }