If you want to construct an instance of a class as copy of a sibling class then I see two options:
Every derived class has to provide (copy?) constructors for each sibling class.
Every derived class has to provide a constructor which copies from their common super class.
The latter sounds better as it reduces the amount of code to be written (and probably causes less maintenance issues later).
Of course, the third option is whether derived classes are necessary at all.
However, I made a little sample for 2nd option:
#include <iostream>
#include <string>
class Pokemon {
private:
static int _idGen;
protected:
int _id;
public:
Pokemon(): _id(++_idGen) { }
Pokemon(const Pokemon&) = default;
virtual const char* what() const = 0;
};
int Pokemon::_idGen;
class Ditto: public Pokemon {
public:
Ditto(): Pokemon()
{
std::cout << "Ditto created. (ID: " << _id << ")\n";
}
Ditto(const Pokemon &other): Pokemon(other)
{
std::cout << "Ditto cloned from " << other.what() << " (ID: " << _id << ").\n";
}
virtual const char* what() const override { return "Ditto"; }
};
class Charamander: public Pokemon {
public:
Charamander(): Pokemon()
{
std::cout << "Charamander created. (ID: " << _id << ")\n";
}
Charamander(const Pokemon &other): Pokemon(other)
{
std::cout << "Charamander cloned from " << other.what() << " (ID: " << _id << ").\n";
}
virtual const char* what() const override { return "Charamander"; }
};
int main()
{
Charamander char1;
Ditto ditto1;
Charamander char2(ditto1);
Ditto ditto2(char1);
return 0;
}
Output:
Charamander created. (ID: 1)
Ditto created. (ID: 2)
Charamander cloned from Ditto (ID: 2).
Ditto cloned from Charamander (ID: 1).
Live Demo on coliru
Sorry, if I mixed the Pokemon's. Not that I know everything about this stuff except that there is this comic series (and all the merchandising around it)...
Regarding a free C++ book: You may find some by google c++ book online
. If you are in doubt about quality then google instead good c++ book online
...