为了账号安全,请及时绑定邮箱和手机立即绑定

为什么“~ChildLabourer”改为“~childLabourer”才可通过

为什么要把“~ChildLabourer”改为“~childLabourer”才可通过????????????不应该和类同名吗?不应该是大写吗??????????????????????????????

正在回答

2 回答

并不是那个原因,你把代码放到vs里调试调试就知道了

0 回复 有任何疑惑可以回复我~

我的没改就通过了,就是和类名同名,提示的应该是错误的,我的是其他方面的错误也是提示析构函数错误应小写,

如下为能运行的代码

代码如下

#include <iostream>

#include <stdlib.h>

#include <string>

using namespace std;

/**

 * 定义工人类: Worker

 * 数据成员: m_strName

 * 成员函数: work()

 */

class Worker

{

public:

    Worker(string name)

{

m_strName = name;

cout << "Worker" << endl;

}

~Worker()

{

cout << "~Worker" << endl;

}

void work()

{

cout << m_strName << endl;

cout << "work" << endl;

}

protected:

string m_strName;

};


/**

 * 定义儿童类: Children

 * 数据成员: m_iAge

 * 成员函数: play()

 */

class Children

{

public:

Children(int age)

{

m_iAge = age;

cout << "Children" << endl;

}

~Children()

{

cout << "~Children" << endl;

}

void play()

{

cout << m_iAge << endl;

cout << "play" << endl;

}

protected:

int m_iAge;

};


/**

 * 定义童工类: ChildLabourer

 * 公有继承工人类和儿童类

 */

class ChildLabourer : public Worker,public Children

{

public:

ChildLabourer(string name, int age):Worker(name),Children(age)

{

cout << "ChildLabourer" << endl;

}


~ChildLabourer()

{

cout << "~ChildLabourer" << endl;

}

};


int main(void)

{

    // 使用new关键字创建童工类对象

ChildLabourer *p=new ChildLabourer("workerss",19);

    // 通过童工对象调用父类的work()和play()方法

p->work();

    p->play();

    // 释放

    delete p;

    p=NULL;


return 0;

}


0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之继承篇
  • 参与学习       75188    人
  • 解答问题       251    个

继承,C++面向对象三大特征之一,通过编码实践方式讲解到操作层面

进入课程

为什么“~ChildLabourer”改为“~childLabourer”才可通过

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号