You might want to consider this approach: (live example: http://ideone.com/xvYFXp )
#include <iostream>
#include <vector>
#include <memory>
class Building {
public:
virtual int getPower() = 0;
virtual int getWater() = 0;
virtual ~Building() {}
};
class IceWell : public Building {
public:
virtual int getPower() {return -100;}
virtual int getWater() {return 50;}
};
class SolarArray : public Building {
public:
virtual int getPower() { return 150; }
virtual int getWater() { return 0; }
};
int main()
{
std::vector<std::shared_ptr<Building>> inventory;
inventory.emplace_back(new IceWell);
inventory.emplace_back(new SolarArray);
inventory.emplace_back(new IceWell);
inventory.emplace_back(new SolarArray);
int total_power = 0;
int total_water = 0;
for (auto building : inventory)
{
total_power += building->getPower();
total_water += building->getWater();
}
std::cout << "Total power: " << total_power << "\n";
std::cout << "Total water: " << total_water << "\n";
return 0;
}
Building ; , , . IceWell SolarArray , , . , , ( !). .
/ , () - . getWater() , "" .
(. ), . , , , . , .
++ " ". , .
++, . ...
, , . , - . "" . , :
struct ResourceFootprint {
int power;
int water;
};
, :
( : http://ideone.com/ubJv8m)
int main()
{
ResourceFootprint ice_well = {-100, +50};
ResourceFootprint solar_array = {+150, 0};
std::vector<ResourceFootprint> buildings;
buildings.push_back(ice_well);
buildings.push_back(ice_well);
buildings.push_back(solar_array);
buildings.push_back(solar_array);
ResourceFootprint total = {0, 0};
for (const ResourceFootprint& r : buildings)
{
total.power += r.power;
total.water += r.water;
}
std::cout << "P: " << total.power << ", W: " << total.water << "\n";
return 0;
}
; , , , . , , , - :
IceWell,-100,50
SolarArray,150,0
HydroElectricPlant,150,150
, , ResourceFootprint. ; , , , , , , . , , , .
; MedicalWaste. , , , , , .
, . ; :
struct ResourceFootprint {
int power;
int water;
int oil;
int food;
int medicine;
int medical_waste;
};
, , ( , , ), , , :
ResourceFootprint ice_well = {-100, +50, 0, 0, 0, 0};
ResourceFootprint solar_array = {+150, 0, 0, 0, 0, 0};
ResourceFootprint hospital = {-100, -50, 0, -50, -50, 50};
ResourceFootprint gas_station = {-10, 0, -100, 0, 0, 0};
ResourceFootprint restaurant = {-20, -20, 0, -100, 0, 0};
ResourceFootprint farm = {-10, -30, -10, 200, 0, 0};
, , , . :
( : http://ideone.com/rukqaz)
ResourceFootprint total = {0, 0, 0, 0, 0, 0};
for (const ResourceFootprint& r : buildings)
{
total.power += r.power;
total.water += r.water;
total.oil += r.oil;
total.food += r.food;
total.medicine += r.medicine;
total.medical_waste += r.medical_waste;
}
. ?
, , - . , , , , IceWell SolarArray , . , - , .
, , -, . , , , . , ; . , .
:
class Building {
public:
virtual ~Building() {}
virtual ResourceFootprint currentResourceLevel() = 0;
};
ResourceFootprint ( ), . ...
. ResourceFootprint - (, const) :
class HydroElectricPlant : public Building {
public:
HydroElectricPlant(const ResourceFootprint& r)
: resources(r) {}
virtual ResourceFootprint currentResourceLevel() { return resources; }
private:
const ResourceFootprint resources;
};
IceWell - :
class IceWell : public Building {
public:
IceWell(const ResourceFootprint& initial)
: resources(initial) {}
virtual ResourceFootprint currentResourceLevel() { return resources; }
void useWater(int amount) { resources.water -= amount; }
private:
ResourceFootprint resources;
};
SolarArray :
class SolarArray : public Building {
public:
SolarArray(const ResourceFootprint& r)
: day_resources(r), night_resources(r)
{
night_resources.power = 0;
}
virtual ResourceFootprint currentResourceLevel()
{
if (is_day())
{
return day_resources;
}
else
{
return night_resources;
}
}
private:
ResourceFootprint day_resources;
ResourceFootprint night_resources;
};
:
- : , ( ).
- : , Building, , .
?
- . , ResourceFootprint, , ResourceFootprint . , , , .
- . , . std::vector, (, res [0] - , res [1] - ) , , .
- , !
, , , , , ++. , , , , , , . . , , .