constructor

20
1 Constructor/ Destructor Functions

Upload: poonamchopra7975

Post on 12-Jan-2015

2.273 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Constructor

1

Constructor/DestructorFunctions

Page 2: Constructor

OBJECTIVES:After this presentation we shall be able to answer following questions:

What is a CONSTRUCTOR Function?Characteristics of ConstructorDefault ConstructorParameterized ConstructorConstructor OverloadingConstructor with Default ArgumentsCopy ConstructorGuidelines for implementing ConstructorsDestructor Function

Page 3: Constructor

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.

Page 4: Constructor

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

Page 5: Constructor

Characteristics of Constructors

Constructor Functions have same name as that of class name.

They do not have return types, not even voidMay have parametersC++ 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.

Page 6: Constructor

C++

The default constructor takes no arguments .If no such constructor is defined, then compiler supplies a default constructor.For Example

X x ; 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.

Default Constructor

Page 7: Constructor

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

Page 8: Constructor

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 9: Constructor

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 ob1(10);number ob2(0 , 0 );

Page 10: Constructor

Example of Constructor

class sum{public: sum();private: int sum1,sum2;};

Page 11: Constructor

C++

void main (){sum obj1; //constructor is called at

this //timecout<<“end of main”<<endl;return;}

Page 12: Constructor

C++ Sample Functions

What is the output??

Page 13: Constructor

C++ Functions

sum::sum (){ sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl;}

Page 14: Constructor

the answer

0 10

end of main

Page 15: Constructor

Example of Constructor withArguments

class sum{public: sum(int,int);private: int sum1,sum2;};

Page 16: Constructor

C++

void main (){sum obj1 (10,20);

//constructor is // called at this timecout<<“end of main\n”;return;}

Page 17: Constructor

C++ Functions

sum::sum (int x,int y)){ sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“

sum2”<<endl;}

Page 18: Constructor

C++ Sample Functions10 sum120 sum2

end mainWhat is the output??sum::sum (int x,int y)){ sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“

sum2”<<endl;}

Page 19: Constructor

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 parametersCalled automatically when the class object is destroyedDe-allocates storage allocated to a classShould be public (or protected)

Page 20: Constructor

public:

~sum (); // DESTRUCTOR FUNCTION

sum::~sum ( )

{

close (infile);

close (outfile);

}

C++ DESTRUCTOR FUNCTION