下面代码输出什么:
#include <iostream>
using namespace std;
class CStudent
{
int i;
public:
CStudent()
{
cout<<"CStudent()"<<endl;
}
~CStudent()
{
}
};
class CManager
{
CStudent s1;
CStudent s2;
public:
CManager()
{
cout<<"CManager()"<<endl;
}
~CManager()
{
}
};
int main()
{
CManager m1;
return 0;
}
答案:
输出:
CStudent()
CStudent()
CManager()
Press any key to continue
当类A中有类B作为它的成员变量,构造类A的时候会先执行类B的构造函数;
如果有多个类B对象,构造类B的对象的顺序按照它的声明的先后顺序去构造