Part-Whole Relationship in C++

Mannan Ul Haq
0

In object-oriented programming, the part-whole relationship refers to the relationship between objects where one object represents a larger entity that is composed of or associated with other objects. In C++, there are three main types of part-whole relationships: association, aggregation, and composition. Let's explore each of them in detail:


1. Association:

Association represents a relationship between two or more objects where they are loosely coupled and can exist independently. Think of association as a relationship between objects where they know about each other or can use each other. It's like two friends who know each other and can interact but don't have a strong dependency. They can exist independently and have their own lives.


Example:


class Car
{
public:

    void startEngine()
    {
        // Start the engine
    }
};

class Driver
{
public:

    void drive(Car* car)
    {
        // Drive the car
        car->startEngine();
    }
};

int main()
{
    Car car;
    Driver driver;

    driver.drive(&car);

    return 0;
}


In the above example, the `Driver` class is associated with the `Car` class. The `Driver` knows about the `Car` and can perform actions on it, such as driving. However, the `Driver` does not have ownership or control over the `Car`.


2. Aggregation:

Aggregation is a stronger relationship where one object is like a container that holds other objects. It's like a collection of things where the things can exist on their own. For example, a department can have many employees, but the employees can still exist even if the department doesn't exist. The container has ownership, but the things inside can exist independently.


Example:


class Car
{
public:

    void startEngine()
    {
        // Start the engine
        cout << "Engine started!" << endl;
    }
};

class Driver
{
private:
    Car* car;

public:
    Driver(Car* carPtr)
    {
        car = carPtr;
    }

    void drive()
    {
        // Drive the car
        car->startEngine();
    }
};

int main()
{
    Car car;
    Driver driver(&car);

    driver.drive();

    return 0;
}


In this example, the `Driver` class has an aggregation relationship with the `Car` class. The `Driver` class contains a pointer (`car`) to a `Car` object, indicating that it has a "has-a" relationship with the `Car` object. The `Driver` class can drive the car by calling the `startEngine()` function of the `Car` object.


3. Composition:

Composition is an even stronger relationship where one object is made up of other objects, and they cannot exist independently. It's like a whole made up of parts. For example, a house is composed of rooms. If you destroy the house, the rooms cease to exist. The parts belong to the whole and cannot exist on their own.


Example:


class House
{
private:
    Room bedroom;
    Room livingRoom;

public:
    House()
    {
        bedroom = "Bedroom";
        livingRoom = "Living Room";
    }
};

class Room
{
private:
    string name;

public:

    Room(const string& roomName)
    {
        name = roomName;
    }
};

int main()
{
    House house;

    return 0;
}


In this example, the `House` class has a composition relationship with the `Room` class. The `House` class contains member objects of the `Room` class (e.g., `bedroom` and `livingRoom`). The `House` object controls the creation and destruction of the rooms, and the rooms cannot exist independently of the house.


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !