C++虚函数接口类
2016-03-15 19:47
363 查看
/*====================================================================== 接口类: 仅含有纯虚函数的类称为接口类。 无数据成员 成员函数全部是纯虚函数 接口类更多的表达一种能力或协议 起飞 降落 子类一定要实现这些纯虚函数 /*======================================================================*/ #include <iostream> #include<string> using namespace std; class Flyable { public: Flyable() {} virtual ~Flyable(){} virtual void land() = 0; virtual void takeoff() = 0; }; class Plane:public Flyable { public: Plane(string name) { this->m_strNum = name; } ~Plane() {} virtual void takeoff() { cout << "飞机起飞" << endl; } virtual void land() { cout << "飞机降落" << endl; } virtual void printcode() { cout << "我是飞机"<< m_strNum << endl; } protected: string m_strNum; }; class FightPlane:public Plane { public: FightPlane(string name):Plane(name) { } virtual ~FightPlane(){} virtual void takeoff() { cout << "战斗机起飞" << endl; } virtual void land() { cout << "战斗机降落" << endl; } virtual void printcode() { cout << "我是战斗机" << m_strNum << endl; } private: }; void flyMatch(Flyable *f1, Flyable*f2) { f1->takeoff(); f1->land(); f2->takeoff(); f2->land(); } int main() { Flyable *f1=new FightPlane("f1"); FightPlane *f2 = new FightPlane("f2"); flyMatch(f1, f2); return 0; }
相关文章推荐
- 我的C语言之路2
- OC语言—— add 与set 的区别
- 简明python教程 --C++程序员的视角(九):函数式编程、特殊类方法、测试及其他
- 简明python教程 --C++程序员的视角(八):标准库
- 【bzoj 3809】Gty的二逼妹子序列 题解&代码(c++)
- 简明python教程 --C++程序员的视角(七):异常
- 简明python教程 --C++程序员的视角(六):输入输出IO
- 简明python教程 --C++程序员的视角(五):面向对象的编程
- 简明python教程 --C++程序员的视角(四):容器类型(字符串、元组、列表、字典)和参考
- 简明python教程 --C++程序员的视角(三):模块
- 简明python教程 --C++程序员的视角(二):函数及作用域
- 简明python教程 --C++程序员的视角(一):数值类型、字符串、运算符和控制流
- C++对C语言的非面向对象特性扩充(3)
- 蓝桥杯 往届试题(C++B组)
- C++ vector多维数组初始化及清零
- C++的发展,特点和源程序构成
- 第一次c++上级报告-2
- C++之命名空间(End Chapter)
- Effective C++第三遍
- C++运算符重载:怎样定义函数才“符合常理”