How Do you Code Composition and Aggregation in C++ ?

Answer Posted / lekshmi

An aggregation is a specific type of composition where no
ownership between the complex object and the subobjects is
implied. When an aggregate is destroyed, the subobjects are
not destroyed.

For example, consider the math department of a school,
which is made up of one or more teachers. Because the
department does not own the teachers (they merely work
there), the department should be an aggregate. When the
department is destroyed, the teachers should still exist
independently (they can go get jobs in other departments).

Because aggregations are just a special type of
compositions, they are implemented almost identically, and
the difference between them is mostly semantic. In a
composition, we typically add our subclasses to the
composition using either normal variables or pointers where
the allocation and deallocation process is handled by the
composition class.

Let’s take a look at our Teacher and Department example in
more detail.

view plaincopy to clipboardprint?

#include <string>
using namespace std;

class Teacher
{
private:
string m_strName;
public:
Teacher(string strName)
: m_strName(strName)
{
}

string GetName() { return m_strName; }
};

class Department
{
private:
Teacher *m_pcTeacher; // This dept holds only one
teacher

public:
Department(Teacher *pcTeacher=NULL)
: m_pcTeacher(pcTeacher)
{
}
};

int main()
{
// Create a teacher outside the scope of the
Department
Teacher *pTeacher = new Teacher("Bob"); // create a
teacher
{
// Create a department and use the constructor
parameter to pass
// the teacher to it.
Department cDept(pTeacher);

} // cDept goes out of scope here and is destroyed

// pTeacher still exists here because cDept did not
destroy it
delete pTeacher;
}

#include <string>
using namespace std;

class Teacher
{
private:
string m_strName;
public:
Teacher(string strName)
: m_strName(strName)
{
}

string GetName() { return m_strName; }
};

class Department
{
private:
Teacher *m_pcTeacher; // This dept holds only one
teacher

public:
Department(Teacher *pcTeacher=NULL)
: m_pcTeacher(pcTeacher)
{
}
};

int main()
{
// Create a teacher outside the scope of the Department
Teacher *pTeacher = new Teacher("Bob"); // create a
teacher
{
// Create a department and use the constructor
parameter to pass
// the teacher to it.
Department cDept(pTeacher);

} // cDept goes out of scope here and is destroyed

// pTeacher still exists here because cDept did not
destroy it
delete pTeacher;
}
In this case, pTeacher is created independetly of cDept,
and then passed into cDept’s constructor. Note that the
department class uses an initialization list to set the
value of m_pcTeacher to the pTeacher value we passed in.
When cDept is destroyed, the m_pcTeacher pointer destroyed,
but pTeacher is not deallocated, so it still exists until
it is independently destroyed.

To summarize the differences between composition and
aggregation:

Compositions:

Typically use normal member variables
Can use pointer values if the composition class
automatically handles allocation/deallocation
Responsible for creation/destruction of subclasses
Aggregations:

Typically use pointer variables that point to an object
that lives outside the scope of the aggregate class
Can use reference values that point to an object that lives
outside the scope of the aggregate class
Not responsible for creating/destroying subclasses
It is worth noting that the concepts of composition and
aggregation are not mutually exclusive, and can be mixed
freely within the same class. It is entirely possible to
write a class that is responsible for the
creation/destruction of some subclasses but not others. For
example, our Department class could have a name and a
teacher. The name would probably be added to the department
by composition, and would be created and destroyed with the
department. On the other hand, the teacher would be added
to the department by aggregate, and created/destroyed
independently.

It is also possible to create other hybrid
aggregate/composition schemes, such as where a class holds
independent subobjects like an aggregate, but will destroy
them when the class goes out of scope like a composition.

While aggregates can be extremely useful (which we will see
more of in the next lesson on container classes), they are
also potentially dangerous. As noted several times,
aggregates are not responsible for deallocating their
subobjects when they are destroyed. Consequently, if there
are no other pointers or references to those subobjects
when the aggregate is destroyed, those subobjects will
cause a memory leak. It is up to the programmer to ensure
that this does not happen. This is generally handled by
ensuring other pointers or references to those subobjects
exist when the aggregate is destroyed

Is This Answer Correct ?    43 Yes 12 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is encapsulation process?

575


What is the purpose of polymorphism?

675


What is oops in simple words?

577


What is the point of polymorphism?

584


What does <> mean pseudocode?

620






I have One image (means a group photo ) how to split the faces only from the image?............ please send the answer nagadurgaraju@gmail.com thanks in advace...

1622


What is polymorphism explain?

684


What is the fundamental idea of oop?

634


How is polymorphism achieved?

583


What is the full form of oops?

607


What is the benefit of oop?

570


What is coupling in oop?

592


What is the main feature of oop?

618


What is polymorphism what are the different types of polymorphism?

561


Why do we need polymorphism in c#?

684