SlideShare a Scribd company logo
Constructor/Destructor Functions Presentation By : Ms. Nita Arora PGT Comp. Sc.  Kulachi Hansraj Model School
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 number {   int a, b ;   public:   number( void); //  ** Constructor Function Declared   -----   ----- }; number :: number( )  //  ** 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 x ; The default constructor for class X is X::X( ) The statement X x ; invokes the default constructor of the compiler to create the  object  x. Default Constructor
C++ Parameterized Constructor class number {   int a, b ;   public:   number( int x, int y); // Constructor Func. Declared   ----- }; number :: number(int x, int y)  //  Constructor Func Defined {  a=x;  b=y;  }  number ob1; // *****  WRONG OBJECT CREATION number  ob1( 0,100 );  // ********** * Implicit Call Known as shorthand method  Easy to implement and looks better number  ob1 = number( 0, 100 );  // **  Explicit Call
C++ : Constructor Overloading class number {   int a, b ;   public:   number( ){a=0;  b=0;  } // Constructor 1   number( int x, int y);  // Constructor 2   ----- }; number :: number(int x, int y)  //  Constructor 2  Defined {  a=x;  b=y;  }
C++ : Constructor with Default Arguments class number {   int a, b ;   public:   number( int x, int y=5 );  // Constructor    ----- }; number :: number(int x, int y)  //  Constructor Defined {  a=x;  b=y;  }  number ob1(10); number ob2(0 , 0 );
Example of Constructor class sum { public: sum(); private: int sum1,sum2; };
C++ void main () { sum obj1;  //constructor is called at this //time cout<<“end of main”<<endl; return; }
C++ Sample Functions What is the output??
C++ Functions sum::sum () { sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl; }
the answer 0 10 end of main
Example of Constructor withArguments class sum { public: sum(int,int); private: int sum1,sum2; };
C++ void main () { sum obj1 (10,20); //constructor is // called at this time cout<<“end of main\n”; return; }
C++ Functions sum::sum (int x,int y)) { sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“ sum2”<<endl; }
C++ Sample Functions sum::sum (int x,int y)) { sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“ sum2”<<endl; } 10 sum1 20 sum2 end main What is the output??
C++ : DESTRUCTOR FUNCTION A special  function which also has same name as that of class but is preceded  with a tilde (~) sign  eg., ~ number( ); 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: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { close (infile); close (outfile); } C++  DESTRUCTOR FUNCTION

More Related Content

PDF
Constructors and Destructors
PDF
Constructor and Destructor
PPTX
Constructor and desturctor
PPTX
Constructor and destructor
PPT
Oop lec 5-(class objects, constructor & destructor)
PPTX
Types of Constructor in C++
PPSX
Constructor and destructor
PPT
Constructor
Constructors and Destructors
Constructor and Destructor
Constructor and desturctor
Constructor and destructor
Oop lec 5-(class objects, constructor & destructor)
Types of Constructor in C++
Constructor and destructor
Constructor

What's hot (20)

PPTX
C++ Constructor destructor
PPTX
Constructor and Types of Constructors
PPT
Constructor and Destructor PPT
PPTX
constructor with default arguments and dynamic initialization of objects
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
PPT
Constructor & Destructor
PPTX
Constructor and Destructor in c++
PPT
Tutconstructordes
PPTX
Constructor & destructor
PPT
C++: Constructor, Copy Constructor and Assignment operator
PPTX
constructor and destructor
PPTX
Constructors and Destructors
PPTX
Constructor ppt
PPTX
Constructors and destructors
PPTX
Constructors & destructors
PDF
04. constructor & destructor
PPTX
constructor & destructor in cpp
PPTX
constructors in java ppt
PPTX
constructors and destructors
PDF
Constructors destructors
C++ Constructor destructor
Constructor and Types of Constructors
Constructor and Destructor PPT
constructor with default arguments and dynamic initialization of objects
Oop Constructor Destructors Constructor Overloading lecture 2
Constructor & Destructor
Constructor and Destructor in c++
Tutconstructordes
Constructor & destructor
C++: Constructor, Copy Constructor and Assignment operator
constructor and destructor
Constructors and Destructors
Constructor ppt
Constructors and destructors
Constructors & destructors
04. constructor & destructor
constructor & destructor in cpp
constructors in java ppt
constructors and destructors
Constructors destructors
Ad

Viewers also liked (20)

PPTX
Java Static Factory Methods
PPT
PPTX
Programming maintenance - Programming methodology
PPT
A message of stephen covey
PDF
Programming methodology lecture01
PDF
Programming methodology lecture26
PPTX
Builder pattern vs constructor
PPT
Ad

Similar to Tut Constructor (20)

PDF
chapter-9-constructors.pdf
PPTX
Constructor and Destructors in C++
PPTX
Constructors and Destructors in C++.pptx
PPTX
constructors shailee.pptxhhhtyygdxixixxxxix
PPTX
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
PPT
Constructor,destructors cpp
PPTX
constructor in object oriented program.pptx
PPTX
Constructors in C++.pptx
PDF
classes and objects.pdfggggggggffffffffgggf
PPTX
21CSC101T best ppt ever OODP UNIT-2.pptx
PDF
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
PDF
Constructors & Destructors [Compatibility Mode].pdf
PPT
Constructors and destructors in C++
PDF
Constructor and Destructor.pdf
PPT
Constructor and destructor in C++
PPT
Constructors and destructors in C++ part 2
PPTX
PDF
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
PPT
Constructors.16
DOCX
Virtual function
chapter-9-constructors.pdf
Constructor and Destructors in C++
Constructors and Destructors in C++.pptx
constructors shailee.pptxhhhtyygdxixixxxxix
CONSTRUCTORS, DESTRUCTORS AND OPERATOR OVERLOADING.pptx
Constructor,destructors cpp
constructor in object oriented program.pptx
Constructors in C++.pptx
classes and objects.pdfggggggggffffffffgggf
21CSC101T best ppt ever OODP UNIT-2.pptx
Const-Dest-PPT-5_removedhbfhfhfhdhfdhd.pdf
Constructors & Destructors [Compatibility Mode].pdf
Constructors and destructors in C++
Constructor and Destructor.pdf
Constructor and destructor in C++
Constructors and destructors in C++ part 2
22 scheme OOPs with C++ BCS306B_module2.pdfmodule2.pdf
Constructors.16
Virtual function

More from Kulachi Hansraj Model School Ashok Vihar (7)

Recently uploaded (20)

PPTX
MYSQL Presentation for SQL database connectivity
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
MIND Revenue Release Quarter 2 2025 Press Release
MYSQL Presentation for SQL database connectivity
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Review of recent advances in non-invasive hemoglobin estimation
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Mobile App Security Testing_ A Comprehensive Guide.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Understanding_Digital_Forensics_Presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Encapsulation_ Review paper, used for researhc scholars
MIND Revenue Release Quarter 2 2025 Press Release

Tut Constructor

  • 1. Constructor/Destructor Functions Presentation By : Ms. Nita Arora PGT Comp. Sc. Kulachi Hansraj Model School
  • 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 number { int a, b ; public: number( void); // ** Constructor Function Declared ----- ----- }; number :: number( ) // ** 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 x ; The default constructor for class X is X::X( ) The statement X x ; invokes the default constructor of the compiler to create the object x. Default Constructor
  • 7. C++ Parameterized Constructor class number { int a, b ; public: number( int x, int y); // Constructor Func. Declared ----- }; number :: number(int x, int y) // Constructor Func Defined { a=x; b=y; } number ob1; // ***** WRONG OBJECT CREATION number ob1( 0,100 ); // ********** * Implicit Call Known as shorthand method Easy to implement and looks better number ob1 = number( 0, 100 ); // ** Explicit Call
  • 8. C++ : Constructor Overloading class number { int a, b ; public: number( ){a=0; b=0; } // Constructor 1 number( int x, int y); // Constructor 2 ----- }; number :: number(int x, int y) // Constructor 2 Defined { a=x; b=y; }
  • 9. C++ : Constructor with Default Arguments class number { int a, b ; public: number( int x, int y=5 ); // Constructor ----- }; number :: number(int x, int y) // Constructor Defined { a=x; b=y; } number ob1(10); number ob2(0 , 0 );
  • 10. Example of Constructor class sum { public: sum(); private: int sum1,sum2; };
  • 11. C++ void main () { sum obj1; //constructor is called at this //time cout<<“end of main”<<endl; return; }
  • 12. C++ Sample Functions What is the output??
  • 13. C++ Functions sum::sum () { sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl; }
  • 14. the answer 0 10 end of main
  • 15. Example of Constructor withArguments class sum { public: sum(int,int); private: int sum1,sum2; };
  • 16. C++ void main () { sum obj1 (10,20); //constructor is // called at this time cout<<“end of main\n”; return; }
  • 17. C++ Functions sum::sum (int x,int y)) { sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“ sum2”<<endl; }
  • 18. C++ Sample Functions sum::sum (int x,int y)) { sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“ sum2”<<endl; } 10 sum1 20 sum2 end main What is the output??
  • 19. C++ : DESTRUCTOR FUNCTION A special function which also has same name as that of class but is preceded with a tilde (~) sign eg., ~ number( ); 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)
  • 20. public: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { close (infile); close (outfile); } C++ DESTRUCTOR FUNCTION