Understanding Dependency in C++: A Beginner's Guide to Class Relationships

Introduction

In Object-Oriented Programming (OOP), Dependency is the weakest type of relationship between two classes. It represents a "uses-a" relationship in which one class temporarily uses another class to perform a specific task without storing or owning it.

Unlike association, aggregation, or composition, a dependent class does not keep a permanent reference or pointer to the other class. Instead, the required object is passed as a function parameter, created locally inside a function, or used only for a short duration. Once the operation is complete, the dependency ends.

Dependency promotes loose coupling, making software easier to maintain, test, and modify because changes to one class have minimal impact on the other.

This document presents two practical examples demonstrating dependency relationships in C++.

Example 1: Student and Printer Relationship

This example demonstrates a dependency relationship where a student temporarily uses a printer to print an assignment. The student does not own or permanently store the printer object.

Program


Explanation

The program defines two independent classes: Student and Printer.

The Student class does not contain a Printer object as a data member. Instead, the printer is passed as a reference parameter to the submitAssignment() function whenever printing is required.

During execution, the student temporarily uses the printer to print the assignment. Once the function finishes, the dependency ends, and neither object stores any information about the other.

This demonstrates dependency because the student only uses the printer for a short period without establishing ownership or a permanent relationship.

Example 2: PaymentService and EmailService Relationship

This example demonstrates dependency between a payment service and an email service. The payment service temporarily uses the email service to send a payment confirmation.

Program


Explanation

The program defines two separate classes: PaymentService and EmailService.

The PaymentService class does not permanently store an email service object. Instead, an EmailService object is passed as a function parameter whenever a payment is processed.

Inside the processPayment() function, the payment is completed first, after which the email service is temporarily used to send a confirmation message.

Once the function execution finishes, the dependency ends. Both objects continue to exist independently, and neither object owns or manages the lifetime of the other.

This example demonstrates dependency because one class temporarily relies on another class to perform a specific task.

Characteristics of Dependency

PropertyDescription
Relationship TypeRepresents a temporary "uses-a" relationship.
OwnershipNo ownership exists between the objects.
Object LifecyclesCompletely independent. One object does not control the lifetime of the other.
ImplementationCommonly implemented by passing objects as function parameters or creating them locally inside functions.
CouplingVery loose coupling, making classes easier to modify and maintain.

Conclusion

Dependency is the weakest relationship between classes in Object-Oriented Programming. It allows one class to temporarily use the services of another class without storing or owning it. Since the relationship exists only during the execution of a specific function, dependency promotes loose coupling, improves code flexibility, and simplifies maintenance. As demonstrated in the Student–Printer and PaymentService–EmailService examples, dependency is commonly implemented in C++ by passing objects as function parameters whenever their functionality is required.