简体   繁体   中英

Inheritance vs Composition:: Is my understanding correct?

In composition, one class explicitly contains the other. However in Inheritance, the base class is implicitly contained in the derived class. Correct or not? I ask this because after several days of studying inheritance, it's only today that I got to read somewhere that an object of a derived class always contains an object of it's base class.

I mean, I thought that there would be only one object and just the functionality would be inherited but I didn't know that it would also contain an object of the base class within.

In Composition, one object contained another object. While in inheritance, your object is acquire properties of base class.

I mean, I thought that there would be only one object and just the functionality would be inherited but I didn't know that it would also contain an object of the base class within.

Yes you are right, there will be only one object and functionality is getting inherited. Even if your base class have member variables, there size will getting added to your object size.

You can directly call public and protected methods of base class. While in cointainership you are only able to access public methods.

That's should be: In composition, one class explicitly contains an object of the other class. However in Inheritance, the base class is implicitly contained in the derived class.

In short: Composition is about the relationship of class and object. Inheritance is about the relationship of class and class.

And please remember "Prefer composition over inheritance". Prefer composition over inheritance?

Consider the code:

class Engine
{
//Some Code
};

class Vehicle
{
//Some Code
};

class Car:Vehicle
{
    Engine engine;
    //Some Code
};

In this case class Car inherits the class Vehicle. An object of the class Car doesn't contain an object Vehicle, rather it is an object of the class Vehicle (Inheritance). On the other hand it does contain an object of the class Engine(Composition).

The fact that you can access a parent's function with this comes from the fact that the Car object is a Vehicle not because it contains an Vehicle object.

In general derived class contains all data members and shares the properties/methods of base class, but there is a difference between composition and inheritance.

By "composition" you mean that one object "has" some other object. In example: human has a liver. In class design it can be presented like below:

class Liver {};

class Human
{
public:
  Human() {}
private:
  Liver mLiver;
}

When talking about an inheritance, there are 2 options: public inheritance roughly says that one object "is" a kind of other object. In example: Human is a kind of living creature. It does not sound naturally to say that human "has" a living creature inside. Public inheritance is a way to go in such case:

class LivingCreature {};

class Human : public LivingCreature
{
public:
  Human() {}
}

Other option is protected/private inheritance, which should be used to implement some object "in terms of" other object. Generally it can also be treated as kind of composition, but first approach is usually better.

Summarizing:

  • If you can say that one object "is" a kind of other, more general object: public inheritance is the best way to go,
  • If you can say that one object "has" other object: use composition.

In composition, one class explicitly contains the other. However in Inheritance, the base class is implicitly contained in the derived class. Correct or not?

It's entirely a matter of knowledge/perspective: if you're aware that inheritance means a base class instance will be embedded in the derived class then saying class Dervived : Base can be seen as explicitly requesting that, while if you're aware that defining a variable inside class X means it's a member variable that will be contained in instances of X , then that can be seen as explicit too.

I ask this because after several days of studying inheritance, it's only today that I got to read somewhere that an object of a derived class always contains an object of it's [sic] base class.

The distinction between actually containing a base class object vs. through some more unspecified/mysterious means being substitutable for a base class instance on occasion, isn't necessarily the most important thing when starting to learn about inheritance, so it's easy to imagine it isn't emphasised in all learning material.

I mean, I thought that there would be only one object and just the functionality would be inherited but I didn't know that it would also contain an object of the base class within.

At an implementation level, it's important that it actually contains a base class instance, so code compiled to handle base class objects can work equally well on derived class instances. The C++ Standard could have deemed it merely an embedded copy of base class content with identical binary layout while not an actual base class object, but then a huge amount of text in the Standard would have to be added to mention that the derived objects could be used in scenarios where a base class instance was acceptable. In other words, the distinction is somewhat arbitrary, but it's easier for everyone if it's both intuitive and lends itself naturally to simpler, more concise Standard wording.

Inheritance vs Composition:: Is my understanding correct?

Conceptual differences:

Inheritance:

In case of inheritance, derived class is sub-type of base class. Meaning if you derive Dog from Animal , then Dog is Animal and all* operations that can be performed on Animal can be performed on Dog .

  • Using private , protected and public inheritance, however, you can control who knows that Dog is Animal and who knows inner workings of Animal . In case of protected or private inheritance only Dog will know that it is Animal , but it won't be obvious from the outside.

Composition:

In case of composition one class is included into another. a Car is not a Wheel . But it contains Wheel . So operations that work on Wheel will not work on a Car .

By declaring member variable of type Wheel as public , private or protected you can control who can access Car 's Wheel s.

I believe that is clear enough?

Implementation details:

In case of C++, members of base class are included into derived class. Also methods that existed in base class will be accessible in derived class - somewhere. Access specifiers private , public and protected AND inheritance type determine which methods are visible and where.

I thought that there would be only one object

It is one object.

In microsoft compiler and g++ objects are "merged" together, meaning that in case of:

struct Base{
    int a;
};

strict Derived: public Base{
    int b;
};

Derived internally will probably (would need to check C++ standard to be sure) have this layout.

strict Derived{
    int a;
    int c;
};

or

struct Derived{
    Base _;
    int c;
};

In case of multiple inheritance and diamond inheritance things will get more complicated and base class can be included multiple times.

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