fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <memory>
  4.  
  5. class Building {
  6. public:
  7. virtual int getPower() = 0;
  8. virtual int getWater() = 0;
  9. virtual ~Building() {}
  10. };
  11.  
  12. class IceWell : public Building {
  13. public:
  14. virtual int getPower() {return -100;}
  15. virtual int getWater() {return 50;}
  16. };
  17.  
  18. class SolarArray : public Building {
  19. public:
  20. virtual int getPower() { return 150; }
  21. virtual int getWater() { return 0; }
  22. };
  23.  
  24. int main()
  25. {
  26. std::vector<std::shared_ptr<Building>> inventory;
  27. inventory.emplace_back(new IceWell);
  28. inventory.emplace_back(new SolarArray);
  29. inventory.emplace_back(new IceWell);
  30. inventory.emplace_back(new SolarArray);
  31.  
  32. int total_power = 0;
  33. int total_water = 0;
  34. for (auto building : inventory)
  35. {
  36. total_power += building->getPower();
  37. total_water += building->getWater();
  38. }
  39.  
  40. std::cout << "Total power: " << total_power << "\n";
  41. std::cout << "Total water: " << total_water << "\n";
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Total power: 100
Total water: 100