简体   繁体   中英

A Request for Simple C++ Composition vs. Inheritance Examples

I am trying to understand the syntactic difference between composition and inheritance in C++.

I'm hoping someone will provide two simple examples. One example of a class that uses composition and one of a class that uses inheritance.

Sure, why not? Since I like robots, let's make a robot that can walk around and grab things. We'll make one robot using inheritance, and another robot using composition:

class Legs
{
public:
   void WalkAround() {... code for walking around goes here...}
};

class Arms
{
public:
   void GrabThings() {... code for grabbing things goes here...}
};

class InheritanceRobot : public Legs, public Arms
{
public:
   // WalkAround() and GrabThings() methods are implicitly
   // defined for this class since it inherited those
   // methods from its two superclasses
};

class CompositionRobot
{
public:
   void WalkAround() {legs.WalkAround();}
   void GrabThings() {arms.GrabThings();}

private:
   Legs legs;
   Arms arms;
};

Note that at least for this example, the CompositionRobot is usually considered to be the better approach, since inheritance implies an is-a relationship, and a robot isn't a particular kind of Arms and a robot isn't a particular kind of Legs (rather a robot has-arms and has-legs ).

To expand a little on @jeremy-friesner's answer (and mostly reuse his code), a lot of the time composition is implemented using more classes than that. Essentially the Legs and Arms classes would be implementations of an interface. This makes it easy to inject those dependencies and, hence, mock/stub them out when unit testing the composite object. Then you'd have something like (ignoring virtual destructor...) :

class Walker // interface
{
public:
    virtual void Walk() = 0;
}

class Legs : public Walker
{
public:
    void Walk() {... code for walking around goes here...}
}

class Grabber // Interface
{
public:
    virtual void GrabThings() = 0;
}

class Arms : public Grabber
{
public:
    void GrabThings() {... code for grabbing things goes here...}
}

class InheritanceRobot : public Legs, public Arms
{
public:
    // Walk() and GrabThings() methods are implicitly
    // defined for this class since it inherited those
    // methods from its two superclasses
};

class CompositionRobot
{
public:
    CompositionRobot(Walker& walker, Grabber& grabber) 
        : legs(walker), 
          arms(grabber) 
    {} 
    void Walk() {legs.Walk();}
    void GrabThings() {arms.GrabThings();}

private:
    Walker& legs;
    Grabber& arms;
};

So the actual implementation used for legs and arms could be set at run-time instead of compile time.

As an aside, I only wrote this as an answer, rather than a comment on Jeremy's answer, to benefit from the code formatting so, if you feel like up-voting it, please do Jeremy's too.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM