c-plus-plus
  1. c-plus-plus-aggregation

C++ Inheritance Aggregation

Inheritance aggregation is a way of achieving code reuse in C++. It is a type of association relationship where one class is a part of another class. It is also known as "has a" relationship. In this relationship, the class that contains the object of another class is called the composite class, and the class whose object is contained in another class is called the component class.

Syntax

The syntax for implementing inheritance aggregation in C++ is as follows:

class ComponentClass {
    // member variables and functions
};

class CompositeClass {
    ComponentClass componentObject;
    // other member variables and functions
};

Example

Let's take the example of a car and its engine. Here, the engine is a component of the car. We can demonstrate inheritance aggregation using these classes:

class Engine {
    int cubicCapacity;
public:
    void setCubicCapacity(int cc) {
        cubicCapacity = cc;
    }
    int getCubicCapacity() {
        return cubicCapacity;
    }
};

class Car {
    Engine carEngine;
public:
    void setEngine(Engine obj) {
        carEngine = obj;
    }
    Engine getEngine() {
        return carEngine;
    }
};

Output

The output of the above example can be obtained as follows:

int main() {
    Engine engineObj;
    engineObj.setCubicCapacity(1600);
    Car carObj;
    carObj.setEngine(engineObj);
    cout << "Engine of car has cubic capacity of: " << carObj.getEngine().getCubicCapacity();
    return 0;
}

The output of the above code will be:

Engine of car has cubic capacity of: 1600

Explanation

In the above example, we have two classes: Engine and Car. Engine is the component class, and Car is the composite class. The Car class contains an object of the Engine class. This is done by declaring a member variable of type Engine in the Car class.

We can create an object of the Engine class and set the cubic capacity of the engine using the setCubicCapacity method. We can then create an object of the Car class and set the Engine object using the setEngine method.

To get the engine's cubic capacity from the Car object, we can use the getEngine method to return the Engine object, and then use the getCubicCapacity method to get the cubic capacity of the engine.

Use

Inheritance aggregation is used when we want to create a relationship between two classes, where one class is a part of the other class. It is commonly used in software design to achieve code reuse.

Important Points

  • In inheritance aggregation, the composite class contains an object of the component class.
  • We can access the component class's methods and member variables through the composite class object.
  • Inheritance aggregation is a type of association relationship.

Summary

Inheritance aggregation is a way of achieving code reuse by creating a relationship between two classes, where one class is a part of the other class. It is commonly used in software design to represent real-world objects. Inheritance aggregation is implemented by declaring a member variable of the component class in the composite class and accessing it through a getter method.

Published on: