主题:继承的一个习题,可是不知道怎么来的结果
#include <iostream>
#include <string>
using namespace std;
class A
{
public:
int a;
int aa;
void display();
A();
};
class B :public A
{
public:
int b;
int bb;
A aaa;
void display();
B();
};
A::A(){a=10;aa=100;cout<<a<<aa<<endl;}
B::B(){a=11;aa=111;}
void A::display(){ cout <<"THE A : "<<endl;}
void B::display(){ cout <<"THE B : "<<a<<aa<<endl;
cout<<"NEXT : "<<aaa.a<<endl;}
int main(int argc, char *argv[])
{
B bbb;
bbb.B::display();
cout <<"The game is over! "<<endl;
return 0;
}
结果是:10100
10100 -------------------->就是这个怎么来的呢?
THE B :11111
NEXT :10
The game is over!
在子类中定义了一个对象不是先调用基类的构造函数吗?不是就调用一次吗?
#include <string>
using namespace std;
class A
{
public:
int a;
int aa;
void display();
A();
};
class B :public A
{
public:
int b;
int bb;
A aaa;
void display();
B();
};
A::A(){a=10;aa=100;cout<<a<<aa<<endl;}
B::B(){a=11;aa=111;}
void A::display(){ cout <<"THE A : "<<endl;}
void B::display(){ cout <<"THE B : "<<a<<aa<<endl;
cout<<"NEXT : "<<aaa.a<<endl;}
int main(int argc, char *argv[])
{
B bbb;
bbb.B::display();
cout <<"The game is over! "<<endl;
return 0;
}
结果是:10100
10100 -------------------->就是这个怎么来的呢?
THE B :11111
NEXT :10
The game is over!
在子类中定义了一个对象不是先调用基类的构造函数吗?不是就调用一次吗?