1) Could someone explain the difference between composite and inheritance class

2) Is there a composite class with inheritance?

8 Years
Discussion Span
Last Post by Alex Edwards

I am assuming that you have googled this and are confused, so I will take a stab at explaining it but feel free to ask questions.

Composition is to contain. An example is easiest

class Wheelbarrow
    Cement A;
    Water B;
    Sand C;

The class contains stuff. In this case material, but it can be anything.
The point is it contains objects/number or other things that the class can use.

Inheritance is to be the same type as. An example

class Car
{. };

class OffroadCar : public Car
{ };

Anything a general car can do, is also done by OffroadCar BUT some things (normally functions) are specialized and some things can be added. e.g.

Car & Offroad car both might need double showSpeed() but Offroad car might have a bool isStuckinMud();


Does it mean that i can only have either 1 composition or 1 inheritance and not both at the same time?


No you can have a bit of both. In C++ you can have multiple inheritance
e.g. class A : public B,public C and class A could contain any number of things... e.g.

class A : public B,public C
     D Item;
     E array[5];
     F* ArrPtr;

In the last example class A inherits from B and C but contains a single instance of D, 5 instances of E and a pointer to an F or some group of F
(exactly which is to be determined by later code).

The only problem you will get, is that the phrase "composite class" is often used to imply that the main function of the class is as a container. It does not preclude inheritance or complex internal functions but in discussions the intent of the phrase is that the class contains and manages stuff.
It is sometimes used in a verb form e.g. "class X is a composite of Y and Z", meaning that X contains one or many instances of Y and Z.


Let's say i have a composition of classes. Does it refer to the same as inheritance class?


1) My Answer: Composition ( I believe you meant to say Composition ? ) is a form of delegation in which instances of a class use, but may not have the same interface of a target object.

The major benefit of Composition? There are many. Let's say you need to use a trait of a particular class but you don't want your class to be an extension of the class (there are many reasons one wouldn't want inheritance - constructor overhead, different 'base class' paths, to have methods defined for the objects main purpose instead of methods inherited from an existing class, etc), then you can use Composition to accomplish this feat --

class Task{};

class Program{

    Task myTask;    // Program HAS-A Task instance (Composition)


Now let's say you need existing methods from another class and you need to build upon that class or redefine the way the methods work. In addition, you want your class to be treated similarly as the class you are extending from. This would be a form of Inheritance --

class Task2{};

 * Program2 inherits from Task2
class Program2 : public Task2{



2) My Answer: Yes you can use both Composition and Inheritance for the same object. This is something one would call a "Proxy Class" because your class has the same information as the object that the class is using. You can use Proxy classes as a means of protecting the target class that is encapsulated and only accessing information from it when conditions are appropriate.

Note: Inlined definition(s) for convenience--

class Notepad{

        virtual void writeData(){ cout << "Writing..." << endl;}
        virtual void deleteData(){cout << "Deleting..." << endl;}

class Proxypad : public Notepad{

    Notepad np;
    char mask;
        Proxypad() : mask(0) {}
        void setState(char x){mask |= x;}
        void deleteState(char x){ mask &= ~x;}
        virtual void writeData(){
            if(mask & 0){
                cout << "Proxy Object is in sleeping state" << endl;
        virtual void deleteData(){
            if(mask & 0){
                cout << "Proxy Object is in sleeping state" << endl;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.