C++面向对象

Posted 爱吃香蕉的猴子0000

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++面向对象相关的知识,希望对你有一定的参考价值。

Hello, 大家好,我是爱吃香蕉的猴子, 写写c++的面向对象的例子


#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;

//抽象cpu类
class CPU
{
public:
    virtual void caculate() = 0;
};

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

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

//架构类
class Computer
{
public:
    Computer(CPU* cpu, Card* card, Memory* mem) {
        this->cpu = cpu;
        this->card = card;
        this->memory = mem;
    }

    void work() {
        this->cpu->caculate();
        this->card->display();
        this->memory->storage();
    }
private:
    CPU* cpu;
    Card* card;
    Memory* memory;
};


//实现层-----------------------------
class IntelCPU : public CPU
{
public:
    virtual void caculate() {
        cout << "Intel CPU 开始计算" << endl;
    }
};

class IntelCard :public Card 
{
public:
    virtual void display() {
        cout << "Intel Card 开始显示了" << endl;
    }
};

class IntelMem :public Memory {
public:
    virtual void storage() {
        cout << "Intel mem开始存储了" << endl;
    }
};

class NvidiaCard :public Card 
{
public:
    virtual void display() {
        cout << "Nvidia显卡开始显示了" << endl;
    }
};

class KingstonMem :public Memory 
{
public:
    virtual void storage() {
        cout << "KingstomMem 开始存储了" << endl;
    }
};

//业务层
int main()
{
    std::cout << "Hello World!\\n";

    CPU* intelCpu = new IntelCPU;
    Card* intelCard = new IntelCard;
    Memory* intelMemory = new IntelMem;

    Computer* com = new Computer(intelCpu, intelCard, intelMemory);
    com->work();
    return 0;
}

                                    Code的搬运工V1.0

以上是关于C++面向对象的主要内容,如果未能解决你的问题,请参考以下文章

c++面向对象程序设计心得

C++作为面向对象语言的三个基本特征:封装,继承和————

C++:面向对象设计的继承性的相关编程代码讲解

C++之面向对象的三个基本特征

C++ 面向对象的三大特征

面向对象简介