Say you had two classes:
method crawl() uses the same implementation in both classes. How would you 'factor out' method crawl into a base class and have the relationship make sense? I can only think of the following solutions, none of which seem very good to me.Code:class Adult { public: void crawl() {...} void run() {...} } class Baby { public: void crawl() {...} }
1. I could have Adult inherit from Baby (deleting Adult's crawl() implementation), but then i would be saying that an Adult 'is-a' Baby.
2. I could compose a Baby object in Adult so as to re-use method crawl(). But again, this would imply that an Adult is composed of a Baby, which doesn't make a whole lot of sense.
3. I could have a Human base class with a concrete implementation of crawl() which both Adult and Baby extend from. However, Baby would be completely empty.
4. I could have a Human base class with a concrete implementation of method crawl(), which both Adult and Baby compose. In this case, Baby would have a crawl() method that simply calls crawl() in Human.
Is there a better way?