Understanding Association in C++: A Beginner's Guide to Class Relationships
Introduction
In Object-Oriented Programming (OOP), classes often need to communicate and work together to accomplish a task. Association is one of the fundamental relationships between classes, where two independent objects interact with each other without establishing ownership.
Association represents a "uses-a" or "has-a" relationship in which one object uses another object to perform a specific operation. Unlike stronger relationships such as aggregation or composition, association does not imply ownership. Each object maintains its own independent lifecycle, meaning that creating or destroying one object has no effect on the existence of the other.
Association is commonly implemented in C++ using pointers or references, allowing objects to collaborate while remaining loosely coupled.
This document presents two practical examples demonstrating association relationships in C++.
Example 1: Doctor and Patient Relationship
This example demonstrates an association between a doctor and a patient. A doctor treats a patient, and a patient may visit a doctor, but neither object owns the other. Both objects continue to exist independently.
Program
Explanation
The program defines two independent classes: Doctor and Patient. Neither class owns the other, and both can exist independently.
The Doctor class provides a member function named treatPatient() that accepts a pointer to a Patient object. Passing a pointer establishes an association between the two objects during the function call.
Inside the main() function, separate objects of both classes are dynamically created. The doctor examines the patient by calling the treatPatient() function.
After the doctor object is deleted, the patient object continues to exist and can still be accessed. This demonstrates that association does not establish ownership and that the lifetime of one object does not depend on the other.
Example 2: Driver and Car Relationship
This example demonstrates a uses-a relationship between a driver and a car. The driver temporarily uses a car for driving, but both objects remain completely independent before and after the interaction.
Program
Explanation
The Car class represents a vehicle, while the Driver class represents a person capable of driving a vehicle.
The Driver class contains the member function driveCar(), which accepts a pointer to a Car object. The function establishes an association between the driver and the car only while the function executes.
Inside the main() function, separate objects of both classes are created on the stack. The driver uses the car by calling the driveCar() function, which starts the vehicle's engine.
After the function completes, both the driver and the car continue to exist independently with their own data members intact. Neither object controls the lifetime of the other, illustrating the loose coupling characteristic of association.
Characteristics of Association
| Property | Description |
|---|---|
| Relationship Type | Represents a "uses-a" or "has-a" relationship between independent objects. |
| Ownership | No ownership exists between the associated objects. |
| Object Lifecycles | Both objects have completely independent lifecycles. |
| Implementation | Commonly implemented using pointers or references passed between classes. |
| Coupling | Provides loose coupling between objects, making programs more flexible and maintainable. |
Conclusion
Association is a fundamental relationship in Object-Oriented Programming that enables independent objects to communicate and collaborate without establishing ownership. Unlike composition or aggregation, associated objects maintain completely separate lifecycles, allowing them to exist independently before, during, and after their interaction. As demonstrated in the Doctor–Patient and Driver–Car examples, association is commonly implemented in C++ using pointers or references, promoting loose coupling, flexibility, and reusable software design.