A parameterized constructor is a constructor that takes one or more parameters. It is used to initialize the member variables of an object with the values passed as arguments to the constructor.
For example, consider a class “Student” with three member variables “name”, “age”, and “marks”. The parameterized constructor for this class might look like this:
class Student {
public:
string name;
int age;
float marks;
// parameterized constructor
Student(string n, int a, float m) {
name = n;
age = a;
marks = m;
}
};
This constructor takes three parameters: “n” for the name, “a” for the age, and “m” for the marks. The body of the constructor initializes the member variables “name”, “age”, and “marks” with the values of the corresponding parameters.
To create an object of the “Student” class using this parameterized constructor, you would use the following syntax:
Student s("John", 20, 80.5); // creates a Student object with name "John", age 20, and marks 80.5
The parameterized constructor allows you to create objects of the class with different initial values for the member variables. It is a convenient way to initialize objects with specific values, rather than having to set the values of the member variables manually after creating the object.