0

I am new to c++. Here's my code: which just performed three abstract classes and 6 derived classes. The code is simple and I have commented in essential parts. My question is presented in the code, where I want to implement the Copy constructor. And if there are bad practices in my code, please let me know so that I can improve. Thanks a lot!

#include <iostream>

using namespace std;

// -------  abstract classes---- //
class CPU {
public:
    virtual void run_cpu() = 0;
};

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

class GPU {
public:
    virtual void run_gpu() = 0;
};
// ----- concrete classes --- //
class AppleCpu : public CPU {
public:
    void run_cpu() override {
        cout << "Apple cpu is running" << endl;
    }

};

class AppleGpu : public GPU{
    void run_gpu() override {
        cout << "Apple gpu is running" << endl;
    }
};

class AppleMemory : public Memory{
    void run_memory() override {
        cout << "Apple Memory is running" << endl;
    }
};

class PearCpu : public CPU {
public:
    void run_cpu() override {
        cout << "pear cpu is running" << endl;
    }

};

class PearGpu : public GPU{
    void run_gpu() override {
        cout << "pear gpu is running" << endl;
    }
};

class PearMemory : public Memory{
    void run_memory() override {
        cout << "pear Memory is running" << endl;
    }
};
// ----  class Computer declaration --- //
class Computer {
public:
    Computer():_cpu{nullptr}, _memory{nullptr}, _gpu{nullptr}{}
    Computer(CPU *cpu, Memory *memory, GPU *GPU) : _cpu{cpu}, _memory{memory}, _gpu{GPU}{};
    Computer(Computer &other){
// My issue!!!!
        // I want a deep copy since the _cpu, _gpu, _memory are pointers
//        this->_cpu = new CPU...; this cannot be down since CPU,GPU,Memory are abstract
//         how can I get to know the type of "other's" constructor parameter?: say an apple or a pear one

    }
    ~Computer(){
        if(ver){
            delete _cpu;
            delete _memory;
            delete _gpu;
            cout<<"successfully dec"<<endl;
        }
    }
 
    void do_work();

private:
    CPU *_cpu;
    Memory *_memory;
    GPU *_gpu;
};
// --- implementation ---//



void Computer::do_work() {
        this->_cpu->run_cpu();
        this->_memory->run_memory();
        this->_gpu->run_gpu();
}



int main() {
    CPU * Cpu = new AppleCpu;
    GPU * Gpu = new PearGpu;
    Memory * mem = new AppleMemory;
    Computer* mix_pc = new Computer(Cpu,mem,Gpu);
    mix_pc->do_work();
    delete mix_pc;
}

billz
  • 11
  • 1
  • 1
    You probably want to implement something like a virtual `clone` function that each derived class overrides in order to create a new (dynamically allocated) copy of itself – UnholySheep Jun 15 '22 at 13:41
  • Can anyone who downvoted this explain the reason ? – limserhane Jun 15 '22 at 13:42
  • @UnholySheep Thanks for answering my question. However I don't know how to implement it and I did not clearly get what do you mean(sorry :x), Can you explain it with a small demo code? – billz Jun 15 '22 at 13:47
  • I didn't downvote, but my suggestions to improve this question would be to further cut down the amount of irrelevant code (do the `do_work` and `hardware_verify` etc. methods have anything to do with this question?) and do some basic research (there are many questions along the lines of "how do I copy an abstract class") – Nathan Pierson Jun 15 '22 at 13:47
  • The question can be posed with exactly **three** classes: one base and two derived. There's no need for most of that code. – Pete Becker Jun 15 '22 at 13:50
  • @NathanPierson Thanks a lot. Let me reduce the irrelevant code. – billz Jun 15 '22 at 13:50
  • [Polymorphic clones in modern C++](https://www.fluentcpp.com/2017/09/08/make-polymorphic-copy-modern-cpp/) for more indepth discussion. (But the link that UnholySheep shared should be sufficient to inspire a solution.) – Eljay Jun 15 '22 at 13:55

0 Answers0