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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / binay pandit

The answer given by the gurunath is perfectly correct. i
mean excellent.

Is This Answer Correct ?    27 Yes 7 No

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

Answer / mohan rathod

Perfect Answer.

Is This Answer Correct ?    2 Yes 1 No

Post New Answer

More OOPS Interview Questions

C#.net Interview Question A=10 B=5 C=A+B Print C The above will be given in a multiline textbox. You need to parse the above input, store values for A,B&c. And you have to display the value of C.

1 Answers   Syncfusion,


What is multilevel inheritance?

0 Answers  


WHAT IS THE DIFFERENCE BETWEEN OBJECT BASED & OBJECT ORIENTD PROGRAMMING LANGUAGE.(GIVE AT LIST 4 PIONT)

1 Answers   TCS,


what are the realtime excercises in C++?

0 Answers   IBM, Wipro,


Write a C++ program without using any loop (if, for, while etc) to print prime numbers from 1 to 100 and 100 to 1 (Do not use 200 print statements!!!)

0 Answers   HCL,






what is difference between String s=new String("vali"); String s="vali"

1 Answers  


What is the concept of object oriented program?

6 Answers  


what i oops concept, how many languages supports oops concept?

3 Answers   Value Labs,


What is overriding vs overloading?

0 Answers  


i hav very low percentage in 12th n BSCwhich is around 50.......coz it was just imposed on me 2 b a science graduate,nw m doin MCA n hav aggregate 74% ....what shud i answer if company's HR ask me about dis much low previous percentage??????

3 Answers   Infosys,


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...

0 Answers  


What is property in oops?

0 Answers  


Categories