SlideShare a Scribd company logo
Object Oriented Programming
Constructors and Destructors
Constructors
• A special member function
• Called automatically when object of that class is
instantiated/created.
• Has the same name as the class it belongs to
• No return type is used for constructors
• Constructors can be parameterized
• Constructors can be overloaded
• Purpose of Constructor
– Perform task needed at the time of object creation like
• Initialize data members
• Get hold on some locks or resources(like files)
• Allocation of run time memory
Types of constructors
• There are two types of constructors
– Default Constructor
• Have no parameters
– Parameterized Constructor
• Have parameters
Types of constructors
• Default Constructor
• Takes no parameters
• Example:
• An alternative way to initialize data members
class Point{
private:
int x,y;
public:
Point(){ //Default ctor
x = 0;
y = 0;
cout<<“I am default ctor of Point class”<<endl;
}
};
class Point{
private:
int x,y;
public:
Point() : x(0), y(0){ //Default ctor
cout<<“I am default ctor of Point class”<<endl;
}
};
Types of constructors
• Parameterized Constructor
• Have parameters
• Example:
• An alternative way to initialize data members
class Point{
private:
int x,y;
public:
Point(int a, int b){ //Parameterized ctor
x = a;
y = b;
cout<<“I am parameterized ctor of Point class”<<endl;
}
};
class Point{
private:
int x,y;
public:
Point(int a, int b) : x(a), y(b){ //Parameterized ctor
cout<<“I am parameterized ctor of Point class”<<endl;
}
};
Constructor Overloading
• Constructors can be overloaded
– It means we can have multiple constructors in a
class
– Objects can be instantiated in different ways
Constructor
Example program Counter
class Counter
{
private:
unsigned int count;
public:
Counter() : count(0)
{ /*empty body*/ }
void inc_count() //increment count
{ count++; }
int get_count() //return count
{ return count; }
};
int main()
{
Counter c1, c2; //define and initialize
cout << “nc1=” << c1.get_count();
cout << “nc2=” << c2.get_count();
c1.inc_count(); //increment c1
c2.inc_count(); //increment c2
c2.inc_count(); //increment c2
cout << “nc1=” << c1.get_count();
cout << “nc2=” << c2.get_count();
}
Output
c1=0
c2=0
c1=1
c2=2
Destructors
• A special member function
• Called automatically when object of that class is
destroyed.
• Has the same name as the class it belongs to
followed by a ~ sign
• No return type is used for destructors
• Destructors can not be parameterized
• Destructors can not be overloaded
• Purpose of Destructor
– Perform task needed at the time of object killing like
• Release resources acquired by an object
• De-allocation of run time memory
Destructors
• An example
class Counter{
private:
int count;
public:
Counter() : count(0){
cout<<“I am ctor of Counter class”<<endl;
}
~Counter(){ //Destructor of Counter class
cout<<“I am dtor of Counter class”<<endl;
}
};
void main(){
cout<<“Start of main”<<endl;
Counter a;
{ cout<<“Inside the block”<<endl;
Counter b;
cout<<“Exiting block”<<endl;
}
cout<<“Exiting main”<<endl;
}
Output
Start of main
I am ctor of Counter class
Inside the block
I am ctor of Counter class
Exiting block
I am dtor of Counter class
Exiting main
I am dtor of Counter class

More Related Content

PDF
Constructor and Destructor.pdf
PPTX
constructocvbcvbcvbcvbr-Destructor (1).pptx
PPTX
Constructors and Destructor_detilaed contents.pptx
PPT
Friend this-new&delete
PPTX
Constructor & destructor
PPTX
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPTX
constructor in object oriented program.pptx
Constructor and Destructor.pdf
constructocvbcvbcvbcvbr-Destructor (1).pptx
Constructors and Destructor_detilaed contents.pptx
Friend this-new&delete
Constructor & destructor
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
An introduction to Constructors and destructors in c++ .pptx
constructor in object oriented program.pptx

Similar to Constructors & Destructors in C++ Simplified (20)

