#include<iostream>
using namespace std;
class Base{
private:
int x;
public:
Base(int i){
x = i;
}
void print(){
cout<<x<<endl;
}
};
class Derived: public Base{
public:
Base b;
public:
//the constructor of the derived class which contains the argument list of base class and the b
Derived(int i, int j):
Base(i),
b(j)
{}
};
int main(){
Derived d(11, 22);
d.print();
d.b.print();
return 0;
}
why the the value x of the b is 11, the value x of the d.b is 22? if the constructor of base initialize the int x in class Base, is there a Base object exist? the argument list of the derived constructor, should be one argument or two when there is a Base b as class member?
Derived::b
is notDerived::Base
– apple apple