SlideShare a Scribd company logo
Constructor/Destructor Functions Presentation By : Ms. Niti Arora
OBJECTIVES:After this presentation we shall be able to answer following questions:  What is a CONSTRUCTOR Function? Characteristics of Constructor Default Constructor Parameterized Constructor Constructor Overloading Constructor with Default Arguments Copy Constructor Guidelines for implementing Constructors Destructor Function
What is a CONSTRUCTOR Function? Special member function used for initialization of objects (data members). A constructor function is called whenever an object is created. Constructors are also called when an object is created as a part of another object.
A Constructor is declared and defined as follows: class X {   int a, b ;   public:   X( void); //  ** Constructor Function Declared   -----   ----- }; X :: X( )  //  ** Constructor Function Defined {   a=0;   b=0; }
Characteristics of Constructors Constructor Functions have same name as that of class name. They do not have return types, not even  void May have parameters C++ has default constructors that are called whenever a class is declared even if no function with the same name exists They should be declared in public section. They are invoked automatically when objects are created.
C++ The default constructor takes no arguments . If no such constructor is defined, then compiler supplies a default constructor. For Example  X X1; The default constructor for class X is X::X( ) The statement X X1 ; invokes the default constructor of the compiler to create the  class object; Default Constructor
C++ Parameterized Constructor class X {   int a, b ;   public:   X( int x, int y); // Constructor Function Declared   ----- }; X :: X(int x, int y)  //  Constructor Function Defined {  a=x;  b=y;  }  X ob1; // *****  WRONG OBJECT CREATION X  ob1( 0,100 );  // ********** * Implicit Call Known as shorthand method  Easy to implement and looks better X  ob1 = X( 0, 100 );  // **  Explicit Call Create a temporary Object And execute copy constructor Create object and  copy contents of temporary object
C++ : Constructor with Default Arguments class X {   int a, b ;   public:   X( int x, int y=5 );  // Constructor    ----- }; X :: X(int x, int y)  //  Constructor Defined {  a=x;  b=y;  }  X ob1(10); X ob2(0 , 0 ); A Parameterized constructor can also have default paramter. Calling with one value Calling with both values
C++ : Constructor Overloading class X {   int a, b ;   public:   X( ){a=0;  b=0;  } // Default Constructor   X( int x, int y)  // Parameterized Constructor   { a=x; b=y;   }   ----- }; Two or more constructors with the same name but different in their signature or arguments.
Example of Constructor class Fact { private: int F; public: Fact() { F=1; } Fact(int n); void Cal(int x); void display() { cout<<“factorial= “<<F; } }; Default constructor to initialize F with 1 Parameterized function to calculate factorial Fact :: Fact(int n) { for(int i=1;i<=n;i++) { F*=I; } } void Fact :: Cal(int n) { for(int i=1;i<=n;i++) { F*=I; } }
C++ void main () { int n; cout<<“Enter Number”; cin >> n; Fact F1(n); F1.display(); } void main () { int n; Fact F1 cout<<“Enter Number”; cin >> n; F1.Cal(n); F1.display(); } Parameterized constructor is executed Default constructor is executed
C++ : DESTRUCTOR FUNCTION A special  function which also has same name as that of class but is preceded  with a tilde (~) sign  eg., ~ X( ); Does not have a return type (not even void) Cannot have parameters Called automatically when the class object is destroyed De-allocates  storage allocated to a class Should be public (or protected)
public: ~X (); // DESTRUCTOR FUNCTION X::~X ( ) { cout<<“Over……………….”; } C++  DESTRUCTOR FUNCTION
Why A destructor Function? Used to execute when object goes out of scope so good bye messages can be displayed. Releasing dynamic memory It used to display messages when object goes out of scope
Example of destructor class X { int x,y; public: X() { cout<<“\n Constructor”; } ~X() { cout<<“\n Destructor”; } void get() { cin>>x>>y; } void disp() { cout<<x<<y<<endl; } }; Constructor executed when object is created. Destructor executed when object goes out of scope
void main() { X X1; } Output Constructor Destructor As soon as main gets over destructor will be executed

More Related Content

PPT
Constructor
PPTX
Constructors & destructors
PPSX
Constructor and destructor
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPTX
constructors and destructors in c++
PPT
Constructor and destructor in C++
PDF
Constructor and Destructor
PPTX
Types of Constructor in C++
Constructor
Constructors & destructors
Constructor and destructor
C++: Constructor, Copy Constructor and Assignment operator
constructors and destructors in c++
Constructor and destructor in C++
Constructor and Destructor
Types of Constructor in C++

What's hot (19)

PPTX
Constructor in c++
PPT
Bca 2nd sem u-2 classes & objects
PPTX
C++ Constructor destructor
PDF
Constructors and Destructors
PDF
04. constructor & destructor
PPTX
Constructors and destructors
PPTX
Constructor and destructor
PPTX
Constructor ppt
PPTX
Constructor and Destructor in c++
PPT
Constructor & Destructor
PPTX
Constructor and Types of Constructors
PPTX
Constructors and Destructors
PPT
Constructor and Destructor PPT
PDF
Constructors destructors
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPT
Constructors and destructors in C++
PPTX
classes & objects in cpp overview
PPTX
constructor & destructor in cpp
Constructor in c++
Bca 2nd sem u-2 classes & objects
C++ Constructor destructor
Constructors and Destructors
04. constructor & destructor
Constructors and destructors
Constructor and destructor
Constructor ppt
Constructor and Destructor in c++
Constructor & Destructor
Constructor and Types of Constructors
Constructors and Destructors
Constructor and Destructor PPT
Constructors destructors
Oop Constructor Destructors Constructor Overloading lecture 2
Constructors and destructors in C++
classes & objects in cpp overview
constructor & destructor in cpp
Ad

Similar to Tutconstructordes (20)

PDF
Constructors and destructors
PPTX
Constructor & destructor
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
PPT
3. constructors and destructor
PDF
Constructors or destructors unit(II).pdf
PPTX
Constructor and Destructor
PPTX
Constructors in C++.pptx
PDF
Constructor and Destructor.pdf
PPTX
An introduction to Constructors and destructors in c++ .pptx
PPT
class constructors and destructurs in c++.ppt
DOCX
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
PDF
chapter-9-constructors.pdf
PPT
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
PPT
ConsTRUCTION AND DESTRUCTION
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PPT
Constructors and destructors in C++ part 2
PPTX
Constructor and desturctor
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPTX
constructors and destructors
PPTX
Constructors and destructors
Constructor & destructor
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
3. constructors and destructor
Constructors or destructors unit(II).pdf
Constructor and Destructor
Constructors in C++.pptx
Constructor and Destructor.pdf
An introduction to Constructors and destructors in c++ .pptx
class constructors and destructurs in c++.ppt
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
chapter-9-constructors.pdf
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
ConsTRUCTION AND DESTRUCTION
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
Constructors and destructors in C++ part 2
Constructor and desturctor
Constructors & Destructors [Compatibility Mode].pdf
constructors and destructors
Ad

More from Niti Arora (6)

PPTX
Cyber Safety
PPT
Computer xi
PPTX
Word families
PPT
Generation of computer
PPT
Generation of computer
PPT
Introduction to Computer
Cyber Safety
Computer xi
Word families
Generation of computer
Generation of computer
Introduction to Computer

Recently uploaded (20)

PPTX
GDM (1) (1).pptx small presentation for students
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Lesson notes of climatology university.
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Computing-Curriculum for Schools in Ghana
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Institutional Correction lecture only . . .
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PDF
RMMM.pdf make it easy to upload and study
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GDM (1) (1).pptx small presentation for students
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Lesson notes of climatology university.
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Abdominal Access Techniques with Prof. Dr. R K Mishra
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Microbial disease of the cardiovascular and lymphatic systems
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Module 4: Burden of Disease Tutorial Slides S2 2025
Computing-Curriculum for Schools in Ghana
Complications of Minimal Access Surgery at WLH
Institutional Correction lecture only . . .
O5-L3 Freight Transport Ops (International) V1.pdf
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
RMMM.pdf make it easy to upload and study
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf

Tutconstructordes

  • 2. OBJECTIVES:After this presentation we shall be able to answer following questions: What is a CONSTRUCTOR Function? Characteristics of Constructor Default Constructor Parameterized Constructor Constructor Overloading Constructor with Default Arguments Copy Constructor Guidelines for implementing Constructors Destructor Function
  • 3. What is a CONSTRUCTOR Function? Special member function used for initialization of objects (data members). A constructor function is called whenever an object is created. Constructors are also called when an object is created as a part of another object.
  • 4. A Constructor is declared and defined as follows: class X { int a, b ; public: X( void); // ** Constructor Function Declared ----- ----- }; X :: X( ) // ** Constructor Function Defined { a=0; b=0; }
  • 5. Characteristics of Constructors Constructor Functions have same name as that of class name. They do not have return types, not even void May have parameters C++ has default constructors that are called whenever a class is declared even if no function with the same name exists They should be declared in public section. They are invoked automatically when objects are created.
  • 6. C++ The default constructor takes no arguments . If no such constructor is defined, then compiler supplies a default constructor. For Example X X1; The default constructor for class X is X::X( ) The statement X X1 ; invokes the default constructor of the compiler to create the class object; Default Constructor
  • 7. C++ Parameterized Constructor class X { int a, b ; public: X( int x, int y); // Constructor Function Declared ----- }; X :: X(int x, int y) // Constructor Function Defined { a=x; b=y; } X ob1; // ***** WRONG OBJECT CREATION X ob1( 0,100 ); // ********** * Implicit Call Known as shorthand method Easy to implement and looks better X ob1 = X( 0, 100 ); // ** Explicit Call Create a temporary Object And execute copy constructor Create object and copy contents of temporary object
  • 8. C++ : Constructor with Default Arguments class X { int a, b ; public: X( int x, int y=5 ); // Constructor ----- }; X :: X(int x, int y) // Constructor Defined { a=x; b=y; } X ob1(10); X ob2(0 , 0 ); A Parameterized constructor can also have default paramter. Calling with one value Calling with both values
  • 9. C++ : Constructor Overloading class X { int a, b ; public: X( ){a=0; b=0; } // Default Constructor X( int x, int y) // Parameterized Constructor { a=x; b=y; } ----- }; Two or more constructors with the same name but different in their signature or arguments.
  • 10. Example of Constructor class Fact { private: int F; public: Fact() { F=1; } Fact(int n); void Cal(int x); void display() { cout<<“factorial= “<<F; } }; Default constructor to initialize F with 1 Parameterized function to calculate factorial Fact :: Fact(int n) { for(int i=1;i<=n;i++) { F*=I; } } void Fact :: Cal(int n) { for(int i=1;i<=n;i++) { F*=I; } }
  • 11. C++ void main () { int n; cout<<“Enter Number”; cin >> n; Fact F1(n); F1.display(); } void main () { int n; Fact F1 cout<<“Enter Number”; cin >> n; F1.Cal(n); F1.display(); } Parameterized constructor is executed Default constructor is executed
  • 12. C++ : DESTRUCTOR FUNCTION A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ X( ); Does not have a return type (not even void) Cannot have parameters Called automatically when the class object is destroyed De-allocates storage allocated to a class Should be public (or protected)
  • 13. public: ~X (); // DESTRUCTOR FUNCTION X::~X ( ) { cout<<“Over……………….”; } C++ DESTRUCTOR FUNCTION
  • 14. Why A destructor Function? Used to execute when object goes out of scope so good bye messages can be displayed. Releasing dynamic memory It used to display messages when object goes out of scope
  • 15. Example of destructor class X { int x,y; public: X() { cout<<“\n Constructor”; } ~X() { cout<<“\n Destructor”; } void get() { cin>>x>>y; } void disp() { cout<<x<<y<<endl; } }; Constructor executed when object is created. Destructor executed when object goes out of scope
  • 16. void main() { X X1; } Output Constructor Destructor As soon as main gets over destructor will be executed