10.x — Chapter 10 comprehensive quiz

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


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 No
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 Classroom class hold Students.
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
10.7 -- std::initializer_list

26 comments to 10.x — Chapter 10 comprehensive quiz

  • Ryan

    I remember seeing a nice hangman code on this site and decided to have a go at it myself. At first I used char to store my words and it was a clock-full of syntax and problems. Is it possible to show an example of class that supports for a char to function for checking each letter.

    What can I improve on this code?

    • - Initialize your variables with brace initializers.
      - Limit your lines to 80 characters in length for better readability.
      - Don't compare booleans to false/true.
      - Use `std::size` instead of manual array size calculation.
      - You're not using `BodyPart` for anything.
      - Line 42: `if (StickFigure[i].ToShow)`
      - You're using the same name style for variables, function, and types. This will lead to confusion.
      - Don't use `system`, it doesn't work on other platforms.
      - The loop in line 41 should stop once it found a part that doesn't get drawn.
      - Seed random number generators only once. Seeding them multiple times resets the sequence and can cause the same "random" numbers.
      - Use `constexpr` for compile-time constants.
      - Initialize empty strings with empty curly braces. The `const char*` constructor is slower.
      - Constructors and destructors are for construction and cleanup only. The compiler is allowed to omit them, even if it changes your programs behavior.
      - Use single quotation marks for characters.
      - Inconsistent name style.
      - Line 147+: `while (game.playGame());`
      - Pass fundamental types by value.

      Try to incorporate these comments in your code to make sure you understand them. Feel free to ask if you have any questions.

      I assume you mean char arrays. You can do it with them, but it'll be a lot more work because you have to manage the memory yourself. Use `std::string` and `std::string_view` unless you have a reason to use char arrays.

  • Anastasia

    I don't understand the difference between 1b (a text editor and a file) and 1d (a programmer and a computer)... I'd say they are both dependencies.

    The TextEditor class needs a File object, so it can pass it to the save() function. Otherwise the save() function wouldn't work. This means some functionality of the TextEditor class depends on the File class.

    The Programmer class needs a Computer object to pass in to the watchCatVideos() function. Without a Computer object it won't work. Some functionality of the Programmer class depends on the Computer class.

    Is the difference in how essential a dependency is for the overall functionality? I mean if a Text editor can't save anything it's almost pointless, while a programmer should be able to survive a day or two without cat videos... Or it's more abstract than that and I just didn't get it? :/

  • Vir1oN

    I think there`s a word missing in the last sentence of the quiz "2) Select one: If you can design a class using (composition, aggregation, association, or dependency), then you should." "Should use" or "should choose" may sound better.

  • sallyx

    Members existence managed by class    -> Dependency -> YES?
    A text editor class with a save() function that takes a File object. -> How does the text editor manage File ojbect existence?

  • Ran

    Unfortunately, I had a hard time on this quiz.

    ## b
    Association. Test editor use a save() function to save a file.

    Why wrong. This is not a use relation?

    ## d
    Dependency. It is similar to the function std::cout.

    Why wrong? In this case, the Program is similar to a normal function;
    A Computer can be viewed as a function (similar to std::cout). So,
    the relation could be described as Dependency?

    • nascardriver

      Hi Ran!

      > I had a hard time on this quiz
      That's not a problem. You don't need to know how things are called in order to understand them. As long as you understood lesson 10.6 and 10.7 you'll be fine (Unless you're learning c++ for school/university, then you'll need to know this too).

    • Alex

      b) Typically the existence of the File class would be managed by the text editor, so this fits better with a dependency.
      d) The computer and programmer likely exist independently of each other (one doesn't necessarily manage the other).

  • 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.

      • Anastasia

        Ah, I think I understand better now...
        But in case Programmer has-a computer (a pointer to a computer object) which it uses for several different functions, wouldn't it be an aggregation? Although a computer is not a part of a programmer... Or can it be considered as such (in an abstract sense)?

        edit: And yes, it says in the answer that the Programmer and the Computer are otherwise unrelated. But for example in 1c question - An Adventurer and a Sword would be otherwise unrelated too, as I see it...
        I'm sorry for being so dumb, but this Programmer confuses me :(

  • 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


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

  • 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 all code inside code tags: [code]your code here[/code]