018_linuxC++之_抽象类的引入

(一)参考原文链接:C++多态

(二)

抽象类
在介绍抽象类之前,我们先介绍一下纯虚函数。

1.纯虚函数

在基类中仅仅给出声明,不对虚函数实现定义,而是在派生类中实现。这个虚函数称为纯虚函数。普通函数如果仅仅给出它的声明而没有实现它的函数体,这是编译不过的。纯虚函数没有函数体。

纯虚函数需要在声明之后加个=0;

class    <基类名>
{
  virtual <类型><函数名>(<参数表>)=0; ......
};

2.抽象类

含有纯虚函数的类被称为抽象类。抽象类只能作为派生类的基类,不能定义对象,但可以定义指针。在派生类实现该纯虚函数后,定义抽象类对象的指针,并指向或引用子类对象。

1)在定义纯虚函数时,不能定义虚函数的实现部分;

2)在没有重新定义这种纯虚函数之前,是不能调用这种函数的。

抽象类的唯一用途是为派生类提供基类,纯虚函数的作用是作为派生类中的成员函数的基础,并实现动态多态性。继承于抽象类的派生类如果不能实现基类中所有的纯虚函数,那么这个派生类也就成了抽象类。因为它继承了基类的抽象函数,只要含有纯虚函数的类就是抽象类。纯虚函数已经在抽象类中定义了这个方法的声明,其它类中只能按照这个接口去实现。

3.接口和抽象类的区别

1)C++中我们一般说的接口,表示对外提供的方法,提供给外部调用。是沟通外部跟内部的桥梁。也是以类的形式提供的,但一般该类只具有成员函数,不具有数据成员;

2)抽象类可以既包含数据成员又包含方法。
在这里插入图片描述

(三)示例

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {                                /*抽象类*/
private:
    int a;
public:
    virtual void eating(void) = 0;
    virtual void wearing(void) = 0;
    virtual void driving(void) = 0;
    virtual ~Human() { cout<<"~Human()"<<endl; }
    virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
    void eating(void) { cout<<"use knife to eat"<<endl; }
//    void wearing(void) {cout<<"wear english style"<<endl; }
    void driving(void) {cout<<"drive english car"<<endl; }
    virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
    virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
    void eating(void) { cout<<"use chopsticks to eat"<<endl; }
    void wearing(void) {cout<<"wear chinese style"<<endl; }
    void driving(void) {cout<<"drive chinese car"<<endl; }
    virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
    virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};



int main(int argc, char **argv)
{
    //Human h;        /*抽象类不能实例化对象*/
    //Englishman e;    /*子类没有覆写完全部的存虚函数,则子类也是抽象类,则不能实例化对象*/
    Chinese c;

    return 0;
}

(四)在子类中没有编写存虚函数的,可在派生类中编写,这样可以编译通过

#include <iostream>
#include <string.h>
#include <unistd.h>

using namespace std;

class Human {
private:
    int a;
public:
    virtual void eating(void) = 0;
    virtual void wearing(void) = 0;
    virtual void driving(void) = 0;
    virtual ~Human() { cout<<"~Human()"<<endl; }
    virtual Human* test(void) {cout<<"Human's test"<<endl; return this; }
};

class Englishman : public Human {
public:
    void eating(void) { cout<<"use knife to eat"<<endl; }
    void wearing(void) {cout<<"wear english style"<<endl; }
    void driving(void) {cout<<"drive english car"<<endl; }
    virtual ~Englishman() { cout<<"~Englishman()"<<endl; }
    virtual Englishman* test(void) {cout<<"Englishman's test"<<endl; return this; }
};


class Chinese : public Human {
public:
    void eating(void) { cout<<"use chopsticks to eat"<<endl; }
    void wearing(void) {cout<<"wear chinese style"<<endl; }
    //void driving(void) {cout<<"drive chinese car"<<endl; }
    virtual ~Chinese() { cout<<"~Chinese()"<<endl; }
    virtual Chinese* test(void) {cout<<"Chinese's test"<<endl; return this; }
};

class Guangximan : public Chinese {
    void driving(void) {cout<<"drive guangxi car"<<endl; }
};

int main(int argc, char **argv)
{
    //Human h;
    Englishman e;
    Guangximan g;

    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值