一道C++基础题

[复制链接]
查看11 | 回复2 | 2008-9-5 15:03:09 | 显示全部楼层 |阅读模式
读程序,写出程序的执行结果。
class A{
public:

A(){cout<<"A'constructor is called"<<endl;}

~A(){cout<<"A'destructor is called"<<endl;}
};
class B{
public:

B(){cout<<"B'constructor is called"<<endl;}

~B(){cout<<"B'destructor is called"<<endl;}
};
class C : public B{

A a;
public:

C():B(),a()

{
cout<<"C'constructor is called"<<endl;

}

~C(){cout<<"C'destructor is called"<<endl;}
};
void main()
{

C c();
}
在线等.高分赏 答对就有分

回复

使用道具 举报

千问 | 2008-9-5 15:03:09 | 显示全部楼层
主函数里面的C c(),应该改成C c; 因为没有参数,系统会自动调用构造函数的不应该加括号,还有要注意加#include头文件,下面是运行结果B'constructor is calledA'constructor is calledC'constructor is calledC'destructor is calledA'destructor is calledB'destructor is called...
回复

使用道具 举报

千问 | 2008-9-5 15:03:09 | 显示全部楼层
cout<<"B'constructor is called"<<endl;cout<<"A'constructor is called"<<endl; cout<<"C'constructor is called"<<endl;cout<<"C'destructor is called"<<endl;cout<<"A'destructor ...
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行