In C++, constructors are special member functions, which are called automatically, when an object is created. There are two main types of constructors: default constructors and parameterized constructors.
A default constructor is a constructor that does not take any parameters or has default values for all parameters.
whereas a parameterized constructor is a constructor, which takes one or more parameters and is used to initialize an object with specific values passed when an object is created.
In this chapter, we will learn only about the Parameterized Constructor.
C++ Parameterized Constructors
C++ parameterized constructor is a constructor, which accepts one or more arguments or parameters, to initialize an object with specific values, when created. Unlike the default constructor, which takes no arguments, a parameterized constructor takes one or more arguments.
Syntax
Here is the following syntax for declaring a parameterized constructor:
ClassName(parameters){// Initialization code}
Example of Parameterized Constructor
Here is the following example of a parameterized constructor in C++:
#include <iostream>usingnamespace std;classMyClass{public:int a, b;// Parameterized constructor with two argumentsMyClass(int x,int y){
a = x;// Initialize 'a' with the value of 'x'
b = y;// Initialize 'b' with the value of 'y'
cout <<"Parameterized constructor called"<< endl;}// Method to display values of 'a' and 'b'voiddisplay(){
cout <<"a: "<< a <<", b: "<< b << endl;}};intmain(){
MyClass obj1(10,20);// Calls the parameterized constructor with 10 and 20
obj1.display();// Output: a: 10, b: 20return0;}
Output
Parameterized constructor called a: 10, b: 20
Explanation
- First defined the class named MyClass, with two public data members a and b of int type.
- Then a parameterized constructor, MyClass(int x, int y), takes two arguments (int x, int y) and initializes them a and b with values passed when the object is created.
- Now when the MyClass obj1(10, 20) is called, the parameterized constructor will be invoked.
- and display() method will print the values of a and b.
Multiple Parameterized Constructors (Constructor Overloading)
Constructor overloading or multiple parameterized constructors in C++, is a concept, which allows users to define multiple constructors in the same class with different parameter lists, where each constructor can initialize the object differently depending on the arguments passed while creating an object.
Key points
- Same Constructor Name − All overloaded constructors must have the same name as the class.
- Different Parameters − Constructors must have a different number or types of parameters.
- Flexibility − Constructor overloading also gives flexibility during object initialization by allowing the same class to be instantiated in various ways.
Syntax
Here is the following syntax for multiple parameterised constructor:
classClassName{public:// Default constructor (no parameters)ClassName(){// Initialization code}// Parameterized constructor with one parameterClassName(Type1 param1){// Initialization code using param1}// Parameterized constructor with two parametersClassName(Type1 param1, Type2 param2){// Initialization code using param1 and param2}// Parameterized constructor with three parametersClassName(Type1 param1, Type2 param2, Type3 param3){// Initialization code using param1, param2, and param3}};
Parameterized Constructors with Default Arguments
A parameterized constructor with default arguments is a constructor, which allows a user to provide default values for one or more parameters, which means you can either pass values while creating the object or let the constructor use the default values for the missing parameters.
Syntax
Here is the following syntax for a parameterized constructor with default arguments:
classClassName{public:// Constructor with default argumentsClassName(type1 param1 = default_value1, type2 param2 = default_value2,...){// Constructor implementation}};
Advantages of Using Parameterized Constructors
Parameterized constructors offer various benefits, which make object initialization more flexible, clean, and efficient. Below we have discussed some of the key advantages.
- It allows flexible object initialization with simplified code, improving readability and maintainability.
- It prevents Object Creation with Invalid Data, where it allows valid data only.
- It reduces the need for setter functions and supports constructor overloading.
- It also provides better memeory management (with Dynamic Memory Allocation), enforces Default Values for Missing Arguments, and overall improves code performance.
Leave a Reply