The correct answer is: C. constructor
A constructor is a special member function that is called when an object of a class is created. It is used to initialize the object’s data members. The name of the constructor must be the same as the name of the class.
In C++, a class named Student must have a constructor whose name is Student
. The constructor can be defined with or without parameters. If it is defined without parameters, it is called a default constructor. If it is defined with parameters, it is called a parameterized constructor.
The following is an example of a default constructor for the Student
class:
c++
class Student {
public:
Student() {
// Initialize the data members of the object
}
};
The following is an example of a parameterized constructor for the Student
class:
c++
class Student {
public:
Student(int id, string name) {
// Initialize the data members of the object
this->id = id;
this->name = name;
}
};
The constructor is called when an object of the class is created. For example, the following code creates a new object of the Student
class and calls the default constructor:
c++
Student student;
The following code creates a new object of the Student
class and calls the parameterized constructor:
c++
Student student(123, "John Doe");