C++多态

目录

一 多态简介

一 虚函数 

二 纯虚函数


一 多态简介

多态时程序调用的多种形态。当类之间存在层次结构,并且类之间是通过继承关联时,就会用到多态。

多态意味着在调用成员函数时,会根据调用函数的对象的类型来执行不同的函数。

多态需要依赖虚函数才能实现,关于虚函数的概念见如下描述:

一 虚函数 

是在基类中使用关键字 virtual 声明的函数。在派生类中重新定义基类中定义的虚函数时,会告诉编译器不要静态链接到该函数。我们想要的是在程序中任意点可以根据所调用的对象类型来选择调用的函数,这种操作被称为动态链接,或后期绑定。

二 纯虚函数

在基类中定义虚函数,以便在派生类中重新定义该函数更好地适用于对象,但是您在基类中又不能对虚函数给出有意义的实现,这个时候就会用到纯虚函数。

总结:

  1. 当父类仅有常规函数和非纯虚函数时,子类继承过来的函数只要函数名称相同会覆写父类函数;
  2. 当父类有纯虚函数时,子类必须实现父类的纯虚函数;

示例代码:

基类animal

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_12_POLYMORPHIC_ANIMAL_H
#define CPP_12_POLYMORPHIC_ANIMAL_H

#include <iostream>
#include <string>

using namespace std;

//创建动物类: 作为狗类和猫类的基类
class Animal {
public:
    string name;
    string color;
    string food;
    Animal();
    Animal(string c,string f);
    Animal(string  n,string c,string f);

    //把run方法加上virtual关键字,代表这个方法是一个虚函数,父类可以有其方法的实现,
    //但是当使用多态时(父类对象先指向子类对象,然后在使用父类对象调用子类方法),子类的方法会覆盖父类方法,最终将调用子类方法。
    virtual void run();

    //申明一个纯虚函数: 只需申明,不需要定义,最终有继承的类实现
    virtual void ability()=0;

    void print();
};


#endif //CPP_12_POLYMORPHIC_ANIMAL_H



//
// Created by 11010 on 2023/4/9.
//

#include "Animal.h"

Animal::Animal() {

}

Animal::Animal(string c, string f):color(c),food(f) {

}

//动物具有跑的能力
void Animal::run() {
cout<<name+"具有跑的能力"<<endl;
}

Animal::Animal(string n, string c, string f):name(n),color(c),food(f) {

}

void Animal::print() {
 cout<<"name:"<<name<<", color:"<<color<<", food: "<<food<<endl;
}

子类dog

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_12_POLYMORPHIC_DOG_H
#define CPP_12_POLYMORPHIC_DOG_H
#include <iostream>
#include <string>
#include "Animal.h"

using namespace std;

//狗类,继承animal类
class dog: public Animal{
    string name;
    string color;
    string food;

public:
    dog();
    dog(string color,string food);
    dog(string name,string color,string food);

    void run();
    void ability() override; //实现父类的虚函数
};


#endif //CPP_12_POLYMORPHIC_DOG_H



//
// Created by 11010 on 2023/4/9.
//

#include "dog.h"
#include "Animal.h"

dog::dog() : Animal() {

}

dog::dog(string color, string food) : Animal(color, food) {
    this->color = color;
    this->food = food;
}

dog::dog(string name, string color, string food) : Animal(name, color, food) {
    this->name = name;
    this->color = color;
    this->food = food;
}

void dog::run() {
    cout << name + "具有跑的能力" << endl;
}

void dog::ability() {
    cout << name + "具有看家的能力" << endl;
}

子类cat

//
// Created by 11010 on 2023/4/9.
//

#ifndef CPP_12_POLYMORPHIC_CAT_H
#define CPP_12_POLYMORPHIC_CAT_H
#include <iostream>
#include <string>
#include "Animal.h"

using namespace std;

//猫类
class cat :public Animal{
    string name;
    string color;
    string food;

public:
    cat();
    cat(string color,string food);
    cat(string name,string color,string food);

    void run();
    void ability() override; //实现父类的虚函数
};


#endif //CPP_12_POLYMORPHIC_CAT_H





//
// Created by 11010 on 2023/4/9.
//

#include "cat.h"

cat::cat() :Animal(){

}

cat::cat(string color, string food) : Animal(color, food),
color(color),food(food) {

}

cat::cat(string name, string color, string food) :Animal(name, color, food),name(name),
color(color),food(food) {

}

void cat::run() {
    cout << name + "具有跑的能力" << endl;
}

void cat::ability() {
    cout << name + "具有捉老鼠的能力" << endl;
}

主函数

#include <iostream>
#include "Animal.h"
#include "dog.h"
#include "cat.h"
#include "string"
#include "iostream"

using namespace std;


int main() {

    //创建父对象
    Animal *animal = nullptr; //new Animal("动物", "五颜六色", "一切食物");
    //animal->run();
    //animal->print();


    //创建子对象
    dog d("小狗", "红色", "骨头");
    cat c("小猫", "灰色", "耗子");

    animal = &d;
    animal->run();
    animal->print();
    animal->ability();

    animal = &c;
    animal->run();
    animal->print();
    animal->ability();

    /*总结:
    1.当父类仅有常规函数和非纯虚函数时,子类继承过来的函数只要函数名称相同会覆写父类函数;
    2.当父类有纯虚函数时,子类必须实现父类的纯虚函数;*/

    return 0;
}

完整代码clone地址:git@gitcode.net:XiaoWang_csdn/cpp_12_polymorphic.git

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值