Association and Aggregation
Association
Association is a has-a relationship between two classes. It represents a more general relationship where one class is connected to another but not necessarily in a strong way.
Example:
Suppose we have two classes, Teacher and Student , and there is an association between them
Below is the implementation of the above example:
C++
#include <iostream> #include <vector> class Student { public : Student(std::string name) : name(name) { } std::string getName() const { return name; } private : std::string name; }; class Teacher { public : Teacher(std::string name) : name(name) { } void addStudent( const Student& student) { students.push_back(student); } void listStudents() { std::cout << name << " teaches: " ; for ( const Student& student : students) { std::cout << student.getName() << " " ; } std::cout << std::endl; } private : std::string name; std::vector<Student> students; }; int main() { Teacher mathTeacher( "Mr. Smith" ); Student alice( "Alice" ); Student bob( "Bob" ); mathTeacher.addStudent(alice); mathTeacher.addStudent(bob); mathTeacher.listStudents(); return 0; } |
Mr. Smith teaches: Alice Bob
In this example:
- Teacher and Student are two classes representing a teacher and a student respectively.
- The Teacher class has a method addStudent to associate students with a teacher.
- An association is created by adding students to the teacher’s list.
Aggregation
Aggregation is a type of association that represnets a whole-part relationship. It implies that one one class (the whole) contians or is composed of another class (the part). Aggregation is often depicted as a diamond-shape arrow.
Let’s modify the previous example to demonstrate aggregation:
C++
#include <iostream> #include <vector> class Student { public : Student(std::string name) : name(name) { } std::string getName() const { return name; } private : std::string name; }; class School { public : School(std::string name) : name(name) { } void addStudent( const Student& student) { students.push_back(student); } void listStudents() { std::cout << "Students at " << name << ": " ; for ( const Student& student : students) { std::cout << student.getName() << " " ; } std::cout << std::endl; } private : std::string name; std::vector<Student> students; }; int main() { School mySchool( "XYZ School" ); Student alice( "Alice" ); Student bob( "Bob" ); mySchool.addStudent(alice); mySchool.addStudent(bob); mySchool.listStudents(); return 0; } |
Students at XYZ School: Alice Bob
In this updated example:
We introduced a School class, which contains a list of students. This represents an aggregation, where a school is composed of students.
Object Model | Object Oriented Analysis & Design
Object-Oriented Programming (OOP) is a fundamental paradigm in modern software development that has transformed the way we design, build, and maintain software systems. OOP is centered around the concept of objects, which are self-contained, reusable units that encapsulate both data and the operations that can be performed on that data. This approach mirrors real-world modeling, making it easier to understand, manage, and expand software projects.
Important Topics for the Object Model
- Objects and Classes
- Encapsulation and Data Hiding
- Message Passing
- Inheritance
- Polymorphism
- Generalization and Specialization
- Association and Aggregation
- Benefits of Object Model
- Conclusion