歪歪君 2016-04-15 13:04 采纳率: 0%
浏览 1434
已采纳

不明白以下结果是怎么出来的,请求分析过程。康桑阿米达

#include
using namespace std;
class Base
{
friend void fun();
public:
Base(int data = 0)
:b(data)
{
cout << "Base()" << endl;
}
~Base()
{
cout << "~Base()" << endl;
}
static void show()
{
cout << "static show()" << endl;
}
void add()
{
a++;
}
static int a;
private:
int b;
};
int Base::a = 0;
class Derive :public Base
{
public:
Derive(int data = 0)
:d(data)
{
cout << "Derive()" << endl;
}
~Derive()
{
cout << "~Derive()" << endl;
}
void Add()
{
a++;
}
private:
int d;
};

int main()
{
Derive d;
d.show();
d.a = 2;
d.add();
d.Add();
cout << d.a << endl;
return 0;
}

Base()
Derive()
static show()
4
~Derive()
~Base()

  • 写回答

3条回答

  • 小灸舞 2016-04-15 13:45
    关注

    前两个输出应该没问题吧(基类的构造函数函数优先被调用,和析构正好相反)
    子类不能从父类继承的有:
    1. 构造函数
    2. 拷贝构造函数
    3. 析构函数
    子类和父类是共享一个静态成员变量的(不是继承,因为静态成员是没有this指针的,是继承不过来的)
    所以打印出4应该能理解了吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料