Week 8
Week 8
Programming
Week – 8
BS CS 2nd Semester
Identification of classes and
their relationships
• Identifying classes and their relationships in C++ (or any object-
oriented programming language) is a crucial step in designing
software.
• This involves determining the main entities in your system, defining
their responsibilities, and establishing how they interact with each
other.
1. Identifying Classes
a. Understand the Problem Domain
Analyze the requirements: Understand what the software is supposed to do. Look
at the requirements or problem statement.
Identify key nouns: Nouns often indicate potential classes.
For example, in a banking application, nouns like "Account," "Customer," and
"Transaction" might suggest potential classes.
b. Look for Real-World Entities
Identify real-world objects that the software needs to model.
For instance, in a library management system, books, members, and librarians are
real-world entities that can be modeled as classes.
1. Identifying Classes (Contd.)
c. Group Related Attributes and Behaviors
Group related data and behaviors together.
For example, an "Account" class might have attributes like accountNumber and
balance and methods like deposit() and withdraw().
d. Avoid Excessive Granularity
Don’t create too many small, fine-grained classes for every little thing.
Aim for a balance where classes represent meaningful concepts in your system.
2. Establishing Relationships
Between Classes
•a. Association
Definition: A relationship where one class uses or interacts with another. It represents a "uses-a"
or "has-a" relationship.
Example: A Customer class might have an association with an Account class because a customer
has an account.
•b. Aggregation
Definition: A special form of association that represents a "whole-part" relationship, where the
part can exist independently of the whole.
Example: A Library class might aggregate a Book class. Books can exist independently of a
specific library.
2. Establishing Relationships
Between Classes
•c. Composition
Definition: A stronger form of aggregation where the part cannot exist independently of the
whole. This is a "contains-a" relationship with strict ownership.
Example: A Car class might have a Engine class. The engine cannot exist independently of the
car; if the car is destroyed, so is the engine.
•d. Inheritance
Definition: A relationship where one class derives from another, inheriting its attributes and
behaviors. This represents an "is-a" relationship.
Example: A SavingsAccount class might inherit from a base Account class because a savings
account is a type of account.
3. How to Identify Relationships
•Look for dependencies: Determine if a class relies on another class for some of its
operations. This could indicate an association.
•Check for ownership: If one class owns or is responsible for another class, this
may indicate aggregation or composition.
•Identify commonalities: If multiple classes share similar attributes or behaviors,
you might have an inheritance relationship.
•Review the problem domain: Revisit the problem requirements to ensure all
relationships make sense within the context.
Example Scenario
Consider a simple Library Management System:
1. Identify Classes:
o Book
o Member
o Librarian
o Library
2. Establish Relationships:
o Association: Member borrows a Book.
o Aggregation: Library contains Book.
o Composition: Library has a LibraryCatalog (if the catalog is destroyed when the library is destroyed).
o Inheritance: Librarian could inherit from a base class Person if there are shared attributes like name and address.