I'm trying to get the hang of Inheritance and Deep Copy but I'm having some trouble. I have 3 classes (1 Base and 2 Derived) as follows :
class Base {
protected:
int id;
public:
Base(int i) : id(i) {};
virtual ~Base();
};
class DeriveA : public Base {
int specialID;
public:
DeriveA(int s, int i) : Base(i), specialID(s) {};
~DeriveA();
};
class DeriveB : public Base {
int specialID;
public:
DeriveB(int s, int i) : Base(i), specialID(s) {};
~DeriveB();
};
On my main I have something like this:
int main() {
Base **Array;
int i, n;
Array = new Base*[5];
for (i = 0 ; i < 5 ; i++) {
n = rand() % 2;
if (n)
Array[i] = new DeriveA(i, n);
else
Array[i] = new DeriveB(i, n);
}
}
If specific circumstances are met I want to hard copy an array object over other objects. I find it hard to make a copy constructor for it since Array[0] = Array[2];
doesn't work for me. I don't want to use any vectors or std::copy because that's not my "educational" goal.
PS 1 : Is assignment operator better for this purpose since I have initialized all objects of array.
PS 2 : Since it's a generic code there are some errors that I left out. Please ignore them and focus on the question.