Start learning free

LESSON 605 OF 634 · OBJECT-ORIENTED PROGRAMMING

Printable Things in C++

Practice

You write an interface and two classes that use it.

Printable is an interface with one method, label(). It returns a short text about the item.

Two classes implement it:

  • Book(title, author). Its label is the title, then by, then the author.
  • Ticket(code). Its label is Ticket, then the code.

One space separates the words of a label.

C++ has no interface keyword. Printable is a class whose only method is pure virtual, ending with = 0.

Then write printAll(items). It goes in the class Solution. It returns every item's label, in the same order.

The items mix books and tickets. Use each one only through label().

The starter code already has the classes and their constructors. The harness prints each label on its own line.

 

Example 1:

Input: new Book("Dune", "Herbert"), new Ticket("A12")
Output:
Dune by Herbert
Ticket A12

Explanation: The book joins its title and author with by.
The ticket puts Ticket before its code.

Example 2:

Input: new Ticket("B7"), new Ticket("C3"), new Book("Emma", "Austen")
Output:
Ticket B7
Ticket C3
Emma by Austen

Explanation: The labels keep the order of the items.

Example 3:

Input: new Book("1984", "Orwell")
Output:
1984 by Orwell

Explanation: A title can be digits. It is still text.

 

Constraints:

  • 1 to 100 items.
  • Each title, author and code is one word.
  • A word has 1 to 20 letters or digits.
STEP 1 OF 6
    Ctrl + Enter to check

    Printable Things in C++

    Keep going with C++500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free