使用枚举和for循环创建类

时间:2015-10-29 04:36:08

标签: c++ pointers for-loop enums instances

enum { WOOD, BRICK, GRAIN, HEMP, WOOL, RAWMETAL, HONEY, SALT, METALGOODS, MEAD, CLOTH, BEER, STOCKFISH, CLOTHING, CHEESE, PITCH, PELTS, MEAT, WINE, SPICES, TOTALRESOURCES };
// An array of strings for each of the resource names
// As long as these are in the same order as the enum everything should match up
// .: resName[PIGIRON] is "Pig Iron"
string resName[]{ "Wood", "Brick", "Grain", "Hemp", "Wool", "Raw Metal", "Honey", "Salt", "Metal Goods", "Mead", "Cloth", "Beer", "Stockfish", "Clothing", "Cheese", "Pitch", "Pelts", "Meat", "Wine", "Spices" };
enum { Edinburgh, Scarborough, Boston, London, Bruges, Haarlem, Nimwegen, Groningen, Cologne, Minden, Bremen, Erfurt, Hamburg, Lubeck, Rostock, Berlin, Ripen, Flensburg, Aalborg, Naevsted, Bergen, Stavanger, Oslo, Stockholm, Gothenburg, Malmo, Ahus, Visby, Stettin, Posen, Breslau, Danzig, Thorn, Warsaw, Konigsberg, Kaunas, Riga, Reval, Helsinki, Novgorod, TOTALTOWNS};
string townName[]{ "Edinburgh", "Scarborough", "Boston", "London", "Bruges", "Haarlem", "Nimwegen", "Groningen", "Cologne", "Minden", "Bremen", "Erfurt", "Hamburg", "Lubeck", "Rostock", "Berlin", "Ripen", "Flensburg", "Aalborg", "Naevsted", "Bergen", "Stavanger", "Oslo", "Stockholm", "Gothenburg", "Malmo", "Ahus", "Visby", "Stettin", "Posen", "Breslau", "Danzig", "Thorn", "Warsaw", "Konigsberg", "Kaunas", "Riga", "Reval", "Helsinki", "Novgorod"};
class resource
{
public:
    float demand, production, businessNeeds, businessProduction;

    // This function, called a constructor, is run every time a new resource is created
    // In this case, it assigns 0 to everything
    resource()
    {
        demand = 0;
        production = 0;
        businessNeeds = 0;
        businessProduction = 0;
    }
    float net()
    {
        return (this->production - this->demand);
    }
    float businessNet()
    {
        return (this->businessProduction - this->businessNeeds);
    }
};

class town
{
public:
    // The array of pointers to each of a our resource objects
    resource *resList[TOTALRESOURCES];

    // This is the town constructor
    town()
    {
         // Loops through the array and creates a new resource object in each
         // the resource constructor assigns the default values of 0.
        for (int i = 0; i < TOTALRESOURCES; i = i + 1)
        {
            resList[i] = new resource();
        }
    }

    ~town()
    {
    // Loops through the array and deletes each resource object
    for (int i = 0; i < TOTALRESOURCES; i = i + 1)
    {
    delete resList[i];
    }
};

int main()
{
    //What do I do here?
    for (int i = 0; i < TOTALTOWNS; i++)
    {
         town townName[i];
    }
    system("pause");
    return 0;
}

所以,我是一名软件工程专业的学生,​​我只是换了专业,所以我决定花个些时间来学习如何更好地用c ++编写代码。我决定建立一个程序,可以为名为 Patrician IV 的视频游戏规划物流。

我已经为这个项目投入了大约5天的工作量,并且发现了我的初始代码存在一些严重问题(难以添加新功能和更改内容)。所以我退后一步,尝试以更简洁的方式构建我的类,同时还能够循环遍历每个城镇实例,以便我可以更新 demand 轻松生成 businessNeeds businessProduction 值。在此之前,我正在复制和粘贴大约6行代码40次。

我想知道:

(A)是否有可能做我想做的事情 - 即我可以使用枚举和for循环来构建城镇的实例。

(B)如何循环遍历每个城镇,以便我可以为资源变量添加值。

(C)关于如何将指针用于类似目的的三年级解释也很棒。

:) 谢谢!

2 个答案:

答案 0 :(得分:0)

在main函数中,使用与resList相同的想法来初始化城镇对象,所以:

town* townName[TOTALTOWNS]
for (int i = 0; i < TOTALTOWNS; i++)
{
     townName[i]= new town();
}

然后,我猜你想为每个不同的资源提供不同的值。切换语句与枚举很好。所以我会推荐这样的东西:

for (int i = 0; i < TOTALRESOURCES; i = i + 1)  
{
    switch(i)
    {
        case WOOD:
            townName[EDINBURGH]->resList[WOOD]->demand= yourValue;
            break;
        case BRICK:
            break;

    }
}

或者,如果您在城镇骑自行车:

for (int i = 0; i < TOTALTOWNS; i = i + 1)  
{
    switch(i)
    {
        case EDINBURGH:
            break;      //etc.
    }
}

如果您只想更新单个城镇或资源,根据您组织所有内容的方式,您可以创建类似的内容。获取城镇数组和枚举索引的函数,如下所示:

updateTownResources(town* (*townName)[TOTALTOWNS], int townEnum, int resourceEnum, int dValue, int pValue )
{
    townName[townEnum]->resList[resourceEnum]->demand= dValue;
    townName[townEnum]->resList[resourceEnum]->production= pValue;
    //etc...
}

答案 1 :(得分:-1)

int main()
{
    std::vector<town*> townArray;
    //What do I do here?
    for (int i = 0; i < TOTALTOWNS; i++)
    {
         town* pTown = new(std::nothrow) town;
         townArray.push_back (pTown);
    }
    std::vector<town*>::iterator iter = townArray.begin();
    for (;iter != townArray.end(); iter++) {
        (*iter); // gives access to individual objects
    }
    system("pause");
    return 0;
}
相关问题