Composition vs Aggregation
Sometime complex objects are created using smaller & simpler objects. if we take a car; its compose of wheel, body, engine, steering etc.
The process of creating a complex object with the help of small objects is known as object composition.
1. Association
Association is a relationship where all objects have their own life cycle and there is no owner. There is no ownership between the objects and both have their own life-cycle. Both can create and delete independently.
2.Composition
In composition relationship, The main object is responsible for the life & death of its sub objects.
that means when main object is created, sub objects are created. when main object is deleted all its sub parts are deleted
Its also know as "is-a" relationship.
#include<iostream>
using namespace std;
class Enginee
{
int power;
int type;
public:
Enginee()
{
}
void SetEnginee(int p, int t)
{
power = p;
type = t;
}
int getpower()
{
return power;
}
int gettype()
{
return type;
}
};
class Tyre
{
double radious;
public:
Tyre()
{
}
void SetRadious(double d)
{
radious = d;
}
double GetRadious(double d)
{
return radious;
}
};
class Car {
Enginee enginee;
Tyre tyre;
public:
Car(int p, int t, double d)
{
enginee.SetEnginee(p, t);
tyre.SetRadious(d);
}
void ShowPower()
{
cout<<enginee.getpower();
}
void ShowType()
{
cout<<enginee.gettype();
}
void GetWheelRadious()
{
cout<<tyre.GetRadious();
}
};
int main()
{
Car car(1200, 1, 110.5);
return 0;
}

It is also known as "has-a" relationship
class Teacher {
string name;
public:
Teacher(string nameemp)
{
name = nameemp;
}
};
class dept
{
string dept_name;
Teacher *HOD;
public:
dept(string name, Teacher *t)
{
dept_name = name;
HOD = t;
}
};
int main()
{
Teacher *tech = new Teacher("Tom");
dept d("Math", tech);
return 0;
}
Here Teacher and dept are independent to each other. One can live without other. So its aggregation.
The process of creating a complex object with the help of small objects is known as object composition.
1. Association
Association is a relationship where all objects have their own life cycle and there is no owner. There is no ownership between the objects and both have their own life-cycle. Both can create and delete independently.
2.Composition
In composition relationship, The main object is responsible for the life & death of its sub objects.
that means when main object is created, sub objects are created. when main object is deleted all its sub parts are deleted
Its also know as "is-a" relationship.
#include<iostream>
using namespace std;
class Enginee
{
int power;
int type;
public:
Enginee()
{
}
void SetEnginee(int p, int t)
{
power = p;
type = t;
}
int getpower()
{
return power;
}
int gettype()
{
return type;
}
};
class Tyre
{
double radious;
public:
Tyre()
{
}
void SetRadious(double d)
{
radious = d;
}
double GetRadious(double d)
{
return radious;
}
};
class Car {
Enginee enginee;
Tyre tyre;
public:
Car(int p, int t, double d)
{
enginee.SetEnginee(p, t);
tyre.SetRadious(d);
}
void ShowPower()
{
cout<<enginee.getpower();
}
void ShowType()
{
cout<<enginee.gettype();
}
void GetWheelRadious()
{
cout<<tyre.GetRadious();
}
};
int main()
{
Car car(1200, 1, 110.5);
return 0;
}

3. Aggregation
Aggregation is a specialised form of compostions where all objects have their own lifecycle. i.e. life of sub objects are independent of its main objects. Sub objects can live without main objects.It is also known as "has-a" relationship
class Teacher {
string name;
public:
Teacher(string nameemp)
{
name = nameemp;
}
};
class dept
{
string dept_name;
Teacher *HOD;
public:
dept(string name, Teacher *t)
{
dept_name = name;
HOD = t;
}
};
int main()
{
Teacher *tech = new Teacher("Tom");
dept d("Math", tech);
return 0;
}
Here Teacher and dept are independent to each other. One can live without other. So its aggregation.

Comments
Post a Comment