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;
}