[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 and instantiate an object, the parent class object must be constructed first. This is because the derived class inherits from the parent class, and 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::Parent()\n”;
}
};
class Child : public Parent {
public:
Child() {
cout << “Child::Child()\n”;
}
};
int main() {
Child c;
return 0;
}
“`
When this code is compiled and run, the following output is produced:
Parent::Parent()
Child::Child()
As you can see, the Parent class’s constructor is called first, followed by the Child class’s constructor. This is because the Child class inherits from the Parent class, and the Parent class’s constructor must be called before the derived class’s constructor can be called.
I hope this explanation is helpful. Let me know if you have any other questions.