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
};
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
};
Subscribe to:
Post Comments (Atom)
0 Response to "composition in c++"
Post a Comment