8 Sept 2019

  • September 08, 2019
  • Amitraj
Constructors in C++

A constructor is a member function of a class which initializes objects of a class. In C++, Constructor is automatically called when object(instance of class) create. It is special member function of the class.



How constructors are different from a normal member function?

A constructor is different from normal functions in following ways: (characterstics)

1. Constructor has same name as the class itself

2. Constructors don’t have return type

3. A constructor is automatically called when an object is created.

4. If we do not specify a constructor, C++ compiler generates a default constructor for us (expects no parameters and has an empty body).

5. constructor can not be virtual.

6. we can not refer to their address.

7. They can not be inherited , through a derived class can call the base class constructor.

8. They make implicit calls to the operators new and delete when memory allocation is required.

9. Remember, when a constructor is declared for a class, initialization of the class objects become mendatory.





Types of Constructor

1. Default Constructor:-   Default constructor is the constructor which doesn’t take any argument. It has no parameters.



//cpp program to demonstrate default constructor..


#include<iostream>
using namespace std;
class BCA

{
   private:

 int BCA1,BCA2,BCA3;     //Data members

 public:

  BCA()      // Default constructor

  {

   BCA1= 1100;

   BCA2= 1000;

   BCA3= 1100;

   

   cout<<"I am called..\n";

  }

  

void showdata()

{

 cout<<"BCA1 total:"<<BCA1<<endl;

 cout<<"BCA2 total:"<<BCA2<<endl;

 cout<<"BCA3 total:"<<BCA3<<endl;

}

};



int main()

{

 BCA b1,b2,b3;

 b1.showdata();

 b2.showdata();

 b3.showdata();

 return 0;

}


INPUT/OUTPUT:-

I am called..
I am called..
I am called..



BCA1 total:1100
BCA2 total:1000
BCA3 total:1100
BCA1 total:1100
BCA2 total:1000
BCA3 total:1100
BCA1 total:1100
BCA2 total:1000
BCA3 total:1100



Note:  Even if we do not define any constructor explicitly, the compiler will automatically provide a default constructor implicitly




2. Parameterized Constructors:-   


-> It is possible to pass arguments to constructors. Typically, these arguments help initialize an object when it is created. To create a parameterized constructor, simply add parameters to it the way you would to any other function. When you define the constructor’s body, use the parameters to initialize the object.



//cpp program to demonstarte default and parameterized constructor..



#include<iostream>
#include<string.h>
using namespace std;
class student

{
   private:
  
   int rollno;


  char name[20];


 public:


  student() // Default constructor


  {


   cout<<"Enter roll no.:";


   cin>>rollno;


   cout<<"Enter name:";


   cin>>name;


  }


 student(int r,char n[])  // Parameterized constructor


   {


    rollno=r;


    strcpy(name,n); //name=n;


   }


   void show()


   {


     cout<<"\n rollno="<<rollno<<"\n name="<<name;


   }

};

int main()


{
    student s1,s2(10,"xyz"),s3;


 s1.show();


 s2.show();


 s3.show();


 return 0;


}


input/output:-

Enter roll no.:1
Enter name:abc
Enter roll no.:2
Enter name:xyz
 rollno=1
 name=abc
 rollno=10
 name=xyz
 rollno=2
 name=xyz





Uses of Parameterized constructor:

1. It is used to initialize the various data elements of different objects with different values when they are created.

2. It is used to overload constructors.


Can we have more than one constructors in a class?

Yes, It is called Constructor overloading.





Constructor overloading:-  

-> In C++, We can have more than one constructor in a class with same name, as long as each has a different list of arguments.This concept is known as Constructor Overloading and is quite similar to function overloading

-> Overloaded constructors essentially have the same name (name of the class) and different number of arguments.

-> A constructor is called depending upon the number and type of arguments passed.

-> While creating the object, arguments must be passed to let compiler know, which constructor needs to be called.


// CPP program to demonstrate constructor overloading.


#include<iostream>
using namespace std;
class shape 
int a,b,c;
public: 
shape() 
{
    a=b=c=0;
}
shape(int x,int y)
{
    a=x;
    b=y;
    c=100;
}
shape(int x,int y,int z)
{
    a=x;
    b=y;
    c=z;
}
void display()
{
    cout<<"a="<<a<<"    b="<<b<<"   c="<<c<<endl;
}
};
int main()
{
    shape s1,s2(9,8), s3(11,12,16);
    s1.display();
    s2.display();
    s3.display();
    return 0;
}

INPUT/OUTPUT:
a=0    b=0   c=0
a=9    b=8   c=100

a=11    b=12   c=16





Dynamic Constructor in C++ with Examples

-> When allocation of memory is done dynamically using dynamic memory allocator new in a constructor, it is known as dynamic constructor. By using this, we can dynamically initialize the objects.



// cpp program on dynamic constructor..


#include <iostream> 
using namespace std; 

  class abc





    const char* p; 


public: 


    // default constructor 


    abc() 


    { 


        // allocating memory at run time 


        p = new char[6]; 


        p = "abc"; 


    } 


    void display() 


    { 


        cout << p << endl; 


    } 


}; 


int main() 





    abc obj = new abc(); 


    obj.display(); 





Output:

abc

Translate

Popular Posts