330 likes | 789 Views
Constructor & Destructor. By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot. We are going to learn:. What is a CONSTRUCTOR? Characteristics of Constructor Constructor: In Public & In Private Default Constructor Parameterized Constructor Constructor Overloading
E N D
Constructor & Destructor By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot.
We are going to learn: • What is a CONSTRUCTOR? • Characteristics of Constructor • Constructor: In Public & In Private • Default Constructor • Parameterized Constructor • Constructor Overloading • Constructor with Default Arguments • Copy Constructor • Member Initialization List (MIL) • Order of Constructor Invocation • Implementing Constructors • Destructor Function
What is a CONSTRUCTOR? • “Constructor is a ‘special’ member function used for initialization of objects (data members).” • “A constructor is a special member function that is a member of a class and has same name as that class.” • It is used to initialize the object of the class type with a legal initial value. • A constructor is called whenever an object of its associated class is created.
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; cout<<“Hello Students! See Object is created”; }
Declaration and Definition • It can be defined either inside the class definition or outside the class definition class X { int i ; public: int j,k ; X() { i = j = k =0;} }; X::X() { I = j = k =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. • Constructors can not be virtual. • They can not be inherited.
Constructor: In Public & In Private • Generally a constructor should be defined under the public section of a class, so that its object can be created in any function
class X { int i ; X() { i = j = k =0;} public: int j,k ; void check(void); // member function }; void X :: check (void) { X obj1; //valid } int main() { X obj2; //invalid }
Default Constructor A constructor that accept no parameter is called the default constructor. If no such constructor is defined, then compiler supplies a default constructor. For Example X obj1 ; The default constructor for class X is X::X( ) The statement X obj1 ; 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; } • 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; }
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 );
class A { inti,j; public: X(int x=10,int y=20); }; A::X(intx,int y) { i=x; j = y; } int main() { A obj1; A obj2(250); A obj3(2,4); getch(); return 0; } obj1 obj2 obj3 i=250 j=20 i=10 j=20 i=2 j=4
Explicit Constructor • Automatic conversion adds to readability. • But there may be times when you do not want this automatic conversion to take place. For this purpose, C++ defines the keyword explicit.
Dynamic Initialization • In earlier cases, the object is initialized with constants. The values of constants are available at compiler time. i.e., the object initialization is done at compile time. • We can use constructors to initialize objects at run time. The values to be passed to the constructors are made available at runtime and then the constructor is invoked while defining the object. We call this dynamic initialization.
Copy Constructor • A constructor that accepts a reference of its own class is called a copy constructor. • It is used to declare and initialize an object from another objects of same class. • The process of initialization with the help of a copy constructor is known as copy initialization.
Copy Constructor class sample { int i, j; public: sample ( int a, int b) { i = a; j = b; } sample ( sample & s) { i = s.i ; j = s.j ; } void print(void) { cout<<i <<“ “<<j <<“\n”; } Above constructors may be used as follows sample s1 (10,12); //1st const. called sample s2 (s1) // copy const. called sample s3 = s1; copy const. called
Member Initialization List (MIL) • MIL is the method for initialization of the members of a class using the constructor function. • It provide the alternative for providing initializations outside the constructor body. • Its Need : • Readability • Efficiency
Example of Constructor with Arguments class sum { private: int sum1,sum2; public: sum(int,int); print() {cout<<sum1<<“ “<<sum2<<endl;} }; void main () { sum obj1 (10,20); //constructor is called at this time obj1.print(); return; }
Example of Constructor with Arguments sum::sum (int x, int y) { sum1=9; sum2= 10; } What is the output??
the answer 9 10 (not 10 20)
Order of Constructor Invocation • The objects are constructed in the order they are defined. Consider the following statement • Sample s1,s2,s3; // the order of construction is s1,s2,s3. • But when a class containing objects of another class as its members. In such a case, the constructor for member objects are invoked first and then only, the constructor for containing class is invoked. • For example
class C { private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }}; int main() { clrscr(); C objC; getch(); } class A { public: A() { cout<<“Constructing A” <<endl; }}; class B { public: B() { cout<<“Constructing B” <<endl; }};
C++ : DESTRUCTOR FUNCTION • A class can have another special member function called destructor, which is invoked when an object is destroyed. • This function is reverse of the constructors. • 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) • A class cannot have more than one destructor.
C++ DESTRUCTOR FUNCTION public: ~sum (); // DESTRUCTOR FUNCTION sum::~sum ( ) { //close (infile); //close (outfile); Cout<<bye; }