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++的同志们

猜你喜欢

转载自blog.csdn.net/qq_31638535/article/details/83866854