SlideShare a Scribd company logo
Constructor/Destructor Functions
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

PPTX
Functions in c++
Rokonuzzaman Rony
 
PDF
Constructor and Destructor
Kamal Acharya
 
PDF
Constructors and Destructors
Dr Sukhpal Singh Gill
 
PPTX
classes and objects in C++
HalaiHansaika
 
PPTX
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
PPTX
Constructor in java
Madishetty Prathibha
 
PPT
operators and expressions in c++
sanya6900
 
PPT
Operator precedence and associativity
Dr.Sandhiya Ravi
 
Functions in c++
Rokonuzzaman Rony
 
Constructor and Destructor
Kamal Acharya
 
Constructors and Destructors
Dr Sukhpal Singh Gill
 
classes and objects in C++
HalaiHansaika
 
[OOP - Lec 19] Static Member Functions
Muhammad Hammad Waseem
 
Constructor in java
Madishetty Prathibha
 
operators and expressions in c++
sanya6900
 
Operator precedence and associativity
Dr.Sandhiya Ravi
 

What's hot (20)

PPTX
virtual function
VENNILAV6
 
PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
PPSX
Inheritance
Selvin Josy Bai Somu
 
PPTX
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
PPTX
Constructors and Destructor in C++
International Institute of Information Technology (I²IT)
 
PPTX
Exception handling in c++
imran khan
 
PPTX
Java(Polymorphism)
harsh kothari
 
PPTX
Inheritance in c++
Vineeta Garg
 
PDF
C++ Files and Streams
Ahmed Farag
 
PDF
Operators in python
Prabhakaran V M
 
PPTX
Exception handling c++
Jayant Dalvi
 
PPTX
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
PPT
Exception Handling in JAVA
SURIT DATTA
 
PDF
Constructors and destructors
Prof. Dr. K. Adisesha
 
PPTX
Function in C program
Nurul Zakiah Zamri Tan
 
PPT
FUNCTIONS IN c++ PPT
03062679929
 
PPTX
Constructor ppt
Vinod Kumar
 
PDF
Function overloading ppt
Prof. Dr. K. Adisesha
 
PPTX
Inline function
Tech_MX
 
PPT
Control Structures
Ghaffar Khan
 
virtual function
VENNILAV6
 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
 
sSCOPE RESOLUTION OPERATOR.pptx
Nidhi Mehra
 
Exception handling in c++
imran khan
 
Java(Polymorphism)
harsh kothari
 
Inheritance in c++
Vineeta Garg
 
C++ Files and Streams
Ahmed Farag
 
Operators in python
Prabhakaran V M
 
Exception handling c++
Jayant Dalvi
 
[OOP - Lec 16,17] Objects as Function Parameter and ReturnType
Muhammad Hammad Waseem
 
Exception Handling in JAVA
SURIT DATTA
 
Constructors and destructors
Prof. Dr. K. Adisesha
 
Function in C program
Nurul Zakiah Zamri Tan
 
FUNCTIONS IN c++ PPT
03062679929
 
Constructor ppt
Vinod Kumar
 
Function overloading ppt
Prof. Dr. K. Adisesha
 
Inline function
Tech_MX
 
Control Structures
Ghaffar Khan
 
Ad

Viewers also liked (12)

PPTX
Constructors & destructors
ForwardBlog Enewzletter
 
PPTX
Constructor and destructor in c++
Learn By Watch
 
PPTX
Default constructors in C++
Learn By Watch
 
PPTX
Inline function in C++
Learn By Watch
 
PPTX
Friend function & friend class
Abhishek Wadhwa
 
PPT
friend function(c++)
Ritika Sharma
 
PPT
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
PPT
Basic concepts of object oriented programming
Sachin Sharma
 
PPT
Oops ppt
abhayjuneja
 
PPT
Object Oriented Programming Concepts
thinkphp
 
PPT
constructor and destructor-object oriented programming
Ashita Agrawal
 
PPT
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Constructors & destructors
ForwardBlog Enewzletter
 
Constructor and destructor in c++
Learn By Watch
 
Default constructors in C++
Learn By Watch
 
