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

Tutconstructordes

  • 1.
  • 2.
    OBJECTIVES:After this presentationwe 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 aCONSTRUCTOR 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 isdeclared 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 ConstructorsConstructor 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 defaultconstructor 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 Constructorclass 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++ : Constructorwith 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++ : ConstructorOverloading 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 Constructorclass 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++ : DESTRUCTORFUNCTION 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 destructorFunction? 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 destructorclass 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