Updated March 23, 2023
Introduction to Parameterized Constructor in C++
There are many methods in C++. But parameterized constructor in C++ are some special types of method which gets instantiated as soon as an object is created. Therefore, there are two types of constructors defined in C++ namely default constructor, Parametrized constructor. There is a minute difference between default constructor and Parametrized constructor. The default constructor is a type of constructor which has no arguments but yes object instantiation is performed there also. On the other hand, as the name suggests Parametrized constructor is a special type of constructor where an object is created, and further parameters are passed to distinct objects.
Syntax:
class class_name {
Access Specifier:
Member - Variables
Member - Functions
public:
class_name(variables) {
// Constructor code
}
//... other Variables & Functions
}
The syntax says Class_name followed by access specifier which contains member variables and member functions. All these include all the constructor code which means the body of the constructor where it can be called also.
How Parameterized Constructor Works in C++?
Whenever a parameterized constructor is defined simultaneously an object gets instantiated which holds details or the values and parameters the object will contain or possess. It becomes a possible situation to pass arguments to that object. To create a parameterized constructor, it is needed to just add parameters as a value to the object as the way we pass a value to a function.
Somewhat similar scenario we do by passing the parametrized values to the object created with the class. Parameters are used to initialize the objects which are defined in the constructor’s body. Whenever a parameterized constructor is declared the values should be passed as arguments to the function of constructor i.e. constructor function otherwise the conventional way of object declaration will not work. These constructors can be called both implicitly or explicitly.
There are some uses or benefits of using parametrized constructors:
- When constructors are created or instantiated, they are used to initialize and hold the various data elements of different objects having different values.
- One more interesting scenario is that they are used to overload constructors.
Examples of Parameterized Constructor
Here are some of the examples of a parameterized constructor which is given below:
Example #1
Code:
#include <iostream>
using namespace std;
class ParamA {
private:
int b, c;
public:
ParamA (int b1, int c1)
{
b = b1;
c = c1;
}
int getX ()
{
return b;
}
int getY ()
{
return c;
}
};
int main ()
{
ParamA p1(10, 15);
cout << "p1.b = " << p1. getX() << ", p1.c = " << p1.getY();
return 0;
}
Output:
Explanation: In this class, ParamA contains two access specifiers one as a private access specifier and one as a public access specifier. Private access specifier involves a declaration of two variables which will be called and references later at some point of time. Followed by public access specifier where the constructor implementation gets started ParamA (int b1, int c1) refers to constructor initialization with int b1 and int c1 as parameters to be passed as values to the object which will call these values later. Output comes out as 10 and 15(values being passed).
Example #2
Code:
#include <iostream>
using namespace std;
class ParamCode {
public:
int x;
ParamCode (int i);
~ParamCode ();
};
ParamCode::ParamCode (int i) {
x = i;
}
ParamCode::~ParamCode() {
cout<< "Destructing those objects whose x value is " << x <<" \n";
}
int main () {
ParamCode t1(20);
ParamCode t2(15);
cout<< t1.x << " " << t2.x << "\n";
return 0;
}
Output:
Explanation: In Example 2 the use of destructors is being made. Destructors are created to remove the cache, or we can say history of a constructor. Once a constructor is created and values are initialized to those constructors, it is the responsibility of the destructor to take care of the constructor’s existence and remove its history automatically so that it does not create any impact on new constructor or object which is needed to be created.
In the Param code class first, the constructors are being initialized by passing int 1 as a parameter to it followed by a destructor. Then implementation gets started for the parameterized constructor. Then the implementation of the destructor class takes place which is responsible for destructing the object and passing the values.
Example #3
Code:
#include <iostream>
using namespace std;
class Professor {
public:
int id;
string name;
float salary;
Professor (int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display ()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Professor p1=Professor(10, "Aditya", 90000);
Professor p2=Professor(12, "Anu", 60000);
p1.display();
p2.display();
return 0;
}
Output:
Explanation: In this example, a class Professor is declared which includes an access specifier as public type and then it is followed with data members as int id and string name the output which includes the implementation of the constructor will display the name of the professor, Id of the professor and salary he or she earns. Further manipulations can also be made to this.
Example #4
Code:
#include<iostream>
#include<conio.h>
using namespace std;
class Easyexmple {
int j, k;
public:
Easyexmple (int m, int n) {
j = m;
k = n;
cout << "In this Constructor\n";
}
void Display () {
cout << "Values:" << j << "\t" << k;
}
};
int main () {
Easyexmple Object (30, 40);
Object.Display();
getch ();
return 0;
}
Output:
Explanation: A class easy example is declared which have access specifier as public and then initialization of constructor is done in order to assign values and then the object is being created where these values are passed and in this example, the constructor value comes out to be 30 and 40 respectively as per the input to the values or parameters to the constructor.
Conclusion
Constructors are nothing but a special kind of method where we can pass the values if not then also some value gets passed to the end object. The object will hold the values of member variable and methods which are part of the main class and then these values are passed finally with the help of constructors. A parameterized constructor has its own advantage of giving different values to the different objects and they can be overloaded also.
Recommended Articles
This is a guide to Parameterized Constructor in C++. Here we discuss how Parameterized Constructor works in C++ along with examples and code implementation. You can also go through our other related articles to learn more –