C++的多态(polymorphism)

c++中的多态:

  1. 多态性为 多种行为
  2. 同样的方法调用而执行不同的行为、运行不同的代码。
  3. 多态通过分离做什么怎么做,从另一个角度将 接口实现 进行分离。
  4. 多态消除了 类型之间的 耦合 关系。

简单说明几个概念:

类的虚函数

  1. 类的成员函数中包含有 virtual 关键字的函数,例如:
class Animal{
protected:
    static int SoundCnt;
public:
    Animal();
    virtual  ~Animal();

    virtual void makeSound();
    virtual void AnimalRun() const;

    static int getSoundCnt() ;
};

类的纯虚函数

2.声明虚函数,并使虚函数 =0, 则构成纯虚函数,类中的纯虚函数可以有实现代码也可以没有实现代码

class Animal{
protected:
    static int SoundCnt;
public:
    Animal();
    virtual  ~Animal() = 0;

    virtual void makeSound()=0;
    virtual void AnimalRun() const;

    static int getSoundCnt() ;
};

抽象类

1.类的成员函数中至少包含一个 纯虚函数 的类,则为 abstract class(抽象类)

class Animal{
protected:
    static int SoundCnt;
public:
    Animal();
    virtual  ~Animal() = 0;

    virtual void makeSound()=0;
    virtual void AnimalRun() const;

    static int getSoundCnt() ;
};

接口类

1.类中的成员函数全部为 纯虚函 ,则为接口类

class Animal{
protected:
    static int SoundCnt;
public:
    Animal();
    virtual  ~Animal() = 0;

    virtual void makeSound()=0;
    virtual void AnimalRun() const = 0;

    virtual static int getSoundCnt()=0;
};

注意:

  1. 虚函数使得 子类 能够重新定义 基类 中的方法,通过声明基类 指针引用指向派生类,实现调用重载的 子类 中的方法,实现类的 多态 功能。

  2. 类中若包含 纯虚函数, 则该 类 不能创建实例对象 , 且该类为 抽象类

  3. 接口类是 C++ 实现多态的一个典型示例。

一个实现多态的示例 code:

头文件: polymorphism.h

#ifndef POLYMORPHISM_ANIMAL_H
#define POLYMORPHISM_ANIMAL_H
class Animal{
protected:
    static int SoundCnt;
public:
    Animal();
    virtual  ~Animal() = 0;

    virtual void makeSound();
    virtual void AnimalRun() const;

    static int getSoundCnt() ;
};

class Dog:public Animal{

public:
    Dog();
    ~Dog() override;
    void makeSound() override;
    void AnimalRun() const override;

};

class Cat:public Animal{

public:
    Cat();
    ~Cat() override;

    void makeSound() override;
    void AnimalRun() const override;
};
#endif //POLYMORPHISM_ANIMAL_H

头文件的实现: polymorphism.cpp

# include <iostream>
#include "animal.h"
using std::cout;
using std::endl;

int Animal::SoundCnt = 0;
Animal::Animal() {
    cout << "Animal constructor." << endl;
}

Animal::~Animal(){
    cout << "Animal deconstruct." << endl;
}

void Animal::makeSound() {
    cout << "Animal make sounding." << endl;
    ++SoundCnt;
}

void Animal::AnimalRun() const {
    cout << "Animal running." << endl;
}

int Animal::getSoundCnt() {
    cout << "Animal SoundCnt: \t";
    return SoundCnt;
}

// class Dog
Dog::Dog(){
    cout << "Dog constructor." << endl;
}
Dog::~Dog(){
    cout << "Dog deconstruct." << endl;
}

void Dog::makeSound() {
    cout << "Dog makes sound." << endl;
    ++SoundCnt;
}

void Dog::AnimalRun() const {
    cout << "Dog running." << endl;
}

// class Cat
Cat::Cat(){
    cout << "Cat constructor." << endl;
}
Cat::~Cat(){
    cout << "Cat deconstruct." << endl;
}

void Cat::makeSound() {
    cout << "Cat makes sound." << endl;
    ++SoundCnt;
}

void Cat::AnimalRun() const {
    cout << "Cat running." << endl;
}

测试代码,即 main.cpp

#include <iostream>
#include "animal.h"

using std::cout;
using std::endl;

// 按引用 传参,可以避免一次 拷贝赋值 操作,提高效率。
void func(Animal& val){
    val.makeSound();
    val.AnimalRun();
    cout << endl;
}

int getSoundTotal(Animal *p){
    return p->getSoundCnt();
}

int main(){

//    Animal pA;        // 若 Animal 中含 pure virtual function, 则 类Animal 不能创建实例对象, 此时 Animal 是一个抽象类
//    func(pA);

    Dog pG;
    func(pG);

    Cat pT;
    func(pT);

    cout << getSoundTotal(&pT) << endl;

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值