Skip to main content

How To Think Like a Computer Scientist C++ Edition The Pretext Interactive Version

Section 12.1 Composition

By now we have seen several examples of composition (the ability to combine language features in a variety of arrangements). One of the first examples we saw was using a function invocation as part of an expression. Another example is the nested structure of statements: you can put an if statement within a while loop, or within another if statement, etc.
Having seen this pattern, and having learned about vectors and objects, you should not be surprised to learn that you can have vectors of objects. In fact, you can also have objects that contain vectors (as instance variables); you can have vectors that contain vectors; you can have objects that contain objects, and so on.
In the next two chapters we will look at some examples of these combinations, using Card objects as a case study.

Checkpoint 12.1.1.

Which of the following statements is correct?
  • You can have vectors that contain other vectors and objects that contain other objects.
  • This is called composition!
  • You can have vectors that contain other vectors, but you can never have objects that contain other objects.
  • In this chapter you will see how you can have objects that contain other objects.
  • You can never have vectors that contain other vectors, but you can have objects that contain other objects.
  • In this chapter you will see how you can have vectors that contain other vectors.
  • You can never have vectors that contain other vectors, nor objects that contain other objects.
  • Vectors and objects can have nested compositons!

Checkpoint 12.1.2.

You have attempted 1 of 3 activities on this page.