- 打造c++抽象类
与其他语言的抽象类相似,抽象类不能实例化,也不能作为函数的参数,只可以作为指针类型或者引用类型
- 抽象类的基本特征
#pragma once #include<iostream> using namespace std; class jetplane { private: protected: public: virtual void loadweapon(string type,int num)=0;//最显著特征,成员函数声明为virtual,并且等于0 virtual void warfare()=0; };
当然抽象类不能实例化
#include<iostream> #include"jetplane.h" void main() { jetplane tornado(); }
报错:
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C2259 “jetplane”: 不能实例化抽象类 myabstractcls c:usersadministratorsource
eposmyabstractclsmyabstractclsmymain.cpp 5
2.解决多重继承的二义性问题
目前,要求多重继承的类有共同的基类,否则多重继承的二义性仍旧无法解决
实例化的类可以作为抽象类的接口使用
声明类mig31
#pragma once #include<iostream> #include<string> #include"jetplane.h" using namespace std; class mig31:public jetplane { private: string name; string origin; int num; string weaponset; protected: public: mig31::mig31(string name,string origin) { this->name = name; this->origin = origin; cout << "装备了" << this->name << "战机:原产国:" << this->origin << endl; } void loadweapon(string type, int num) { } void warning() { cout << this->name << "战机飞行员:" << "你已进俄罗斯空天军火力范围,我方奉命将你击落" << endl; } void warfare(string weaponset,int num) { cout<<this->name<< "战机发射了"<< weaponset<<"数量"<<num << endl; } };
- 声明类JAS-39
class jas39:public jetplane { private: string name; string origin; int num; string weaponset; protected: public: jas39::jas39(string name,string origin) { this->name = name; this->origin = origin; cout << "装备了" << this->name << "战机:原产国:" << this->origin << endl; } void warning() { cout << this->name << "战机飞行员:" << "你已进入瑞典领空,请迅速撤离,这是来自瑞典皇家空军的警告" << endl; } void loadweapon(string type, int num) { } void warfare(string weaponset,int num) { cout<<this->name<< "战机发射了"<< weaponset<<"数量"<<num << endl; } };
- 声明类f-15
class f15:public jetplane { private: string name; string origin; int num; string weaponset; protected: public: f15::f15(string name,string origin) { this->name = name; this->origin = origin; cout << "装备了" << this->name << "战机:原产国:" << this->origin << endl; } void warning() { cout << this->name << "战机飞行员:" << "乳罩呼叫内裤~乳罩呼叫内裤~,俄国人的轰炸机逼近阿拉斯加,是否给他们点儿颜色?" << endl; } void loadweapon(string type, int num) { } void warfare(string weaponset,int num) { cout <<this->name<< "战机发射了"<< weaponset<<"数量"<<num << endl; } };
- 声明JH-7
class jh7:public jetplane { private: string name; string origin; int num; string weaponset; protected: public: jh7::jh7(string name,string origin) { this->name = name; this->origin = origin; cout << "装备了" << this->name << "战机:原产国:" << this->origin << endl; } void warning() { cout << this->name << "战机飞行员:" << "你已进东海防空识别区,人民解放军空军奉命将你方驱离,请立刻离开我方空域" << endl; } void loadweapon(string type, int num) { } void warfare(string weaponset,int num) { cout<<this->name<< "战机发射了"<< weaponset<<"数量"<<num << endl; } };
- 在主程序文件中调用
#include<iostream> #include"jetplane.h" #include"mig31.h" #include"f15.h" #include"jh7.h" #include"jas39.h" void warning(jetplane *fighter) { fighter->warning(); } void main() { jetplane *fighter =NULL; f15 hawk("F-15", "美国"); mig31 foxbat("米格-31","苏联"); jas39 gripen("JAS-39鹰狮","瑞典"); jh7 flyleopard("JH-7飞豹", "中国"); fighter = &hawk; fighter->warning(); fighter = &foxbat; fighter->warning(); fighter = &gripen; fighter->warning(); fighter = &flyleopard; fighter->warning(); system("pause"); }
输出结果:
装备了F-15战机:原产国:美国
装备了米格-31战机:原产国:苏联
装备了JAS-39鹰狮战机:原产国:瑞典
装备了JH-7飞豹战机:原产国:中国
F-15战机飞行员:乳罩呼叫内裤~乳罩呼叫内裤~,俄国人的轰炸机逼近阿拉斯加,是否给他们点儿颜色?
米格-31战机飞行员:你已进俄罗斯空天军火力范围,我方奉命将你击落
JAS-39鹰狮战机飞行员:你已进入瑞典领空,请迅速撤离,这是来自瑞典皇家空军的警告
JH-7飞豹战机飞行员:你已进东海防空识别区,人民解放军空军奉命将你方驱离,请立刻离开我方空域
请按任意键继续. . .