1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
class Building
{
public:
Building(string owner, int rooms, double area) :
owner(owner),
rooms(rooms),
area(area)
{
}
friend ostream& operator<<(ostream &os, const Building& b);
bool operator<(const Building& other) const
{
return area < other.area;
}
protected:
string owner;
int rooms;
double area;
};
// Helper to send Building to an output stream
ostream& operator<<(ostream& os, const Building& b)
{
os << setw(20) << b.owner <<
setw(8) << b.rooms <<
setw(12) << b.area << endl;
return os;
}
class House : public Building
{
public:
House(string family, string owner, int rooms, double area) :
family_(family),
Building(owner, rooms, area)
{
}
private:
string family_;
};
int main()
{
vector<House> houses
{
{"Smith", "John Smith", 8, 980.7 },
{"Jones", "Jane Jones", 6, 802.3},
{"Rawr", "Meow Rawr", 9, 844.4 }
};
sort(houses.begin(), houses.end());
cout << setw(20) << "Owner" <<
setw(8) << "Rooms" <<
setw(12) << "Area" << endl;
for (auto& house : houses)
cout << house;
return 0;
}
|