ConstructorDestructor Functions Presentation By LAKHAN LAL BAGWAN PGT
Constructor/Destructor Functions Presentation By : LAKHAN LAL BAGWAN PGT Comp. Sc. KV NDA PUNE-23 1
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++ Default Constructor The default constructor takes no arguments. If no such constructor is defined, then compiler supplies a default constructor. For Example Xx; 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.
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; } n number ob 1; // ***** WRONG OBJECT CREATION number ob 1( 0, 100 ); // ***** * Implicit Call n Known as shorthand method n Easy to implement and looks better number ob 1 = 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 ob 1(10); number ob 2(0 , 0 );
Example of Constructor class sum { public: sum(); private: int sum 1, sum 2; };
C++ void main () { sum obj 1; //constructor is called at this //time cout<<“end of main”<<endl; return; }
C++ Sample Functions What is the output? ?
C++ Functions sum: : sum () { sum 1=0; sum 2=10; cout<<sum 1<<“ “<<sum 2<<endl; }
the answer 0 10 end of main
Example of Constructor with. Arguments class sum { public: sum(int, int); private: int sum 1, sum 2; };
C++ void main () { sum obj 1 (10, 20); //constructor is // called at this time cout<<“end of mainn”; return; }
C++ Functions sum: : sum (int x, int y)) { sum 1=x; sum 2=y; cout<<sum 1<<“ sum 1”; cout<<sum 2<<“ sum 2”<<endl; }
C++ Sample Functions What is the output? ? sum: : sum (int x, int y)) { sum 1=x; sum 2=y; cout<<sum 1<<“ sum 1”; cout<<sum 2<<“ sum 2”<<endl; } 10 sum 1 20 sum 2 end main
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)
C++ DESTRUCTOR FUNCTION public: ~sum (); // DESTRUCTOR FUNCTION sum: : ~sum ( ) { close (infile); close (outfile); }
- Slides: 20