Constructors in C++ and its Types

Constructors in C++ and its Types

4 mins read1.2L Views Comment
Updated on Dec 29, 2023 16:56 IST

Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly three types of constructors in C++, Default, Parameterized and Copy constructors. In this article we will learn more about constructors in C++, its types and constructor overloading.

2022_04_Constructors-in-C.jpg

Constructors in C++ is a class member function called when an object of that class is created. Its main purpose is to initialize the data members of the object. Constructors have the same name as the class and can be declared either in the public or private section of the class. They do not have a return type, not even void.

But before we explore Constructor in C++ programming language in great detail, let’s go through the list of topics listed under the table of content (TOC) that we will cover in this article:

For more insights about C++, visit: C++ Certification courses

Table of Content (TOC)

What is Constructor in C++?

A constructor in C++ is a special member function with exact same name as the class name. 

  • The constructor name is the same as the Class Name. Reason: Compiler uses this character to differentiate constructors from the other member functions of the class.
  • A constructor must not declare a return type or void. Reason: As it’s automatically called and generally used for initializing values.
  • They can be defined inside or outside the class definition.
  • Automatically calls when an object is created for the class.
  • It uses to construct that means to initialize all the data members (variables) of the class.

Also, Read: OOPs concepts in C++

Crack the code of C/C++ with our comprehensive guide; find details on top colleges, programmes and online courses.

Constructor in C++ can be defined in the following ways:

Definition Inside the class:

Syntax –


 
class CLASS_NAME
{
………
public :
CLASS_NAME([parameter_list]) //constructor definition
{
.............
}
//other member functions
};
Copy code

Definition Outside the class:

Syntax


 
class CLASS_NAME
{
………
public :
CLASS_NAME([parameter_list]) //constructor Declaration
{
. . . . . .
}
//other member functions
};
CLASSNAME: :CLASSNAME([parameter_list]) //Constructor Definition
{
. . . . . . . . . . .
}
Copy code

You can also explore: C++ Structure and Functions

Points to remember about constructors in C++:

  • Constructors invoke simultaneously whenever an object of a class creates.
  • The compiler will automatically generate a default constructor if no constructor is defined explicitly.
  • Preferably, constructors use to declare the data members(variables). Usually, not utilized for generating input and output.
  • They also allocate memory at run time using the ‘new’ operator in C++.
  • More than one constructor can be declared as well. It’s “Constructor Overloading.”

Also Read: Top C++ Interview Questions and Answers

Constructors types in C++

There are 3 types of Constructors in C++. Let’s learn about them.

1. Default Constructor

A constructor with no arguments (or parameters) in the definition is a default constructor. It is the type of constructor in C++ usually used to initialize data members (variables) with real values. 

Note: The compiler automatically creates a default constructor without data member (variables) or initialization if no constructor is explicitly declared.

Syntax of Default Constructor


 
class CLASS_NAME
{
………
public :
CLASS_NAME() //Default constructor
{
. . . . . .
}
//other member functions
};
Copy code

Example:


 
#include <iostream>
using namespace std;
//Class Name: Default_construct
class Default_construct
{
public:
int a, b;
// Default Constructor
Default_construct()
{
a = 100;
b = 200;
}
};
int main()
{
Default_construct con; //Object created
cout << "Value of a: " << con.a;
cout<< "Value of b: " << con.b;
return 0;
}
Copy code

2. Parameterized Constructor

Unlike the Default constructor, It contains parameters (or arguments) in the constructor definition and declaration. More than one argument can also pass through a parameterized constructor.

You can also explore: Recursion in C++

Moreover, this type of constructor in C++ is used for overloading to differentiate between multiple constructors.

Syntax


 
class class_Name
{
public:
class_Name(datatype variable) //Parameterized constructor
{
………………..
}
};
Copy code

Example:


 
#include <iostream>
using namespace std;
// class name: Rectangle
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
}
double calculateArea() {
return length * breadth;
}
};
int main() {
// create objects to call constructors
Rectangle obj1(10,6);
Rectangle obj2(13,8);
cout << "Area of Rectangle 1: " << obj1.calculateArea();
cout << "Area of Rectangle 2: " << obj2.calculateArea();
return 0;
}
Copy code
 

3. Copy Constructor

A copy constructor is the third type among various types of constructors in C++. The member function initializes an object using another object of the same class. It helps to copy data from one object to another.

Example


 
#include <iostream>
using namespace std;
// class name: Rectangle
class Rectangle {
private:
double length;
double breadth;
public:
// parameterized constructor
Rectangle(double l, double b) {
length = l;
breadth = b;
}
// copy constructor with a Rectangle object as parameter copies data of the obj parameter
Rectangle(Rectangle &obj) {
length = obj.length;
breadth = obj.breadth;
}
double calculateArea() {
return length * breadth;
}
};
int main() {
// create objects to call constructors
Rectangle obj1(10,6);
Rectangle obj2 = obj1; //copy the content using object
//print areas of rectangles
cout << "Area of Rectangle 1: " << obj1.calculateArea();
cout << "Area of Rectangle 2: " << obj2.calculateArea();
return 0;
}
Copy code

Also, Read: Function overloading in C++

Constructor Overloading in C++

Overloading in Constructors are the constructors with the same name and different parameters (or arguments). Hence, the constructor call depends upon data types and the number of arguments.

Example:


 
// C++ program to showing Constructor overloading
#include <iostream>
using namespace std;
class constructor
{
public:
float area;
// Constructor with no parameters
constructor()
{
area = 0; //Giving value 0 to variable area
}
// Constructor with two parameters a and b
constructor(int a, int b)
{
area = a * b;
}
void display() //member function of the class
{
cout<< "Area: "<<area<< endl;
}
};
int main()
{
// Constructor Overloading
// with two different constructors
// of class name
constructor obj;
constructor obj2( 22, 40);
obj.display();
obj2.display();
return 1;
}
Copy code

Output:

2022_06_image-14.jpg

You can also explore these articles related to C++:

C++ Structure and Functions
C++ Structure and Functions
In C++, the structure is known to be a user-defined data structure and function is defined as a selection of code that only executes when called. In this article, we...read more
All About Virtual Functions in C++
All About Virtual Functions in C++
In C++, a virtual function is a member function of a class that can be overridden by derived classes. It is declared as virtual in the base class and can...read more
Recursion in C++
Recursion in C++
Recursion in C++ is a method that calls itself repeatedly until a predetermined condition is satisfied. A recursive function has a base case and a recursive condition, and it repeatedly...read more

 

 

FAQs

What is C++?

C++ is a popular object-oriented programming language. Programmers and software developers widely use it because of its flexibility and low-level functionality.

What is a Class?

A class is a user-defined data type that holds variables and member functions together, which can be accessed by using objects.

What is an Object?

An object is the class instance that invokes the data members and member functions on creation.

What is Constructor in C++?

The constructor name is the same as the Class Name. A constructor must not declare a return type or void. They can be defined inside or outside the class definition. They are automatically called when an object is created for the class.

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio