Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Constructors in C++

Constructors

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.

If the constructor is not manually defined by the user, C++ calls its own version of constructor which is hidden from the user to give some default values and assign memory to member class entities.

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"; }
Constructor in C++

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
Constructors in C++

Default Constructors

  • Highlights – No Passing Arguments in the constructor.

For Example –

class Demo
{
int i;
public:
Demo() { i=8000; } }; int main() {
Demo
demo; cout << demo.i; }

Output – 8000

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

What is we don’t initialise our own version of constructor?

#include <iostream>
using namespace std;
class Demo
{
public:
int i;
};

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

Parameterised Constructors

  • Highlights – Arguments are passed with the constructor.

For example –

#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; cout << demo2.i; }

Here in our case the Output will be 6000 and 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
#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()
{
// We have initialised the first value via the help of parameterized constructor
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