[amp_mcq option1=”the parent class object must be constructed first” option2=”the child class object must be constructed first” option3=”the parent class object must not be constructed” option4=”the child class object must not be constructed” correct=”option1″]
The correct answer is: A. the parent class object must be constructed first.
When you create a derived class, the parent class object is always constructed first. This is because the derived class inherits from the parent class, and the parent class’s members are available to the derived class. The parent class’s constructor must be called before the derived class’s constructor can be called.
Here is an example:
“`class Parent {
public:
Parent() {
cout << “Parent constructor called” << endl;
}
};
class Child : public Parent {
public:
Child() {
cout << “Child constructor called” << endl;
}
};
int main() {
Child c;
return 0;
}
“`
In this example, the Parent constructor is called first, followed by the Child constructor. This is because the Child constructor inherits from the Parent constructor.
I hope this helps!