Object-Oriented Design (OOD), UML Class Diagrams & Architecture
Idea in plain words
Tiered Architecture: Separating UI / Presentation Layer (console menu), Business Logic Layer (domain models), and Data Access Layer (file storage).
Exam tip
High-yield exam topic (Very High).
Explanation
Design bridges abstract analysis to concrete C++ implementation. Applying the Single Responsibility Principle (SRP) ensures that individual classes handle specific tasks (e.g., FileStorageManager handles persistence, while Account handles domain business validation).
Key points
- UML Class Diagram: Standard diagrammatic notation showing class attributes (- private, # protected, + public), methods with signatures, and relationships.
- Relationships: Association (uses), Aggregation (weak has-a, hollow diamond), Composition (strong has-a lifecycle dependency, filled diamond), Generalization (is-a inheritance, hollow triangle).
- Sequence Diagrams: Temporal event flow between objects for primary operational workflows.
- File Structure Architecture: Designing record structures with fixed byte lengths to support direct indexing.
Example
Example
Composition vs Aggregation: A Car has a mandatory Engine (Composition - Car owns Engine's lifetime). A Department has Professors (Aggregation - Professors exist independently if Department dissolves).
Code examples
// UML Class Representation to C++ Header Translation:
// +-----------------------------------+
// | Account |
// +-----------------------------------+
// | - accountNumber : int |
// | # balance : double |
// +-----------------------------------+
// | + deposit(amount : double) : void |
// | + withdraw(amount : double) : bool|
// | + getBalance() : double |
// +-----------------------------------+
class Account {
private:
int accountNumber;
protected:
double balance;
public:
Account(int acc, double initBal) : accountNumber(acc), balance(initBal) {}
virtual void deposit(double amount) { balance += amount; }
virtual bool withdraw(double amount) = 0; // Pure virtual
double getBalance() const { return balance; }
virtual ~Account() {}
};
Exam questions
- [8 Marks] Draw a comprehensive UML Class Diagram for an Inventory Management System showing inheritance, aggregation, and visibility specifiers.
- [7 Marks] Explain the difference between Composition and Aggregation with UML notation and equivalent C++ code snippets.