Composition: It is relationship between two classes, where one class is parent and other is child. It is represented by filled solid diamond, parent side owns the diamond side. It has two properties:
- It has ownership (It is parent and child relationship), child cannot belong to other parent.
- Life of child depends on parent life (If parent dies then child will also die).
Example:
- Building has rooms
- Chess board has square blocks
- Hand has fingers
- Car has engine
- Dog has tail
Many more example but all will follow above 2 properties (child cannot belong to other parent and has parent life).
Note: child cannot belong to other parent, it means “third person or class cannot access the child directly, but can access via parent”. Example (Building has rooms) visitor cannot access room directly, they need to go via building class(parent of room class).
Technically: Parent class has child object, it will be created inside parent constructor and will be destroyed inside destructor.
class Book { std::string mBookName; Page* pPage; public: Book() { pPage = new Page; std::cout<<"Book Contructor"<<std::endl;}; ~Book() { if(pPage) { delete pPage; pPage = nullptr; } std::cout<<"Book Destructor"<<std::endl;}; };
===================================================================
Aggregation: It is relationship between two classes, where one class is parent and other is child. It is represented by hollow solid diamond, parent side owns the diamond side. It has two properties:
- It has ownership (It is parent and child relationship), child can not belong to other parent.
- Life of child does not depends on parent life, child has own life (If parent dies then child will NOT die).
Example:
- Library has books
- Collage has teachers
- Hand has bracelet
- Car has tyres
- Dog has neck belt
Many more example but all will follow above 2 properties (child cannot belong to other parent and it has own life).
Note: child cannot belong to other parent, it means “third person or class cannot access the child directly, but can access via parent”. Example (Library has books) student or teacher cannot access books directly, they need to go via library class(parent of book class).
Technically: Parent class has child pointer or reference, it will be created outside of parent, other classes cannot access child directly but they need to access via parent.
class Library { Book* mpBook; public: Library(Book* p) { std::cout<<"Library Contructor"<<std::endl; mpBook = p;} ~Library() { std::cout<<"Library Destructor"<<std::endl;}; Book* GetBook() { return mpBook;}; };
======================================================================
Association: It is relationship between two classes, where two classes know each other. It is represented by plain arrow head line, One class or both can call API of each other. It has two properties:
- It has NO ownership.
- Both classes have own life.
Example: (Both classes know each other, can call each other API whenever required)
- Teacher and student
- Hand and notebook
- Car and Car Wash Center
- Dog and Neighbour
Many more example but all will follow above 2 properties (NO ownership and both have own life).
Technically: If one class wants to talk to another class then using another class pointer or reference, they can talk.
class Student { Library* lib; public: Student(Library* l):lib(l) { std::cout<<"Student Constructor"<<std::endl;} ~Student() {std::cout<<"Student Destructor"<<std::endl;} void getBook() { lib->GetBook()->GetBookName(); return; } };
Note: Using pointer or reference of a class in another class, this relation could be association or aggregation, then we need to check the ownership. If there is ownership then they are related by aggregation.
Many person have below opinions:
- If one class has pointer or reference of other class then its association (I don’t agree of this, I think that It could be association or aggregation).
- If one class has vector<object> of other class the its aggregation (I don’t agree of this because if we have more than one object then we need to use some container, How it is related to aggregation. I think that it could be association or aggregation)
Difference between association, aggregation and composition:
Properties | Association | Aggregation | Composition |
Ownership | No Owner | Yes | Yes |
Life | Own Life | Own Life | Parent Life |
Class Diagram:
source code with example:
/* Program: association vs aggregation vs composition Author: Alpha Master Date: 28 March 2021 */ //Header File #include<iostream> #include<memory> class Page { public: Page(){std::cout<<"Page Contructor"<<std::endl;} ~Page(){std::cout<<"Page Destructor"<<std::endl;} }; class Book { std::string mBookName; Page* pPage; public: Book() { pPage = new Page; std::cout<<"Book Contructor"<<std::endl;}; ~Book() { if(pPage) { delete pPage; pPage = nullptr; } std::cout<<"Book Destructor"<<std::endl;}; void GetBookName() { std::cout<<"Name: DigestCPP Book"<<std::endl;}; }; class Library { Book* mpBook; public: Library(Book* p) { std::cout<<"Library Contructor"<<std::endl; mpBook = p;} ~Library() { std::cout<<"Library Destructor"<<std::endl;}; Book* GetBook() { return mpBook;}; }; class Student { Library* lib; public: Student(Library* l):lib(l) { std::cout<<"Student Constructor"<<std::endl;} ~Student() {std::cout<<"Student Destructor"<<std::endl;} void getBook() { lib->GetBook()->GetBookName(); return; } }; class Teacher { Library* lib; public: Teacher(Library* l):lib(l) { std::cout<<"Teacher Constructor"<<std::endl;} ~Teacher() {std::cout<<"Teacher Destructor"<<std::endl;} void getBook() { lib->GetBook()->GetBookName(); return; } }; int main() { std::cout<<"association vs aggregation vs composition"<<std::endl; //Book instance //Book can belong to library only so cannot belong to other parent. //As per aggregation,it should not be used by other classes directly. Book bookObj; //Library instance, its not bound with ownership //so it can be used with other classes like student or teacher. Library libObj(&bookObj); //Student Student stuObj(&libObj); stuObj.getBook(); //Teacher Teacher teaObj(&libObj); teaObj.getBook(); std::cout<<"END"<<std::endl; return 0; }
Output:
association vs aggregation vs composition Page Constructor Book Constructor Library Constructor Student Constructor Name: DigestCPP Book Teacher Constructor Name: DigestCPP Book END Teacher Destructor Student Destructor Library Destructor Page Destructor Book Destructor