Inline function in C++
Learn By Watch
 
Friend function & friend class
Abhishek Wadhwa
 
friend function(c++)
Ritika Sharma
 
Object oriented programming (oop) cs304 power point slides lecture 01
Adil Kakakhel
 
Basic concepts of object oriented programming
Sachin Sharma
 
Oops ppt
abhayjuneja
 
Object Oriented Programming Concepts
thinkphp
 
constructor and destructor-object oriented programming
Ashita Agrawal
 
C++: Constructor, Copy Constructor and Assignment operator
Jussi Pohjolainen
 
Ad

Similar to Constructor (20)

PPT
Tutconstructordes
Niti Arora
 
PPT
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
PPTX
An introduction to Constructors and destructors in c++ .pptx
olisahchristopher
 
PPT
Friend this-new&delete
Shehzad Rizwan
 
PPTX
Constructor and Destructor
Sunipa Bera
 
PPT
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
PPTX
Constructors in C++.pptx
Rassjb
 
PPTX
Constructor in c++
Jay Patel
 
PPTX
Constructor & destructor
Saharsh Anand
 
PPTX
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
PPTX
Constructor and desturctor
Somnath Kulkarni
 
PDF
Constructor and Destructor.pdf
MadnessKnight
 
PPTX
Constructors and Destructors
Keyur Vadodariya
 
PDF
Constructors or destructors unit(II).pdf
malviyatanishk8
 
PPT
3. constructors and destructor
jashobhan pradhan
 
DOCX
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
rajalakshmisf687
 
PPTX
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
PPTX
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
PPTX
constructor & destructor in cpp
gourav kottawar
 
Tutconstructordes
Niti Arora
 
Oop Constructor Destructors Constructor Overloading lecture 2
Abbas Ajmal
 
An introduction to Constructors and destructors in c++ .pptx
olisahchristopher
 
Friend this-new&delete
Shehzad Rizwan
 
Constructor and Destructor
Sunipa Bera
 
ConsTRUCTION AND DESTRUCTION
Shweta Shah
 
Constructors in C++.pptx
Rassjb
 
Constructor in c++
Jay Patel
 
Constructor & destructor
Saharsh Anand
 
[OOP - Lec 13,14,15] Constructors / Destructor and its Types
Muhammad Hammad Waseem
 
Constructor and desturctor
Somnath Kulkarni
 
Constructor and Destructor.pdf
MadnessKnight
 
Constructors and Destructors
Keyur Vadodariya
 
Constructors or destructors unit(II).pdf
malviyatanishk8
 
3. constructors and destructor
jashobhan pradhan
 
Constructor-Types of Constructor:default,parameterized,copy constructor-Destr...
rajalakshmisf687
 
C++ Unit-III Lecture-3a-C++ Programming Concepts
dharawagh9999
 
constructorsfjy5ediykEASFul;IUWORHusi;gfb.pptx
AshrithaRokkam
 
constructor & destructor in cpp
gourav kottawar
 

More from poonamchopra7975 (13)

PPT
Positive attitude2551
poonamchopra7975
 
PPT
Positive attitude
poonamchopra7975
 
DOCX
Arrays
poonamchopra7975
 
PDF
Notes netbeans
poonamchopra7975
 
PPT
Overloading
poonamchopra7975
 
PPTX
Computer parts
poonamchopra7975
 
PPTX
Computer networking
poonamchopra7975
 
PDF
Sample Paper I.P
poonamchopra7975
 
PDF
CBSE Sample Paper IP
poonamchopra7975
 
DOCX
Sample paper i.p
poonamchopra7975
 
DOCX
Sample paper
poonamchopra7975
 
Positive attitude2551
poonamchopra7975
 
Positive attitude
poonamchopra7975
 
Notes netbeans
poonamchopra7975
 
Overloading
poonamchopra7975
 
Computer parts
poonamchopra7975
 
Computer networking
poonamchopra7975
 
Sample Paper I.P
poonamchopra7975
 
CBSE Sample Paper IP
poonamchopra7975
 
Sample paper i.p
poonamchopra7975
 
Sample paper
poonamchopra7975
 

Constructor

  • 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