• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1553
  • Last Modified:

implement aggregation and containment in C++

Can you give me examples of implementation of aggregation and also containment in C++?
0
ink777
Asked:
ink777
1 Solution
 
novitiateCommented:
class Driver
{
};
class Engine
{
};
class Car
{
    public:
    void SetDriver(Driver *d){_pDriver = d;}
    private:
    Engine _engine; //Composition
    Driver *_pDriver; //Aggregation
};

or

class Car
{
    public:
    Car()
    {
        _pengine = new Engine();
    }
    virtual ~Car()
    {
        delete _pengine;
    }
    void SetDriver(Driver *d){_pDriver = d;}
    private:
    Engine* _pengine; //Composition
    Driver *_pDriver; //Aggregation
};

Hope you got what I mean...
Composition: Object belongs to one and only one, Car has Engine
Aggregation: Object is not owned and can be shared by more than one. Car uses Driver

_novi_
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now