Example of parameterized constructor in C++
In C++, a constructor is a particular method that is executed by default when an object is created. It is commonly used to initialize the data members for newly created objects.
In C++, the constructor is named the same as the class or structure name. When an object is created, the constructor method is called.
• Constructor is a member of the same class which contains the same name as Class.
- A constructor is a particular type of function for member objects that are used to automatically initialize data elements for an object of a particular class when another object of a particular class is created.
- Constructor is called when an object is created. It is known as a constructor because it builds the values, i.e. supplies data for the object.
- Because constructors do not return values, they don't have a return type.
Characteristics of constructors:
- The constructor name and class name should be the same
- Constructors are mostly declared in the public section for the given class
- Constructors don’t have a return type
- Overloading of constructors can be possible
Parameterized Constructors in C++
We can pass the arguments in parameterized constructors. When an object is created, these parameters typically help in initializing the object.
To build a parameterized constructor, just add arguments to it as you would any other function. Use the arguments to initialize the object when defining the body of the constructors.
Example 1:
//Program for parameterized constructor in C++
#include <iostream>
using namespace std;
// The below code will help in creating the parameterized constructor
class ParamConstCode {
public:
int x1;
ParamConstCode (int in);
~ParamConstCode ();
};
ParamConstCode::ParamConstCode (int in) {
x1 = in;
}
ParamConstCode::~ParamConstCode() {
cout<< "The value of the variable x is: " << x1 <<" \n";
}
//Main section
int main () {
ParamConstCode tem1(29);
ParamConstCode tem2(19);
cout<< "the value for tem1.x1 is: "<<tem1.x1 <<" " << "the value for tem2.x1 is: "<<tem2.x1 << "\n";
return 0;
}
Output
The value for tem1.x1 is: 29 the value for tem2.x1 is: 19
The value of the variable x is: 19
The value of the variable x is: 29
Example 2:
// Program for the implementation of parameterized constructor
#include <iostream>
using namespace std;
// The class Rectangles can be used for calculating the length and breadth of the rectangle
class Rectangles {
private:
double lengthR;
double breadthR;
public:
// The Parameterized constructor for calculating the length and breadth
Rectangles(double le, double br) {
//variables are initialized
lengthR = le;
breadthR = br;
}
double findArea() {
return lengthR * breadthR;
}
};
// the main function of the program where execution starts
int main() {
Rectangles rect1(20, 15.6);
Rectangles rect2(23.5, 66);
// the code for printing the result as areas of the rectangle
cout << "The area of rectangle 1 is: " << rect1.findArea() << endl;
cout << "The area of rectangle 2 is: "<< rect2.findArea() << endl;
return 0;
}
Output
The area of rectangle 1 is: 312
The area of rectangle 2 is: 1551
Example3:
//C++ Program for implementation of Parameterized Constructor
#include <iostream>
using namespace std;
class Points {
private:
int n1, n2;
public:
// P
Points(int m1, int m2)
{
n1 = m1;
n2 = m2;
}
int getX() { return n1; }
int getY() { return n2; }
};
int main()
{
// calling of the constructor
Points parameter1(101, 165);
// Acessing the values
cout << "parameter1.n1 = " << parameter1.getX()
<< ", parameter1.n2 = " << parameter1.getY();
return 0;
}
Output
parameter1.n1 = 101, parameter1.n2 = 165