8.12 C++作业

运用虚函数实现多态

cpp 复制代码
#include <iostream>

using namespace std;

class Animal
{
public:
    virtual void perform()=0;
    virtual ~Animal()
    {
        cout  << "释放父类空间" <<endl;

    }
};

class Monkey:public Animal
{
public:
    void perform()
    {
        cout << "猴子正在骑自行车" << endl;
    }
    ~Monkey()
    {
        cout << "释放子类空间" << endl;
    }
};
class Elepant:public Animal
{
public:
    void perform()
    {
        cout << "大象正在滚皮球" << endl;
    }
};
class Lion:public Animal
{
public:
    void perform()
    {
        cout << "狮子正在跳火圈" << endl;
    }
};
int main()
{
    Animal *p;
    Monkey s1;
    p=&s1;
    p->perform();
    Lion s2;
    p=&s2;
    p->perform();
    Elepant s3;
    p=&s3;
    p->perform();

    cout << "Hello World!" << endl;
    return 0;
}

实现效果

相关推荐
好悬给我拽开线2 小时前
ZILOT 论文解析
算法
kloc2 小时前
聊天室项目入口网关子服务的实现
linux·c++
饮水桶_T3 小时前
JAVA IO流笔记
开发语言·数据结构·算法
星夜离尘564 小时前
MATLAB 离散数据分箱:半开区间、末端点与计数、加权和、平均
算法·matlab
Pniubi4 小时前
力扣76.最小覆盖子串
算法·leetcode·职场和发展
地平线开发者5 小时前
征程6|用 hrt\_model\_exec 看清 YOLOv5x 的输入、原始输出和后处理
人工智能·算法
csdn_aspnet5 小时前
C++ 3个有序点的方向(Orientation of 3 ordered points)
c++·算法
Felven5 小时前
C. Brr Brrr Patapim
c语言·开发语言
赤生云电竞6 小时前
GPU算力共享平台架构实践:任务分发与资源隔离的关键配置
算法·架构·gpu算力
小七在进步6 小时前
string的使用(2)
c++