电脑组装的具体实现(多态)

#include<bits/stdc++.h>
using namespace std;
class CPU{
    public:
    virtual void caculate()=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_mem=mem; 
    }
    //工作函数
    void work(){
        m_cpu->caculate();
        m_vc->display();
        m_mem->storage();
    } 
    private:
    CPU *m_cpu;
    Videocard *m_vc;
    Memory *m_mem; 
}; 
//具体厂商
//INer
class InterCPU : public CPU{
public:
virtual void caculate(){
    cout<<"INter的CPU开始计算"<<endl; 
}    
}; 
class InterVideocard:public Videocard{
    public:
        virtual void display(){
            cout<<"Inter的显卡开始显示了"<<endl;
        }
};
class InterMemory:public Memory{
    public:
        virtual void storage(){
            cout<<"Inter的内存开始存储了"<<endl;
        }
};
class lenovoCPU:public CPU{
public:
virtual void calculate(){
    cout<<"lenovo的CPU开始计算"<<endl; 
}    
}; 
class lenovoVideocard:public Videocard{
    public:
        virtual void display(){
            cout<<"lenovo的显卡开始显示了"<<endl;
        }
};
class lenovoMemory:public Memory{
    public:
        virtual void storage(){
            cout<<"lenovo的内存开始存储了"<<endl;
        }
};
void test01(){
    CPU *interCpu=new InterCPU;
    Videocard *intercard=new InterVideocard;
    Memory *interMem=new InterMemory;
    //创建第一台电脑
    Computer *computer1=new Computer(interCpu,intercard,interMem) ;
    computer1->work();
    delete computer1;
}
int main(){
    test01();
    return 0;
}

 

posted @ 2023-05-04 14:12  艾鑫4646  阅读(8)  评论(0编辑  收藏  举报