Computer Programming

J Mwaura

Lecture 4: Relationships Among Classes

Computer Programming

J Mwaura

Class Relationships

Inheritance

Association

Dependency

Inheritance

The 2nd principle of Object Oriented Programming

We use Unified Modeling Language (UML) to shows the relationship between classes & objects graphically

Base class or superclass - the general class

Derived class or subclass - a specific class

A derived class extends its base class;

  • Derived class inherits all of the data members & member functions of the base class (with the exception of constructors, destructor, & assignment operators that need to be redefined)
  • It can create new data members & member functions

Inheritance

UML diagram for inheritance

Inheritance

Types of inheritance in C++;

  • Private inheritance (default type)
  • Protected inheritance
  • Public inheritance

Use (:) after a base class (e.g. B-base & D-derived class)

Inheritance

Types of inheritance

Public Inheritance

A private member in the base class becomes an inaccessible (hidden) member in the derived class

A public member in the base class becomes a public member in the derived class

Public inheritance is the most common. e.g.

  • Class Student inherits from the class Person

Protected Inheritance

Protected inheritance is rare and virtually never used

Private inheritance does not define a type inheritance; it defines an implementation inheritance

Private Inheritance

Public members of the base class become private members in the derived class

This property allows inherited implementation (code reuse)

Inheritance

Inheritance

Forms of inheritance

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid Inheritance (also known as Virtual Inheritance)

Forms of Inheritance

Forms of Inheritance

Association

Not all relationships between classes can be defined as inheritance

it models the has-a relationship

An association between two classes shows a relationship. For example, we can define a class named Person and another class named Address

A person lives in an address and the address is occupied by a person. The Address class is not inherited from the Person class; neither the other way

Types of Association

1. Aggregation

  • An aggregation is a one-to-may relationship from the aggregator to the aggregatee
  • In an aggregation, the lifetime of the aggregatee is independent of the lifetime of the aggregator

Example

Types of Association

2. Composition

  • Composition is a special kind of aggregation in which the lifetime of the containee depends on the lifetime of the container
  • The distinction between aggregation and composition is normally conceptual; it depends on how the designer thinks about the relationship

Dependency

Dependency is a weaker relationship than inheritance or association

Dependency models the uses relationship

For Class A & B, dependency happens when

  • Class A uses an object of type B as a parameter in a member function
  • Class A has a member function that returns an object of type B
  • Class A has a member function that has a local variable of type B

End of Lecture 4

Computer Programming

That's it!

Queries about this Lesson, please send them to: jmwaura.uni@gmail.com

*References*

  • C++ Programming - An Object-Oriented Approach, 2019 Behrouz, Richard et.al
  • Accelerated C++ - Practical Programming by Example, 2000 Andrew, Barbara et.al
Courtesy of
Computer Programming