Explain Composition in C++ with an example

October 28, 2007 · Filed Under C++ Questions, Placement Questions 



Don't want to miss a single bit? Subscribe By Email for Daily Jobs

Enter your email address :

All the latest from JobsAdda on Jobsadda.com delivered to your mailbox everyday.

Composition allows software to be developed by assembling existing components rather than creating new ones. Composition is also sometimes called as aggregation and defines the process of putting an object inside another object. It models the has-a relationship. E.g. a class employee can contain an object of type salary which itself is another type of object. e.g.

class Salary
{
private:
double DA;
double HRA;
double travelAllowance;
double basic salary;
public:
//member functions
};
Class Employee
{
private:
int empno;
Salary salary; //composition: salary object is contained inside employee
public:
//member functions
};

Related Articles


Get Latest JobsAdda.com Jobs, news & updates  via Email