[C++]接口类

仅含有纯虚函数的类称为接口类

无数据成员

成员函数都是纯虚函数

Flyable.h

#ifndef  FLYABLE_H
#define FLYABLE_H

class Flyable
{
public:
    virtual void takeoff() = 0;
    virtual void land() = 0;
};

#endif // ! FLYABLE_H

Plane.h

#ifndef PLANE_H
#define PLANE_H

#include <string>
#include "Flyable.h"
using namespace std;

class Plane :public Flyable
{
public:
    Plane(string code);
    virtual void takeoff();
    virtual void land();
    void printCode();
private:
    string m_strCode;
};


#endif // !PLANE_H

Plane.cpp

#include "Plane.h"
#include <iostream>
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;
}

FighterPlene.h

#ifndef FIGHTERPLANE_H
#define FIGHTERPLANE_H

#include "Plane.h"
#include <string>
using namespace std;

class FighterPlane :public Plane
{
public:
    FighterPlane(string code);
    virtual  void takeoff();
    virtual void land();
};

#endif // !FIGHTERPLANE_H

FighterPlane.cpp

#include "FighterPlane.h"
#include "FighterPlane.h"
#include <iostream>
using namespace std;

FighterPlane::FighterPlane(string code) :Plane(code)
{
}

void FighterPlane::takeoff()
{
    cout << "FigheterPlane---takeoff" << endl;
}

void FighterPlane::land()
{
    cout << "FighterPlane---land" << endl;
}

Demo.cpp

#include "Plane.h"
#include "FighterPlane.h"


void flyMatch(Flyable *f1, Flyable *f2)
{
    f1->takeoff();
    f1->land();
    f2->takeoff();
    f2->land();
}

int main(void)
{
    Plane p1("001");
    FighterPlane p2("002");
    p1.printCode();
    p2.printCode();

    flyMatch(&p1, &p2);

    system("pause");
    return 0;
}

运行

Paste_Image.png

多继承,父类可以是接口类也可以是普通类

Person.h

#ifndef PLANE_H
#define PLANE_H

#include <string>
#include "Flyable.h"
using namespace std;

class Plane 
{
public:
    Plane(string code);
    void printCode();
private:
    string m_strCode;
};


#endif // !PLANE_H

FighterPlane.h

#ifndef FIGHTERPLANE_H
#define FIGHTERPLANE_H

#include "Plane.h"
#include <string>
using namespace std;

class FighterPlane :public Plane,public Flyable
{
public:
    FighterPlane(string code);
    virtual  void takeoff();
    virtual void land();
};

#endif // !FIGHTERPLANE_H

Demo.cpp

#include "Plane.h"
#include "FighterPlane.h"


void flyMatch(Plane *f1, Flyable *f2)
{
    f1->printCode();

    f2->takeoff();
    f2->land();
}

int main(void)
{
    FighterPlane p1("001");
    FighterPlane p2("002");

    flyMatch(&p1, &p2);

    system("pause");
    return 0;
}
Paste_Image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一、程序设计概念等 结构化程序设计特点: 程序设计=数据结构+算法程序内容=过程+过程调用 面向对象的程序设计方法...
    C2U阅读 716评论 0 1
  • 前言 把《C++ Primer》[https://book.douban.com/subject/25708312...
    尤汐Yogy阅读 9,541评论 1 51
  • 动态调用动态库方法c/c++linuxwindows 关于动态调用动态库方法说明 一、 动态库概述 1、 动态库的...
    KINGZ1993阅读 13,976评论 0 10
  • 1.面向对象的程序设计思想是什么? 答:把数据结构和对数据结构进行操作的方法封装形成一个个的对象。 2.什么是类?...
    少帅yangjie阅读 5,051评论 0 14
  • 梦,多梦是人完成睡眠过程后,感觉乱梦纷纭并伴有头晕疲倦的一种状态。中医学认为,多梦的根本原因在于机体内在变化。古书...
    夏七格格阅读 257评论 0 0