写出下列程序的运行结果,并说明原因.(c++)
classBase{public:Base(intx):d1(x){cout<<“Basecons.”<<endl;}~Base(){cout<<“Basedes.”<<...
class Base{
public:
Base(int x):d1(x){cout<< “Base cons.”<<endl;}
~Base(){cout<< “Base des.”<<endl;}
};
class Derived:public Base{
public:
Derived(int x):Base(x) {cout<< “Derived cons.”<<endl;}
~Derived(){cout<< “Derived des.”<<endl;}
};
void main()
{ Derived obj(5); } 展开
public:
Base(int x):d1(x){cout<< “Base cons.”<<endl;}
~Base(){cout<< “Base des.”<<endl;}
};
class Derived:public Base{
public:
Derived(int x):Base(x) {cout<< “Derived cons.”<<endl;}
~Derived(){cout<< “Derived des.”<<endl;}
};
void main()
{ Derived obj(5); } 展开
1个回答
展开全部
你好,程序有错误,做如下修改:
#include <iostream>
using namespace std;
class Base{
public:
int d1;
Base(int x):d1(x){cout<< "Base cons."<<endl;}
~Base(){cout<< "Base des."<<endl;}
};
class Derived:public Base{
public:
Derived(int x):Base(x) {cout<< "Derived cons."<<endl;}
~Derived(){cout<< "Derived des."<<endl;}
};
void main()
{ Derived obj(5); }
运行结果如下:
Base cons.
Derived cons.
Derived des.
Base des.
原因:
先构建基类实例
再构建派生类实例
派生类实例析构
基类实例析构
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询