I'm converting some Java code to C++ and I want to keep the class structure similar. However, I have encountered the following problem, which I don't know how to solve; I do this in Java:
public class Mother {
protected Father make;
public Mother(){
make = maker();
make.print(); };
public Father maker(){
return new Father();};}
public class Daughter extends Mother {
public Daughter(){
super();}
@Override
public Father maker(){
return new Son();};}
public class Father {
public void print(){
System.out.println("I am the Father!\n");}}
public class Son extends Father {
@Override
public void print(){
System.out.println("I am the son!\n");};}
public static void main(String[] args) {
Daughter dot = new Daughter();
}
will produce: I am the son! While:
class father{
public:
virtual void print(){
std::cout << "I am the father!\n";
}; };
class son: public father{
public:
virtual void print(){
std::cout << "I am the son!\n";
};};
class mother{
protected:
father *make;
public:
mother(){
make = maker();
make->print();
};
virtual father *maker(){
return new father();
};};
class daughter: public mother{
public:
daughter(): mother() {
};
virtual father *maker(){
return new son();
};};
int main(int argc, const char * argv[]) {
daughter *d = new daughter();
will produce I am the father!. How can I make the C++ code to produce the same result as the Java code? Thanks.