class first{
int fa,fb;
public:
first();
first(int x,int y);
void display();
};
first::first():fa(0),fb(0){
}
first::first(int x,int y):fa(x),fb(y){
}
void first::display(){
cout<<fa<<" "<<fb;
}
class second{
first f;
int sa,sb;
public:
second();
second(int x,int y,int a,int b);
void display();
};
second::second():sa(0),sb(0){
}
second::second(int x,int y,int a,int b):f(x,y),sa(a),sb(b){
}
void second::display(){
cout<<"The Numbers are ";
f.display();
cout<<" "<<sa<<" "<<sb<<endl;
}
如果这个问题已经被问过,我很抱歉。
这是一个简单的代码,用于演示 c++ 中嵌套类的工作。但是,在类中,即使之前已经定义second
了对象,我也可以使用类的构造函数在其上调用构造函数。如何在已定义的类实例上调用构造函数?f
second