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

正在回答

6 回答

class Bus :public Movable 打错了、、、

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

加了还有错吗,错误提示是你没有包含 typeinfo,如果加了还有错,加了过后的错误提示是什么,贴出来看看,或者加我,我给你发代码。

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

#include <iostream>

#include <stdlib.h>

#include <string>

#include <typeinfo>

using namespace std;


/**

 * 定义移动类:Movable

 * 纯虚函数:move

 */

class Movable

{

public:

    virtual void move() = 0;

};


/**

 * 定义公交车类:Bus

 * 公有继承移动类

 * 特有方法carry

 */

class Bus : public Movable

{

public:

    virtual void move()

    {

        cout << "Bus -- move" << endl;

    }

    

    void carry()

    {

        cout << "Bus -- carry" << endl;

    }

};


/**

 * 定义坦克类:Tank

 * 公有继承移动类

 * 特有方法fire

 */

class Tank :public Movable

{

public:

    virtual void move()

    {

        cout << "Tank -- move" << endl;

    }


    void fire()

    {

        cout << "Tank -- fire" << endl;

    }

};


/**

 * 定义函数doSomething含参数

 * 使用dynamic_cast转换类型

 */

void doSomething(Movable *obj)

{

    obj->move();


    if(typeid(*obj)==typeid(Bus))

    {

       Bus *bus = dynamic_cast<Bus *>(obj);

        bus->carry();

    }


    if(typeid(*obj)==typeid(Movable))

    {

        Tank *tank = dynamic_cast<Tank *>(obj);

        tank->fire();

    }

}


int main(void)

{

    Bus b;

    Tank t;

    doSomething(&b);

    doSomething(&t);

    return 0;

}



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

define_ray 提问者

又复制一遍干嘛?你看我那个哪里错了??指出错的地方就行
2015-12-24 回复 有任何疑惑可以回复我~
#include <iostream>
#include <stdlib.h>
#include <string>
#include <typeinfo>
using namespace std;

/**
 * 定义移动类:Movable
 * 纯虚函数:move
 */
class Movable
{
public:
    virtual void move() = 0 ;
};

/**
 * 定义公交车类:Bus
 * 公有继承移动类
 * 特有方法carry
 */
class Bus : public Movalbe
{
public:
    virtual void move()
    {
        cout << "Bus -- move" << endl;
    }
    
    void carry()
    {
        cout << "Bus -- carry" << endl;
    }
};

/**
 * 定义坦克类:Tank
 * 公有继承移动类
 * 特有方法fire
 */
class Tank : public Movable
{
public:
    virtual void move()
    {
        cout << "Tank -- move" << endl;
    }

    void fire()
    {
        cout << "Tank -- fire" << endl;
    }
};

/**
 * 定义函数doSomething含参数
 * 使用dynamic_cast转换类型
 */
void doSomething(Movable *obj)
{
    obj->move();

    if(typeid(*obj) == typeid(Bus))
    {
       Bus *bus = dynamic_cast<Bus *>(obj);
        bus->carry();
    }

    if(typeid(*obj) == typeid(Tank))
    {
        Tank *tank = dynamic_cast<Tank *>(obj);
        tank->fire();
    }
}

int main(void)
{
    Bus b;
    Tank t;
    doSomething(&b);
    doSomething(&t);
    return 0;
}

帮忙看下哪里不对……

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

用了 typeid 了,需要 #include <typeinfo> 才行

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

define_ray 提问者

加了还是不行,代码在下面,帮忙看看……
2015-12-24 回复 有任何疑惑可以回复我~

你需要在前面加上#include<typeinfo>这个头文件

1 回复 有任何疑惑可以回复我~
#1

define_ray 提问者

加上之后还有错……
2015-12-24 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消
C++远征之多态篇
  • 参与学习       66223    人
  • 解答问题       334    个

本教程将带领大家体会面向对象三大特性中的多态特性

进入课程

如图,哪里错了??

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

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

帮助反馈 APP下载

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

公众号

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