Constructor & Destructor

30
Constructor & Destructor 1 By: Vaishali V Kaneria Asst. Prof., Dept of MCA, AITS, Rajkot.

description

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 - PowerPoint PPT Presentation

Transcript of Constructor & Destructor

Page 1: Constructor & Destructor

Constructor & Destructor

1

By:Vaishali V Kaneria

Asst. Prof., Dept of MCA,AITS, Rajkot.

Page 2: Constructor & Destructor

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

Page 3: Constructor & Destructor

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.

Page 4: Constructor & Destructor

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”;}

Page 5: Constructor & Destructor

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;}

Declaration and Definition

Page 6: Constructor & Destructor

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.

Page 7: Constructor & Destructor

Generally a constructor should be defined under the public section of a class, so that its object can be created in any function

Constructor: In Public & In Private

Page 8: Constructor & Destructor

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}

Page 9: Constructor & Destructor

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.

Default Constructor

Page 10: Constructor & Destructor

C++ Parameterized Constructorclass 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

Page 11: Constructor & Destructor

C++ Parameterized Constructor

Page 12: Constructor & Destructor

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; }

Page 13: Constructor & Destructor

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 );

Page 14: Constructor & Destructor

class A { int i,j; public: X(int x=10,int

y=20);};A::X(int x,int y){ i=x; j = y;}

int main()

{

A obj1;

A obj2(250);

A obj3(2,4);

getch();

return 0;

}

i=10j=20

i=250j=20

i=2j=4

obj1 obj2 obj3

Page 15: Constructor & Destructor

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.

Page 16: Constructor & Destructor

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.

Dynamic Initialization

Page 17: Constructor & Destructor

Dynamic Initialization

Page 18: Constructor & Destructor

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.

Page 19: Constructor & Destructor

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

Copy Constructor

Page 20: Constructor & Destructor

Copy Constructor

Page 21: Constructor & Destructor

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

Page 22: Constructor & Destructor

Example of Constructor with Argumentsclass 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;

}

Page 23: Constructor & Destructor

sum::sum (int x, int y){ sum1=9; sum2= 10; }

What is the output??

Example of Constructor with Arguments

Page 24: Constructor & Destructor

the answer

9 10

(not 10 20)

Page 25: Constructor & Destructor

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

Page 26: Constructor & Destructor

class A{ public: A() { cout<<“Constructing

A” <<endl; }};class B { public: B() { cout<<“Constructing

B” <<endl;}};

class C{ private: A objA; B objB; public: C() { cout<<“Constructing C” <<endl; }};int main(){ clrscr(); C objC; getch();}

Page 27: Constructor & Destructor

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.

Page 28: Constructor & Destructor

public:

~sum (); // DESTRUCTOR FUNCTION

sum::~sum ( )

{

//close (infile);

//close (outfile);

Cout<<bye;

}

C++ DESTRUCTOR FUNCTION

Page 29: Constructor & Destructor

Who is Bjarne Stroustrup??

Page 30: Constructor & Destructor

Thank You