Search

10.x — Chapter 10 comprehensive quiz

In this chapter, we learned about some different kinds of relationships between two objects.

Summary

The process of building complex objects from simpler ones is called object composition. There are two types of object composition: composition, and aggregation.

Composition exists when a member of a class has a part-of relationship with the class. In a composition relationship, the class manages the existence of the members. To qualify as a composition, an object and a part must have the following relationship:

  • The part (member) is part of the object (class)
  • The part (member) can only belong to one object (class) at a time
  • The part (member) has its existence managed by the object (class)
  • The part (member) does not know about the existence of the object (class)

Compositions are typically implemented via normal member variables, or by pointers where the class manages all the memory allocation and deallocation. If you can implement a class as a composition, you should implement a class as a composition.

Aggregations exists when a class has a has-a relationship with the member. In an aggregation relationship, the class does not manage the existence of the members. To qualify as an aggregation, an object and its parts must have the following relationship:

  • The part (member) is part of the object (class)
  • The part (member) can belong to more than one object (class) at a time
  • The part (member) does not have its existence managed by the object (class)
  • The part (member) does not know about the existence of the object (class)

Aggregations are typically implemented via pointer or reference.

Associations are a looser type of relationship, where the class uses-an otherwise unrelated object. To qualify as an association, an object and an associated object must have the following relationship:

  • The associated object (member) is otherwise unrelated to the object (class)
  • The associated object (member) can belong to more than one object (class) at a time
  • The associated object (member) does not have its existence managed by the object (class)
  • The associated object (member) may or may not know about the existence of the object (class)

Associations may be implemented via pointer or reference, or by a more indirect means (such as holding the index or key of the associated object).

In a dependency, one class uses another class to perform a task. The dependent class typically is not a member of the class using it, but rather is temporarily created, used, and then destroyed, or passed into a member function from an external source.

In a container class one class provides a container to hold multiple objects of another type. A value container is a composition that stores copies of the objects it is holding. A reference container is an aggregation that stores pointers or references to objects that live outside the container.

std::initializer_list can be used to implement constructors, assignment operators, and other functions that accept a list initialization parameter. std::initailizer_list lives in the <initializer_list> header.

Property\Type Composition Aggregation Association Dependency
Relationship type Whole/part Whole/part Otherwise unrelated Otherwise unrelated
Members can belong to multiple classes No Yes Yes Yes
Members existence managed by class Yes No No Yes
Directionality Unidirectional Unidirectional Unidirectional or bidirectional Unidirectional
Relationship verb Part-of Has-a Uses-a Depends-on

Quiz time

This chapter is pretty straightforward and a little more abstract than the previous ones, so this quiz will be short and to the point.

1) What type of relationship (composition, aggregation, association, or dependency) do the following describe?
1a) An Animal class that contains an animal type (enum) and name (string).
Show Solution

1b) A text editor class with a save() function that takes a File object. The save() function writes the contents of the editor to disk.
Show Solution

1c) An Adventurer class that can carry various kinds of Items, such as swords, wands, potions, or spellbooks. These Items can be dropped and picked up by other Adventurers.
Show Solution

1d) A Programmer uses a Computer to watch cat videos on the internet.
Show Solution

1e) A Computer class that contains a CPU class. The CPU can be removed from the Computer and tested on its own.
Show Solution

2) Select one: If you can design a class using (composition, aggregation, association, or dependency), then you should.
Show Solution

11.1 -- Introduction to inheritance
Index
10.7 -- std::initializer_list

11 comments to 10.x — Chapter 10 comprehensive quiz

  • Dani

    Alex, why 1d is an association and not a dependency?
    As I see it, the computer is not a part of the programmer, but something he uses to see the cat videos (like std::string would use std::cout to print on screen).
    What am I missing?

    • Alex

      It could be either. If the Programmer has a ViewCatVideos() function that takes a Computer parameter, then the Computer is a dependency.

      But association really a better fit here. More likely the Programmer class has an associated Computer member pointing to the Computer being used for all of the tasks the Programmer is likely to do, so the caller doesn’t have to pass in the associated Computer for every member function. This would be an association.

  • Akshay Chavan

    Hi Alex,

    In the line "To quality as an association, and object and an associated object must have the following relationship", shouldn’t it be "qualify" and "an object" ?

  • Martin

    To quality as a composition/aggregation/association should be "to qualify as a…"

    "a composition that stories copies" should be
    "a composition that stores copies"

  • Mohammad

    Hello

    about question 1d, only one person can use the computer at a time, doesn’t that mean its not an association?

    • Alex

      ‘Nope. The associated object (member) can belong to more than one object (class) at a time. Note the word “can” -- that doesn’t mean it has to.

  • EGa

    How to make composition in C++ though?

  • LuatNT

    @Alex Could you please explain more detail about "1c" case? I think that the answer can be aggregation, am I wrong?

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter