• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1557
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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