data members of the derived class
data members of the base class
both (a) and (b)
neither (a) nor (b)
Answer is Right!
Answer is Wrong!
The correct answer is: C. both (a) and (b)
A derived class constructor may include arguments for data members of the derived class and/or data members of the base class.
For example, consider the following code:
“`
class Base {
public:
Base() {
cout << “Base::Base()” << endl;
48.6-11.4 42.9-11.4 132.3-11.4 132.3s0 89.4 11.4 132.3c6.3 23.7 24.8 41.5 48.3 47.8C117.2 448 288 448 288 448s170.8 0 213.4-11.5c23.5-6.3 42-24.2 48.3-47.8 11.4-42.9 11.4-132.3 11.4-132.3s0-89.4-11.4-132.3zm-317.5 213.5V175.2l142.7 81.2-142.7 81.2z"/>
Subscribe on YouTube
}
};
class Derived : public Base {
public:
Derived(int x) : x_(x) {
cout << “Derived::Derived(int)” << endl;
}
private:
int x_;
};
“`
In this example, the Derived constructor takes an argument for the x_ data member. This argument is passed to the Base constructor, which is then called to initialize the base class members.
The following code shows how to create an object of type Derived and pass an argument to the constructor:
Derived d(10);
This code will print the following output:
Base::Base()
Derived::Derived(int)
The Base::Base() statement is executed first, followed by the Derived::Derived(int) statement. The Derived::Derived(int) statement initializes the x_ data member of the Derived object with the value 10.