1. 程式人生 > >C++的中的繼承,多型和虛擬函式

C++的中的繼承,多型和虛擬函式

首先繼承,多型,虛擬函式,我們先了解一下各位的關係。

繼承是子類繼承父類,完成基礎功能的獲取,當然繼承有三種許可權,public,protect和private,如果不加許可權限定,預設繼承是私有繼承

許可權表如下:

所以可以看到凡私有成員,子類都不能用,不過有方法能用,這裡不討論。

多型:實際上就是通過繼承實現的,函式的多型性是指一個函式被定義成多個不同引數的函式。當你呼叫這個函式時,就會呼叫不同的同名函式。當然是不同的物件,也就是不同的類進行了虛擬函式的重寫。

虛擬函式:虛擬函式有兩種,一種是純虛擬函式,一種是就是非純虛擬函式。純虛擬函式只要申明一下,不需要定義,但非純虛擬函式是需要定義的(實現的)。只要包含有純虛擬函式的類,就稱為抽象類

。抽象類是不能被例項化的。

虛擬函式的申明:

虛擬函式
父類:virtual void getsize() const;
子類:void getsize() const override;

純虛擬函式:

父類:virtual void pest_virt() const = 0;
子類:void pest_virt() const override;

上面的const可以去掉,加上去的意思據說是防止過載,也就是防止引數變化

這裡以幾段程式碼試了一下:

定義兩個類,annimal和pest

#ifndef DUOTAIJINGTAI_ANIMAL_H
#define DUOTAIJINGTAI_ANIMAL_H

#include <string>

class animal {
public:
    animal(){};       ///申明建構函式不能沒有中括號,否則會報錯,未定義的annimal,很懵逼
    ~animal(){};
    static void print(std::string &a);
    void Gettpye();
    static std::string type;
    virtual void getsize() const;
    virtual void Who(){printf("I am annimal\n");} ;


};

class cat:animal{
public:
    cat(){};
    void getsize() const override;
    void Who(){printf("I am cat\n");} ;
    void Claw(){printf("I have a claw\n");};

};

class pest{
public:
    pest(){};
    virtual void pest_virt()=0;
};

class worm:public pest{
public:
    worm(){};
    virtual void pest_virt() override;
};



#endif //DUOTAIJINGTAI_ANIMAL_H

其中annimal是普通的基類,pest是抽象類

下面是兩個類的定義

#include <iostream>
#include "../include/animal.h"
void animal:: print(std::string &a)
{
    std::cout<<a<<std::endl;
    a="changed in print";
}
void animal:: Gettpye()
{
    std::cout<<type<<std::endl;
    type ="changed in get type";
    std::cout<<type<<std::endl;
}

std:: string animal::type;

void cat::getsize() const {
    std::cout<<"cat size"<<std::endl;
}

void animal::getsize() const{
    std::cout<<"annimal size"<<std::endl;
}

void worm:: pest_virt(){
    std::cout<<" i am a worm"<<std::endl;
}

下面是測試的主函式:

#include <iostream>
#include "include/animal.h"
#include <string>
int main() {
    animal::print(animal::type);
    animal::type ="hello delete";
    animal::print(animal::type);
    animal *test = new animal();
    test->Gettpye();
    animal::type ="hello delete change";
    animal test_;
    test_.Gettpye();
    test_.print(animal::type);
    cat Cat;
    Cat.getsize();
    Cat.gender =1;
    animal *unkown = new cat();
    unkown->Who();
    unkown->gender;
   
    

    worm ww;
    ww.pest_virt();
    delete unkown;
    delete test;
    return 0;
}

下面是輸出的結果,符合意料之中:

hello delete
changed in print
changed in get type
hello delete change
changed in get type
changed in get type
cat size
I am cat
i am a worm

那麼如何實現多型

C++中的多型是用虛擬函式實現的: 子類覆蓋父類的虛擬函式, 然後宣告一個指向子類物件的父類指標,聽起來有些拗口,實際上很簡單,操作一下

如Base *b = new Derive();
當呼叫b->f()時, 呼叫的是子類的Derive::f(),如果f()沒有被重寫,那麼結果是一樣的,如果被重寫了,那麼就會實現子類的功能。

用我上面的code示例:

animal *unkown = new cat();
unkown->Who();

輸出的結果是I am a cat,當有多個子類的時候,那麼就可以實現多中動物的描述,但是注意:這時候的父類指標只能呼叫父類內部有的成員,如果成員是在子類新建立的,那麼將無法呼叫。

比如我用unknow呼叫cat的Claw成員函式

 unkown->Claw();

編譯器直接報錯:

以上。

我是通過這些程式碼熟悉了上述三個名詞的關係,我覺的看再多還是要實際動手,當然我這只是入門級的理解,不過慢慢來,C++primer多看看,多寫程式碼,自然就理解深刻了,加油,C++的同志們