-
#ifndef FLYABLE_H
#define FLYABLE_H
//在Flyable這個類中定義兩個純虛函數(shù)takeoff()和land()
class Flyable
{
public:
virtual void takeoff() = 0;
virtual void land() = 0;
};
#endif
查看全部 -
#ifndef PLANE_H
#define PLANE_H
#include "Flyable.h"
#include <string>
using namespace std;
class Plane:public Flyable ?//公有繼承了Flyable
{
public:
void carry(); //Plane具有一個特有的成員函數(shù)carry(運輸)
virtual void takeoff(); //實現(xiàn)了Flyable中的虛函數(shù)takeoff和land
virtual void land();
};
#endif
查看全部 -
#include <iostream>
#include "Plane.h"
using namespace std;
void Plane::carry()
{
cout << "Plane --> carry()" << endl;
}
void Plane::takeoff()
{
cout << "Plane --> takeoff()" << endl;
}
void Plane::land()
{
cout << "Plane --> land()" << endl;
}
查看全部 -
#include <iostream>
#include "stdlib.h"
#include "Bird.h"
#include "Plane.h"
using namespace std;
/*********************************************************************************************************/
/*RTTI
? ?1.Flyable類,成員函數(shù): takeoff. land
? ?2.Plane類,成員函數(shù): takeoff. land, carry
? ?3.Bird類,成員函數(shù): takeoff. land, foraging
? ?4.全局函數(shù)doSomething(Flyable *obj)
*/
/*********************************************************************************************************/
void dosomething(Flyable *obj)
{
cout << typeid(*obj).name() << endl; ?//打印傳入的對象指針究竟是什么類型的對象
obj->takeoff();
if(typeid(*obj) == typeid(Bird)) //這里判斷obj這個指針?biāo)赶虻膶ο笫遣皇荁ird類型
{
Bird *bird = dynamic_cast<Bird *>(obj); //將obj這個指針通過dynamic_cast強制轉(zhuǎn)換為Bird指針,并且將這個指針賦值給一個新的指針bird
bird->foraging(); //通過這個bird指針來調(diào)用foraging(覓食)這個成員函數(shù)
}
if(typeid(*obj) == typeid(Plane)) //這里判斷obj這個指針?biāo)赶虻膶ο笫遣皇荁ird類型
{
Plane *plane = dynamic_cast<Plane *>(obj); //將obj這個指針通過dynamic_cast強制轉(zhuǎn)換為Plane指針,并且將這個指針賦值給一個新的指針plane
plane->carry(); //通過這個plane指針來調(diào)用carry(運輸)這個成員函數(shù)
}
obj->land();
}
int main()
{
Bird b;
dosomething(&b);
system("pause");
return 0;
}
查看全部 -
//Plan.cpp
#include <iostream>
#include "Plane.h"
using namespace std;
Plane::Plane(string code)
{
m_strCode = code;
}
void Plane::takeoff()
{
cout <<"Plane -takeoff" << endl;
}
void Plane::land()
{
cout <<"Plane --land" << endl;
}
void Plane::printCode()
{
cout <<m_strCode<< endl;
}
查看全部 -
//main.cpp
#include<iostream>
#include<stdlib.h>
#include"FighterPlane.h"
using namespace std;
/*********************************************************************************************************/
/*接口類
? ? 1. Flyable類,成員函數(shù): takeoff、land
? ? 2. Plane類,成員函數(shù):構(gòu)造函數(shù)、takeoff、land、 printCode ,數(shù)據(jù)成員:m_strCode
? ? 3. FighterPlane類,成員函數(shù):構(gòu)造函數(shù)、takeoff. land
? ? 4.全局函數(shù)flyMatch(Flyable *f1, Flyable *f2)
*/
/*********************************************************************************************************/
?
void flyMatch(Flyable *f1,Flyable *f2)
{
f1->takeoff ();
f1->land ();
f2->takeoff ();
f2->land ();
}
int main(void)
{
FighterPlane p1("001");
FighterPlane p2("002");
p1.printCode ();
p2.printCode ();
flyMatch(&p1,&p2);
return 0;
}
查看全部 -
virtual不能加在構(gòu)造函數(shù)前
virtual不能加在普通函數(shù)前
virtual不能加在靜態(tài)成員前
virtual加在內(nèi)聯(lián)函數(shù)inline前,內(nèi)聯(lián)屬性被覆蓋
查看全部 -
#include<iostream>
#include"Flable.h"
#include"Bird.h"
#include"Plane.h"
using namespace std;
void dosomething(Flable *obj)
{
cout << typeid(*obj).name() << endl;
obj->takeoff();
if (typeid(*obj)==typeid(Bird))
{
Bird *bird = dynamic_cast<Bird*>(obj);
bird->foraging();
}
if (typeid(*obj) == typeid(Plane))
{
Plane *plane = dynamic_cast<Plane*>(obj);
plane->carry();
}
obj->land();
}
int main(void)
{
Bird p;
dosomething(&p);
system("pause");
return 0;
}
查看全部 -
typeid返回的對象類型
查看全部 -
定義:在某基類中聲明為 virtual 并在一個或多個派生類中被重新定 義的成員函數(shù)
語法:virtual 函數(shù)返回類型 函數(shù)名(參數(shù)表) {?函數(shù)體?}
用途:實現(xiàn)多態(tài)性,通過指向派生類的基類指針,訪問派生類中同名覆蓋成員函數(shù)
查看全部 -
通過父類捕獲子類異常
Exception 可以理解為類型 &e是拋出的異常值
查看全部 -
異常和多態(tài)的關(guān)系
父類定義一個接口類
子類具體捕獲不同的異常,拋出子類異常則都可以通過父類捕獲
catch(Exception &e)
可以通過父類exception 捕獲所有子類異常,并通過父類的虛函數(shù)調(diào)用不同子類的方法?
查看全部 -
常見的異常
查看全部 -
捕獲異常的值例子
查看全部 -
捕獲異常的值
查看全部
舉報