C++多态案例3

 

#include <iostream>
#include <string>
void test();

using namespace std;

class CPU {
public:
    virtual void calculate() = 0;
};

class VideoCard {
public:
    virtual void display() = 0;
};

class Memory {
public:
    virtual void storage() = 0;

};

class Computer {
public:
    Computer(CPU *cpu, VideoCard *vc, Memory *mem) {
        m_cpu = cpu;
        m_vc = vc;
        m_memory = mem;
    }

    void work() {
        //让零件工作起来,调用接口
        m_cpu->calculate();
        m_vc->display();
        m_memory->storage();
    }

    //提供析构函数,释放3个电脑零件
    ~Computer() {
        if (m_cpu != NULL) {
            delete m_cpu;
            m_cpu = NULL;
        }
        if (m_vc != NULL) {
            delete m_vc;
            m_vc = NULL;
        }
        if (m_memory != NULL) {
            delete m_memory;
            m_memory = NULL;
        }
    }

private:
    CPU *m_cpu;//CPU的零件指针
    VideoCard *m_vc;//显卡零件指针
    Memory *m_memory;//内存条零件指针

};

//具体厂商
//Intel
class IntelCPU : public CPU {
public:
    void calculate() {
        cout << "Intel的CPU开始计算" << endl;
    }
};

class IntelVideoCard : public VideoCard {
public:
    void display() {
        cout << "intel的显卡开始显示" << endl;
    }
};

class IntelMemory : public Memory {
public:
    void storage() {
        cout << "intel的内存条开始存储" << endl;
    }
};

//联想厂商
class LenovoCPU : public CPU {
public:
    void calculate() {
        cout << "Lenovo的CPU开始计算" << endl;
    }
};

class LenovoVideoCard : public VideoCard {
public:
    void display() {
        cout << "Lenovo的显卡开始显示" << endl;
    }
};

class LenovoMemory : public Memory {
public:
    void storage() {
        cout << "Lenovo的内存条开始存储" << endl;
    }
};

//虚析构和纯虚析构
int main() {
    test();
    return 0;
}

void test() {
    CPU *intelCpu = new IntelCPU();
    VideoCard *intelVideoCard = new IntelVideoCard();
    Memory *intelMemory = new IntelMemory();
    //创建第一台电脑
    Computer *c = new Computer(intelCpu, intelVideoCard, intelMemory);
    c->work();
    delete c;
    cout << "------------------------" << endl;
    //创建第二台电脑
    Computer *c2 = new Computer(new LenovoCPU(), new LenovoVideoCard, new LenovoMemory);
    c2->work();
    delete c2;

    cout << "------------------------" << endl;
    //创建第二台电脑
    Computer *c3 = new Computer(new LenovoCPU(), new IntelVideoCard, new LenovoMemory);
    c3->work();
    delete c3;
}
Intel的CPU开始计算
intel的显卡开始显示
intel的内存条开始存储
------------------------
Lenovo的CPU开始计算
Lenovo的显卡开始显示
Lenovo的内存条开始存储
------------------------
Lenovo的CPU开始计算
intel的显卡开始显示
Lenovo的内存条开始存储

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是C++中面向对象编程的重要特性之一,它可以让我们在编写程序时更加灵活和易于扩展。下面我们来看一个简单的多案例。 假设我们有一个动物类(Animal),其中有一个虚函数makeSound(),用于输出动物的叫声。然后我们派生出几个动物子类(如狗Dog、猫Cat等),它们都重写了makeSound()函数输出自己的叫声。最后我们编写一个测试程序,用于创建不同的动物对象,并调用它们的makeSound()函数输出叫声。 下面是详细的代码及讲解: ```cpp #include <iostream> using namespace std; // 动物类 class Animal { public: virtual void makeSound() { cout << "动物叫声..." << endl; } }; // 狗类 class Dog : public Animal { public: virtual void makeSound() { cout << "汪汪汪..." << endl; } }; // 猫类 class Cat : public Animal { public: virtual void makeSound() { cout << "喵喵喵..." << endl; } }; // 测试函数 void test(Animal* animal) { animal->makeSound(); } // 主函数 int main() { Animal* animal1 = new Animal(); Animal* animal2 = new Dog(); Animal* animal3 = new Cat(); test(animal1); test(animal2); test(animal3); delete animal1; delete animal2; delete animal3; return 0; } ``` 上述代码中,Animal类是一个基类,我们将makeSound()函数声明为虚函数,表示它可以被子类重写。Dog和Cat类分别继承自Animal类,并重写了makeSound()函数,输出不同的叫声。 在测试函数test()中,我们将传入的动物指针参数强制转换为Animal类型,然后调用它们的makeSound()函数。这里需要注意的是,我们将动物指针参数声明为Animal*类型而不是Dog*或Cat*类型,这是因为在多中,我们通常使用基类指针或引用来指向派生类对象,这样可以让我们更加灵活地操作对象。 在主函数中,我们创建了三个动物对象,分别为基类对象、狗类对象和猫类对象,并将它们作为参数传递给test()函数进行测试。由于makeSound()函数被声明为虚函数,因此在调用test()函数时会根据传入的实际对象类型来动调用对应的函数,这就是多的体现。 最后需要注意的是,在程序结束时我们需要释放动分配的内存,以免出现内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值