Learn C++ · lesson 46 of 61 · about 8 minutes
Capstone: Planning and Modelling
Plan the hero's armoury: list its features and design the Item and Armoury classes.
What you'll learn
- Turn an idea into features
- Choose classes and containers
- Write the first version of the model
The hero's armoury
After the dragon's lair, the hero sets up an armoury: every sword, shield and helmet in one place, ready to equip. It's the capstone, and it's built like a real catalogue. An Item has a name, a kind such as weapon and a power, with const getters so nothing outside can change them. The Armoury keeps every item in a std::map<int, Item> under an ID it hands out, starting at 1, so two swords with the same name are still two items. Items are typed as sword weapon 12 until done, and each one is announced as it's added: Added #1: sword.
The program
Your capstone is a library catalogue:
1. Add books with a title, author and year
2. Borrow and return books
3. Search by author
4. Save the catalogue to a file and load it back
5. Print a tidy, sorted report
Choosing the model
| Thing | Design |
|---|---|
| One book | class Book: title, author, year, on_loan |
| All books by ID | std::map<int, Book> in a Library |
| Next ID to give out | an int member in Library |
| Bad requests | exceptions such as std::out_of_range |
The first version
#include <iostream>
#include <map>
#include <string>
class Book {
public:
Book(std::string title, std::string author, int year)
: title_(std::move(title)), author_(std::move(author)), year_(year) {}
const std::string& title() const { return title_; }
const std::string& author() const { return author_; }
int year() const { return year_; }
bool on_loan() const { return on_loan_; }
private:
std::string title_;
std::string author_;
int year_;
bool on_loan_ = false;
};
class Library {
public:
int add(Book b) {
books_.emplace(next_id_, std::move(b));
return next_id_++;
}
std::size_t size() const { return books_.size(); }
private:
std::map<int, Book> books_;
int next_id_ = 1;
};
int main() {
Library lib;
int a = lib.add(Book("Dune", "Herbert", 1965));
int b = lib.add(Book("Emma", "Austen", 1815));
std::cout << "ids " << a << " and " << b << ", " << lib.size() << " books\n";
}Output
ids 1 and 2, 2 booksTip
emplace builds the map entry in place. Giving each book an ID means two copies of the same title can both be in the library.
The same plan, for the armoury
The armoury follows the same plan: Item instead of Book, with a kind instead of an author, and Armoury instead of Library. The model also gets two small extras the later lessons need: setEquipped() on an item, and all(), which hands out the map as a const reference, so other code can read every item but not change any.
Reading items until done
#include <iostream>
#include <map>
#include <sstream>
#include <string>
int main() {
std::istringstream typed("sword 12 axe 18 done");
std::map<int, std::string> names;
int nextId = 1;
std::string name;
int power = 0;
while (typed >> name && name != "done" && typed >> power) {
names.emplace(nextId++, name);
}
std::cout << names.size() << " items, #2 is the " << names.at(2) << "\n";
}Output
2 items, #2 is the axeNow 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.