I have just joined your website. I am attending B.Tech in Computer science and have completed upto inheritance in c++.
My first question here is: What is the relationship between inheritance and composition and why the two can be used in place of other.

Recommended Answers

All 6 Replies

This article was written for Java but applies to C++ too.

>What is the relationship between inheritance and composition
Inheritance is a special case of composition.

>why the two can be used in place of other
They solve the same problem and they're equally functional. If you're willing to suffer notational and structural inconveniences, inheritance can always be used in place of composition, and vice versa.

>> inheritance can always be used in place of composition, and vice versa.
Oh??? How can virtual functions be implenmented with composition?

>> inheritance can always be used in place of composition, and vice versa.
>Oh??? How can virtual functions be implenmented with composition?
You forgot to quote the first part of that sentence:

If you're willing to suffer notational and structural inconveniences, inheritance can always be used in place of composition, and vice versa.

I still didn't get it.Can anyone explain with an example?

Did you read the link I posted ????

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.