Constructors in C++

C++ Constructors

On this page we will discuss about constructors in C++. Constructors are special functions in C++, these are used widely to initialise the objects with values for a class. Whenever any object is created at run time, the constructor is called by the compiler which initialises the defined value.

Constructors in C++

Constructors in C++ Language

Syntax

Defining Internally to the class

class Demo { 
  char c; 
  public: 
     Demo(){ 
       // Constructor declaration here 
       // User defined assignments for initialisation
  }
};

Defining Externally to the class

class Demo
{
 char c;
 public:
   Demo(); 
     //Constructor Declared here
};

Demo::Demo()   
// Constructor definition here
{
    // initialisation of values by the user
    char = "PrepInsta";
}

Why Constructors are used?

  • When an object is created, the memory is assigned the object entities and some garbage values are assigned to those.
  • These garbage values may cause hinderance in the programs, as it may be needed to only assign very specific values to entities/data.
  • Constructors do not have any return argument.
  • They have the same name as the name of the class and are enclosed within the class
  • C++ has its own version of constructor which assigns some garbage or random values to object entities.

Types of Constructors –

The following are the types of constructors in C++ –

  • Default
  • Parameterized
  • Copy

Default Constructors

  • Highlights – No Passing Arguments in the constructor.

For Example –

Run
#include <iostream>
using namespace std;
class Demo
{
   public:
        int i;
        Demo(){
            i=8000;
          }
};

int main()
{
  Demo  demo;
  cout << demo.i;

return 0;
}

Output

8000

The constructor, will be called and assign the value of i as 8000.

Parameterised Constructors

  • Highlights – Arguments are passed with the constructor.

For example –

Run
#include <iostream>
using namespace std;
class Demo
{
  public:
    int i;
    Demo(int val)
    {
       i=val;
    }
};
int main()
{
    //setting parameterised values
    Demo demo(6000);
    Demo demo2(12000);
    cout << demo.i<<endl;;
    cout << demo2.i;

return 0;
}

Output

6000 
12000

Copy Constructors

  • Highlights – Arguments are passed as objects and object is copied as instantiated as a new object.
  • The object can be passed in multiple ways
    • Demo d2 = d1;
    • Demo d3(d1);
  • While the following doesn’t call the copy constructor but only assigns the value with the help of assignment operator
    • Demo d4;
    • d4 = d1

For example –

Run
#include <iostream>
using namespace std;
class Demo 
{ 
   private: 
    int a, b; 
    public: 
    Demo()
    { }
    Demo(int a1, int b1) { 
     a = a1; 
     b = b1; 
    } 
    // Copy constructor example here
    Demo(const Demo &d2) 
    {
        a = d2.a; 
        b = d2.b;
   } 

    int getA()            
    {  
        return a; 

    } 
    int getB()            
    {  
        return b; 

    } 
}; 

int main() 
{ 
    Demo d1(5000, 6000); 
    
    // We have called a copy constructor 
    Demo d2 = d1; 

    // We have called a copy constructor     
    Demo d3(d1);
    Demo d4;

    // Assignment operation happens there is no call to copy constructor
    d4 = d1;

    // displaying values for both constructors 
    cout << "d1.a = " << d1.getA() << ", d1.b = " << d1.getB();
    cout << "\nd2.a = " << d2.getA() << ", d2.b = " << d2.getB(); 

    cout << "\nd3.a = " << d3.getA() << ", d3.b = " << d3.getB(); 
    cout << "\nd4.a = " << d4.getA() << ", d4.b = " << d4.getB(); 
    return 0; 
}

Output –

d1.a = 5000, d1.b = 6000
d2.a = 5000, d2.b = 6000      
d3.a = 5000, d3.b = 6000 d4.a = 5000, d4.b = 6000

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription