孤世星辰 发表于 2022-3-19 21:10:27

一个抽象函数的问题

#include<iostream>
using std::cout,std::endl;
//CPU类
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_mem = mem;
        }

        void work()
        {
                m_cpu->calculate();
                m_vc->display();
                m_mem->storage();
        }
private:
        CPU* m_cpu; //CPU零件指针
        VideoCard* m_vc; //显卡内存指针
        Memory* m_mem; //内存条零件指针

};

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

};

class IntelVideoCard :public VideoCard
{
public:
        virtual void display()
        {
                cout << "Intel厂商的显卡开始显示了!" << endl;
        }

};

class IntelMemory :public Memory
{
public:
        virtual void distorage()
        {
                cout << "Intel厂商的内存条开始存储了了!" << endl;
        }

};

//Lenovo厂商
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 distorage()
        {
                cout << "Lenovo厂商的内存条开始存储了了!" << endl;
        }

};
void test01()
{
        //第一台电脑的零件
        CPU* intelCpu = new IntelCPU;
        VideoCard* intelCard = new IntelVideoCard;
        Memory* intelMem = new IntelMemory;        //无法实例化这个抽象函数

        //创建第一台电脑
        Computer* computer1 = new Computer(intelCpu,intelCard,intelMem);
        computer1->work();
        delete computer1;
};


int main() {
        test01();

        system("pause");
        return 0;
}

傻眼貓咪 发表于 2022-3-19 22:38:28

#include<iostream>
using std::cout, std::endl;
//CPU类
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_mem = mem;
    }

    void work()
    {
      m_cpu->calculate();
      m_vc->display();
      m_mem->storage();
    }
private:
    CPU* m_cpu; //CPU零件指针
    VideoCard* m_vc; //显卡内存指针
    Memory* m_mem; //内存条零件指针

};

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

};

class IntelVideoCard :public VideoCard
{
public:
    virtual void display()
    {
      cout << "Intel厂商的显卡开始显示了!" << endl;
    }

};

class IntelMemory :public Memory
{
public:
    virtual void storage() // <------------------------------------------ 注意这里 -----------------------------------------------------------
    {
      cout << "Intel厂商的内存条开始存储了了!" << endl;
    }

};

//Lenovo厂商
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 distorage()
    {
      cout << "Lenovo厂商的内存条开始存储了了!" << endl;
    }

};
void test01()
{
    //第一台电脑的零件
    CPU* intelCpu = new IntelCPU;
    VideoCard* intelCard = new IntelVideoCard;
    Memory* intelMem = new IntelMemory;      //无法实例化这个抽象函数

    //创建第一台电脑
    Computer* computer1 = new Computer(intelCpu, intelCard, intelMem);
    computer1->work();
    delete computer1;
};


int main() {
    test01();

    system("pause");
    return 0;
}

孤世星辰 发表于 2022-3-20 13:53:48

傻眼貓咪 发表于 2022-3-19 22:38


有什么问题嘛

傻眼貓咪 发表于 2022-3-20 14:11:47

孤世星辰 发表于 2022-3-20 13:53
有什么问题嘛

名字写错了,你上面写的类 class Memory 里面是 storage() 怎么到了下面变成 distorage() 了?
页: [1]
查看完整版本: 一个抽象函数的问题