constructor
DESCRIPTION
TRANSCRIPT
1
Constructor/DestructorFunctions
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
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 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.
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
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; }
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 );
Example of Constructor
class sum{public: sum();private: int sum1,sum2;};
C++
void main (){sum obj1; //constructor is called at
this //timecout<<“end of main”<<endl;return;}
C++ Sample Functions
What is the output??
C++ Functions
sum::sum (){ sum1=0; sum2=10; cout<<sum1<<“ “<<sum2<<endl;}
the answer
0 10
end of main
Example of Constructor withArguments
class sum{public: sum(int,int);private: int sum1,sum2;};
C++
void main (){sum obj1 (10,20);
//constructor is // called at this timecout<<“end of main\n”;return;}
C++ Functions
sum::sum (int x,int y)){ sum1=x; sum2=y; cout<<sum1<<“ sum1”; cout<<sum2<<“
sum2”<<endl;}
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;}
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)
public:
~sum (); // DESTRUCTOR FUNCTION
sum::~sum ( )
{
close (infile);
close (outfile);
}
C++ DESTRUCTOR FUNCTION