#include #include #include using namespace std; // class C struct Street { public: Street(string const& name, int residence_count) : name { name }, residence_count { residence_count } { } string get_name() const { return name; } int get_residence_count() const { return residence_count; } void increase_residence_count(int n) { residence_count += n; } private: // Data member #1 in C string name; // Data member #2 in C int residence_count; }; class City { public: // Constructor #1 City(string const& name, Street const& street) : name{name}, streets { street } { } // Constructor #2 City(string const& name, vector const& streets = {}) : name{name}, streets { streets } { } void add_street(Street const& street) { streets.push_back(street); } // Takes a parameter and modifies the instance void increase_population(int amount) { int const per_street { amount / static_cast(streets.size()) }; for (Street& street : streets) { street.increase_residence_count(per_street); } } // performs some calculation without modifying the instance int get_population() const { int total { 0 }; for (Street const& street : streets) { total += street.get_residence_count(); } return total; } private: // Data member #1 in B string name; // Data member #2 in B // STL container vector streets; }; // testcases int main() { vector streets { Street{ "Universitetsvägen", 10000 }, Street{ "Malmslättsvägen", 6000 }, Street{ "Rydsvägen", 20000 } }; { // Test const and calling a function on const object City const linkoping { "Linköping", streets }; cout << linkoping.get_population() << endl; } { City linkoping { "Linköping", streets }; // test all other member functions Street street { "Teknikringen", 0 }; linkoping.add_street(street); linkoping.increase_population(4000); cout << linkoping.get_population() << endl; } }