Learn C++ · lesson 49 of 61 · about 10 minutes
Capstone: Polishing
Print items with operator<<, produce a sorted armoury report, and review the design.
What you'll learn
- Overload operator<< for a class
- Sort a report by several keys
- Review code for clarity
The armoury report
The last touch is a report the hero can read at a glance. operator<< for Item prints the name and kind lined up on the left with std::setw, then the power, then (equipped) if it is, so std::cout << item just works. The report copies the items into a vector and sorts it by kind and then by power, strongest first, with std::stable_sort, so items that tie stay in the order they were added. It ends with how many items there are and how many are equipped.
Printing your own types
Overloading operator<< lets you write std::cout << book for your own class:
std::ostream& operator<<(std::ostream& os, const Book& b) { return os << b.title; }
It returns the stream so calls can chain.
The final report
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
struct Book {
std::string title;
std::string author;
int year;
bool on_loan;
};
std::ostream& operator<<(std::ostream& os, const Book& b) {
return os << std::left << std::setw(12) << b.title << std::setw(9) << b.author << b.year << (b.on_loan ? " (on loan)" : "");
}
int main() {
std::vector<Book> books = {
{"Persuasion", "Austen", 1817, false},
{"Dune", "Herbert", 1965, true},
{"Emma", "Austen", 1815, false},
};
std::sort(books.begin(), books.end(), [](const Book& a, const Book& b) {
return a.author != b.author ? a.author < b.author : a.year < b.year;
});
for (const auto& b : books) std::cout << b << "\n";
auto out = std::count_if(books.begin(), books.end(), [](const Book& b) { return b.on_loan; });
std::cout << books.size() << " books, " << out << " on loan\n";
}Output
Emma Austen 1815
Persuasion Austen 1817
Dune Herbert 1965 (on loan)
3 books, 1 on loanPutting the stream back
std::setw only lasts for the next thing printed, but std::left stays until something changes it. The armoury's operator<< ends its lined-up part with std::right, so printing an item never leaves the stream aligned on the left for whatever the program prints next.
left stays, setw doesn't
#include <iomanip>
#include <iostream>
int main() {
std::cout << std::left << std::setw(6) << "axe" << "|\n";
std::cout << std::setw(4) << 18 << "|\n";
std::cout << std::right << std::setw(4) << 18 << "|\n";
}Output
axe |
18 |
18|A final review
Before calling it done:
- Is every class responsible for one thing?
- Are members private, with const getters?
- Are invalid operations rejected with clear exceptions?
- Do the tests still pass?
Tip
You've built a real C++ program with classes, containers, exceptions, files, tests and operator overloading. Try adding due dates or a search by title next.
Now try it yourself
This lesson is free in Codiqo, with 8 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.