04 Constructor and Destructor 20-21
04 Constructor and Destructor 20-21
Properties/Characteristics/Features of Constructor:
1) Constructor name is same as that of class name.
2) Constructors are only declared under public section of class.
3) Constructor does not have any return type even void.
4) Constructors are implicitly invoked by the compiler whenever object of class is created.
5) Constructor cannot be virtual.
6) Constructor can be overloaded.
7) They make implicitly call to the new operator when memory allocation is required.
Types of constructor:
Depending upon arguments passed to constructor, it has three types.
1) Default Constructor
2) Parameterized constructor
3) Copy constructor
1) Default constructor:
The constructor that does not have any argument or parameter as input, such
constructor is called as “Default Constructor”.
Default constructor is implicitly invoked by compiler whenever object of class is crated
Syntax:
class class_name
{
public:
class_name( ); // default constructor.
};
1
Lalita bhosale B.Sc-II(Sem-III) 2020-21 lbs
2) Parameterized constructor:
• The constructor that accepts any argument or parameter as input, such constructor is
called as “parameterized Constructor”.
• Parameterized constructor is implicitly invoked by compiler when we pass some values
direct to object of class.
Syntax:
class class_name
{
public:
class_name(datatype1,datatype2); // parameterized constructor.
};
3) Copy constructor:
• Copy constructor is used to create the new object of class.
• In case of copy constructor the reference of object is passed as argument to existing
object, such that value or record hold by existing object is entirely copied into second
object.
• Parameterized constructor is implicitly invoked by compiler when we pass reference of
one object as an argument to another object of class.
2
Lalita bhosale B.Sc-II(Sem-III) 2020-21 lbs
Overloading of Constructor (Multiple Constructor in Class):
• We know that in case of function overloading, we can define or implement many
functions having same name but these functions are differ from each other according to
their signature (Type of argument and number of argument accept by function).
• Like function overloading, we are also able to overload the constructor.
• Constructor overloading means implementing or defining multiple constructor for single
class. But all these constructors are differing from each other according to their
signatures.
Implementation of Constructor overloading:
3
Lalita bhosale B.Sc-II(Sem-III) 2020-21 lbs
Destructor:
• The destructor is also special member function whose name is same as that of class
name but it precedes tiled (~) symbol.
• The main task of destructor is to destroy the object created by the constructor.
• Destructor destroys the object created by constructor means it releases or frees memory
space for future use.
• The destructors are implicitly invoked by the compiler while we exit from program.
Example:
Consider, there is one class having name “person” then its destructor is given as:
class person
{
public:
person( ); // constructor
~ person( ); // destructor
};
Properties/Characteristics/Features of Destructor:
1) Destructor name is same as that of class name but it precedes with tiled(~) sign
2) Destructor is only declared under public section of class.
3) Destructor does not have any return type even void.
4) Destructor does not accept any argument that’s why they are not overloaded.
5) Destructor is implicitly invoked by the compiler while we exit from program.
6) Destructor can be virtual.
7) Destructor cannot be overloaded.
Note that:
• The invocation i.e. execution of destructor is depends on number of objects for a class.
• Class having multiple constructors but it has only one destructor.
Implementation of Destructor:
#include<iostream.h>
#include<conio.h> void main( )
class one {
{ one x;
public: one y;
one( ); // constructor getch( );
~one( ); //destructor }
};
one::one( )
{
cout<<”\n Constructor is called ”; OUT-PUT:
} Constructor is called
one:: ~one( ) Constructor is called
{
cout<<”\n Destructor is called ”;
}
Note: After getting output press Alt+F5 keys
then check output.
4
Lalita bhosale B.Sc-II(Sem-III) 2020-21 lbs
• Syntax to declare static data member:
Here,
‘static’ is keyword which is used to declare member as static
‘datatype’ is any valid data type in C++.
‘mem_name’ is name of data member of class.
Following fig. shows sharing of static data members by different objects of class:
In above fig. static data of class is common to all objects Object1, Object2, Object3, Object4. Or the same copy of
static data is shared among all objects.
Here,
‘static’ is keyword which is used to declare member function as static
‘return_type’ is any valid data type in C++ that shows value return from function.
‘mem_fun’ is name of member function of class.
• Syntax for calling or accessing the static member function.
class_name :: mem_fun(arg1,arg2,…);
Here,
‘class_name’ is name of the class
‘mem_fun’ is name of static member function of class.
‘arg1,arg2,…’ are the arguments accept by the static member function.
Following program demonstrate the use of static data members and static member function to count total
number of objects of class:
#include<iostream.h>
#include<conio.h>
class count
{
static int cnt; //declaration of static data mem.
public:
count( ); //default constructor
static void show( ); //static mem. function
};
int count::cnt; //definition of static data member
count::count( )
{
cnt++;
}
void count::show( )
{
cout<<"\n Total objects= "<<cnt;
}
void main( )
{
clrscr( );
count a, b, c; //three times constructor invoked
count::show( ); // call to static mem. Fun.
getch( );
}
OUTPUT:
Total objects= 3
6
Lalita bhosale B.Sc-II(Sem-III) 2020-21 lbs
Assignment: 3