-->

9. CPP-Constructors

CPP Array of Objects: Previous                                                  Next: CPP Destructors
A constructor is special member function of a class which is used to initialize the member of a class whenever an object is first created. Constructor have some characteristic that makes it different from normal member function:


  1. Constructor have a same name equal to classname.
  2. Constructor doesn’t have any return data type.
  3. No access modifier [private, protected] is allowed. it follows only public modifier.
  4. No inheritance is possible with constructor.

Types of constructors:
  1. Default constructor [non parameterized].
  2. Copy constructor. [parameterized]
1. Default constructor :
  • This type of constructor is automatically created when object of a class creates.
  • This can be called non parameterized because it doesn’t contain parameters.
Syntax :
Class
{
               public :                   
( )
                                {
                                }
}; 
Eg:                                                                                class Sum
                                                                                {
                                                                                                int num1,num2;
                                                                                                public:
                                                                                                Sum()
                                                                                                {
                                                                                                                cout<<”default constructor called”;
                                                                                                }
                                                                                };
                                                                                void main( )
                                                                                {
                                                                                                Sum s1;
}
2. Parameterized:
  • Arguments are passed to this type of constructor.
  • When we make parameterized constructor in a class then default constructor not works.
Eg:
#include
Class Sum
{
                public:
                                Sum(int num1, int num2)
                                {
                                                cout<<”parameterized constructor”;
                                                cout<
                                }
};
void main( )
{
                Sum s1(20,30);
}
Constructors can be declared by two ways :
  1. Sum s1(20,30);
  2. Sum s2=Sum(20,30);
Multiple constructor in a class/constructor overloading :
  • Overloading allow us to make a more than one function in a program. That can be done with constructors also.
  • Multiple constructor can be made in a class, but they must be different in their no of arguments, data types of their arguments.
Eg:
 #include
 class Sum
{
                int a, b, c,sum1;
                public:
                                Sum() // non parameterized constructor
                                {
                                                a=0;
                                                b=0;
                                                c=0;
                                }
                                Sum(int x, int y, int z)
                                {             
                                                a=x;
                                                b=y;
                                                c=z;
                                }
                                void output()
                                {
                                                sum1=a+b+c;
                                                cout<<” sum is = “<
                                }
};
void main()
{
                Sum s;  //default constructor
                Sum s1(50,20,30);  //parameterized constructor called
                S1.calculate();
}

CPP Array of Objects: Previous                                                  Next: CPP Destructors