Start learning free

LESSON 620 OF 634 · OBJECT-ORIENTED PROGRAMMING - CHALLENGES

Book Label in C++

Practice O(1)

A library prints a label for each book. Write the class Book, so that printing a book prints its label.

A Book is made from a title, an author and a year. It keeps all three.

The label starts with the title and the year in parentheses. Then come the word by and the author. For example, Dune (1965) by Frank Herbert.

In C++, the program prints each book with <<. Below the class, write operator<< for Book. It sends the label to the stream.

The program makes the books in the input. Then it prints each book on its own line.

 

Example 1:

Input:
("Dune", "Frank Herbert", 1965)
Output:
Dune (1965) by Frank Herbert
Explanation: The title comes first.
The year comes next, in parentheses.
The author comes last, after "by".

Example 2:

Input:
("The Hobbit", "J.R.R. Tolkien", 1937)
("1984", "George Orwell", 1949)
Output:
The Hobbit (1937) by J.R.R. Tolkien
1984 (1949) by George Orwell
Explanation: Each book prints its own label, on its own line.
A title can be a number, like "1984".

Example 3:

Input:
("Confessions", "Augustine", 400)
Output:
Confessions (400) by Augustine
Explanation: The year is printed as it is, with no zeros in front.

 

Constraints:

  • 1 to 100 books.
  • A title or an author has 1 to 50 characters.
  • They are letters, digits, spaces and punctuation.
  • They never start or end with a space.
  • The year is a whole number from 1 to 2025.
STEP 1 OF 7
    Ctrl + Enter to check

    Book Label 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