課程
/后端開發(fā)
/C++
/C++遠征之多態(tài)篇
。。。。。
2015-12-23
源自:C++遠征之多態(tài)篇 4-4
正在回答
class Bus :public Movable 打錯了、、、
加了還有錯嗎,錯誤提示是你沒有包含 typeinfo,如果加了還有錯,加了過后的錯誤提示是什么,貼出來看看,或者加我,我給你發(fā)代碼。
#include <iostream>
#include <stdlib.h>
#include <string>
#include <typeinfo>
using namespace std;
/**
?* 定義移動類:Movable
?* 純虛函數(shù):move
?*/
class Movable
{
public:
? ? virtual void move() = 0;
};
?* 定義公交車類:Bus
?* 公有繼承移動類
?* 特有方法carry
class Bus : public Movable
? ? virtual void move()
? ? {
? ? ? ? cout << "Bus -- move" << endl;
? ? }
? ??
? ? void carry()
? ? ? ? cout << "Bus -- carry" << endl;
?* 定義坦克類:Tank
?* 特有方法fire
class Tank :public Movable
? ? ? ? cout << "Tank -- move" << endl;
? ? void fire()
? ? ? ? cout << "Tank -- fire" << endl;
?* 定義函數(shù)doSomething含參數(shù)
?* 使用dynamic_cast轉(zhuǎn)換類型
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;
define_ray 提問者
#include?<iostream> #include?<stdlib.h> #include?<string> #include?<typeinfo> using?namespace?std; /** ?*?定義移動類:Movable ?*?純虛函數(shù):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; ????} }; /** ?*?定義函數(shù)doSomething含參數(shù) ?*?使用dynamic_cast轉(zhuǎn)換類型 ?*/ 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; }
幫忙看下哪里不對……
用了?typeid?了,需要?#include?<typeinfo>?才行
你需要在前面加上#include<typeinfo>這個頭文件
舉報
本教程將帶領(lǐng)大家體會面向?qū)ο笕筇匦灾械亩鄳B(tài)特性
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-03-24
class Bus :public Movable 打錯了、、、
2015-12-27
加了還有錯嗎,錯誤提示是你沒有包含 typeinfo,如果加了還有錯,加了過后的錯誤提示是什么,貼出來看看,或者加我,我給你發(fā)代碼。
2015-12-24
#include <iostream>
#include <stdlib.h>
#include <string>
#include <typeinfo>
using namespace std;
/**
?* 定義移動類:Movable
?* 純虛函數(shù):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;
? ? }
};
/**
?* 定義函數(shù)doSomething含參數(shù)
?* 使用dynamic_cast轉(zhuǎn)換類型
?*/
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;
}
2015-12-24
幫忙看下哪里不對……
2015-12-24
用了?typeid?了,需要?#include?<typeinfo>?才行
2015-12-24
你需要在前面加上#include<typeinfo>這個頭文件