PPTX
Constructors and Destructors
PPTX
PDF
00-intro-to-classes.pdf
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPTX
PPT
Constructor,destructors cpp
PPTX
Constructors and Destructors in C++.pptx
PPTX
Constructors in C++.pptx
PPTX
Constructor and Destructor
PPT
classes and constructors lec 03 & 04.ppt
PPTX
Object orinted programming lecture| Overlaoded Functions
PPTX
OOP.pptx
PPT
Constructor
PPT
Constructor and destructor in C++
PPT
Constructors and destructors in C++ part 2
PDF
Classes
PPTX
Class and object C++.pptx
PPTX
Learning C++ - Class 4
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Constructors and Destructors
00-intro-to-classes.pdf
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Constructor,destructors cpp
Constructors and Destructors in C++.pptx
Constructors in C++.pptx
Constructor and Destructor
classes and constructors lec 03 & 04.ppt
Object orinted programming lecture| Overlaoded Functions
OOP.pptx
Constructor
Constructor and destructor in C++
Constructors and destructors in C++ part 2
Classes
Class and object C++.pptx
Learning C++ - Class 4
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
Ad

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
01-Introduction-to-Information-Management.pdf
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Complications of Minimal Access Surgery at WLH
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Institutional Correction lecture only . . .
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Insiders guide to clinical Medicine.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
01-Introduction-to-Information-Management.pdf
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Complications of Minimal Access Surgery at WLH
Abdominal Access Techniques with Prof. Dr. R K Mishra
Institutional Correction lecture only . . .
Sports Quiz easy sports quiz sports quiz
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
TR - Agricultural Crops Production NC III.pdf
Renaissance Architecture: A Journey from Faith to Humanism
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
Insiders guide to clinical Medicine.pdf
GDM (1) (1).pptx small presentation for students
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
RMMM.pdf make it easy to upload and study
Ad

Constructors & Destructors in C++ Simplified

  • 2. Constructors • A special member function • Called automatically when object of that class is instantiated/created. • Has the same name as the class it belongs to • No return type is used for constructors • Constructors can be parameterized • Constructors can be overloaded • Purpose of Constructor – Perform task needed at the time of object creation like • Initialize data members • Get hold on some locks or resources(like files) • Allocation of run time memory
  • 3. Types of constructors • There are two types of constructors – Default Constructor • Have no parameters – Parameterized Constructor • Have parameters
  • 4. Types of constructors • Default Constructor • Takes no parameters • Example: • An alternative way to initialize data members class Point{ private: int x,y; public: Point(){ //Default ctor x = 0; y = 0; cout<<“I am default ctor of Point class”<<endl; } }; class Point{ private: int x,y; public: Point() : x(0), y(0){ //Default ctor cout<<“I am default ctor of Point class”<<endl; } };
  • 5. Types of constructors • Parameterized Constructor • Have parameters • Example: • An alternative way to initialize data members class Point{ private: int x,y; public: Point(int a, int b){ //Parameterized ctor x = a; y = b; cout<<“I am parameterized ctor of Point class”<<endl; } }; class Point{ private: int x,y; public: Point(int a, int b) : x(a), y(b){ //Parameterized ctor cout<<“I am parameterized ctor of Point class”<<endl; } };
  • 6. Constructor Overloading • Constructors can be overloaded – It means we can have multiple constructors in a class – Objects can be instantiated in different ways
  • 7. Constructor Example program Counter class Counter { private: unsigned int count; public: Counter() : count(0) { /*empty body*/ } void inc_count() //increment count { count++; } int get_count() //return count { return count; } }; int main() { Counter c1, c2; //define and initialize cout << “nc1=” << c1.get_count(); cout << “nc2=” << c2.get_count(); c1.inc_count(); //increment c1 c2.inc_count(); //increment c2 c2.inc_count(); //increment c2 cout << “nc1=” << c1.get_count(); cout << “nc2=” << c2.get_count(); } Output c1=0 c2=0 c1=1 c2=2
  • 8. Destructors • A special member function • Called automatically when object of that class is destroyed. • Has the same name as the class it belongs to followed by a ~ sign • No return type is used for destructors • Destructors can not be parameterized • Destructors can not be overloaded • Purpose of Destructor – Perform task needed at the time of object killing like • Release resources acquired by an object • De-allocation of run time memory
  • 9. Destructors • An example class Counter{ private: int count; public: Counter() : count(0){ cout<<“I am ctor of Counter class”<<endl; } ~Counter(){ //Destructor of Counter class cout<<“I am dtor of Counter class”<<endl; } }; void main(){ cout<<“Start of main”<<endl; Counter a; { cout<<“Inside the block”<<endl; Counter b; cout<<“Exiting block”<<endl; } cout<<“Exiting main”<<endl; } Output Start of main I am ctor of Counter class Inside the block I am ctor of Counter class Exiting block I am dtor of Counter class Exiting main I am dtor of Counter class