Hello everyone,

I am trying to understand composition versus inheritance in C++. I have looked at many web pages, but I haven't found anything that makes sense to me. Can someone please tell me where I can find some examples of programs that might make sense to me? I am a beginner and I am looking for an example that is very "beginner friendly".

Thanks!

Inheritance could also be referred to as "specialization" or an "x is a y" relationship.
For example, a Cat is a "specialized" type of Mammal:
What are some things that define a Mammal? Two common ones are "isHairy" and "isWarmBlooded", among others.
What are some things that define a Cat? Among other things, "soundMade" is "meow".
This would be modeled as 2 classes, "Mammal" and "Cat". The "Cat" class would inherit, and have access to, the protected information in "isHairy" and "isWarmBlooded" from the "Mammal" class, plus it adds the value of "soundMade" to the object.

Composition is better referred to as an "x has a y" relationship.
For example, I just finished a program that simulates a dice game. In that program, the class "farkleGame" has within it instances of other classes called "playerSet", "dieSet", and "settings". The farkleGame object does not have direct access to the private/protected members of the other 3 classes and those members are not part of the farkleGame object. Those members remain parts of their own respective objects which are subsequently "contained" within the farkleGame object. It can only send commands to the public interfaces of the contained objects.

Hope that helps.

Inheritance could also be referred to as "specialization" or an "x is a y" relationship.
For example, a Cat is a "specialized" type of Mammal:
What are some things that define a Mammal? Two common ones are "isHairy" and "isWarmBlooded", among others.
What are some things that define a Cat? Among other things, "soundMade" is "meow".
This would be modeled as 2 classes, "Mammal" and "Cat". The "Cat" class would inherit, and have access to, the protected information in "isHairy" and "isWarmBlooded" from the "Mammal" class, plus it adds the value of "soundMade" to the object.

Composition is better referred to as an "x has a y" relationship.
For example, I just finished a program that simulates a dice game. In that program, the class "farkleGame" has within it instances of other classes called "playerSet", "dieSet", and "settings". The farkleGame object does not have direct access to the private/protected members of the other 3 classes and those members are not part of the farkleGame object. Those members remain parts of their own respective objects which are subsequently "contained" within the farkleGame object. It can only send commands to the public interfaces of the contained objects.

Hope that helps.

hey this is an excellent description of inheritance versus composition. 2 Thumbs up. Thx.

Hello everyone,

I am trying to understand composition versus inheritance in C++. I have looked at many web pages, but I haven't found anything that makes sense to me. Can someone please tell me where I can find some examples of programs that might make sense to me? I am a beginner and I am looking for an example that is very "beginner friendly".

Thanks!

Remember, prefer composition over inheritance. And usually, when
you inherit something, it can usually be composed in the class instead.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.