Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.



Published on

Published in: Education, Technology


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