I would expect the following code to print out:
"This is a Leg", because in my Lion
class, it contains a private variable pointing to the Leg
class.
#include <iostream>
using namespace std;
class Limb{
public:
std::string getFullName() const{
auto name = getName();
return "This is a " + name;
}
private:
virtual std::string getName() const{
return "Limb!";
}
};
class Leg: public Limb {
private:
std::string getName() const override{
return "Leg!"; //This is overriding the parent class's getName
}
};
class Animal{
public:
Animal():x_(){}
Limb getLimb() const{
return x_; // This should return a Leg instance when running under `Lion`, right?
}
std::string getLimbName() const{
return getLimb().getFullName();
}
private:
Limb x_;
};
class Lion: public Animal{
public:
Lion():x_(){}
private:
Leg x_; //I explicitly want a Leg instance, not an Animal.
};
int main()
{
Lion l = Lion();
std::cout<<l.getLimbName()<<std::endl;
return 0;
}