Constructors in C++ and its Types
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.
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)
Best-suited C++ courses for you
Learn C++ with these high-rated online courses
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};
Definition Outside the class:
Syntax β
class CLASS_NAME{ β¦β¦β¦public :CLASS_NAME([parameter_list]) //constructor Declaration{ . . . . . . } //other member functions}; CLASSNAME: :CLASSNAME([parameter_list]) //Constructor Definition{. . . . . . . . . . .}
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};
Example:
#include <iostream>using namespace std; //Class Name: Default_constructclass 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;}
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 { β¦β¦β¦β¦β¦β¦.. }};
Example:
#include <iostream>using namespace std; // class name: Rectangleclass 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 constructorsRectangle obj1(10,6);Rectangle obj2(13,8); cout << "Area of Rectangle 1: " << obj1.calculateArea(); cout << "Area of Rectangle 2: " << obj2.calculateArea(); return 0;}
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: Rectangleclass 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 constructorsRectangle 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;}
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;}
Output:
You can also explore these articles related to C++:
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.
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