从外部类中更改循环内的变量

时间:2017-12-10 13:34:40

标签: class loops variables inheritance

我有一个时间类和一个Build类。收获功能在整个时间内增加了矿物质数量,但是矿物质的数量取决于可用工人的数量。一旦构建开始,工作人员数量就会减少。 那么,如何在循环内减少工作者数量,而不是在循环结束后减少工作者数量。 我希望矿物质不断增加,但是根据Build调用,工人数量应该增加和减少。

class Time {
private:
    int minerals = 50;
        int workers = 6;
    int vespene = 0;
    int supply = 6;
    int time = 0;


public:
    Time ();
    int Time_counter();
    void harvest();
    int mineral_count();
    int worker_count();
    int supply_count();
    void update(int m, int w, int s, int t);
    void Display();
};

Time::Time ()
{
    for (int i =0; i<9; i++)
    {
        update(minerals, workers, supply, time);
        harvest();
        time = i;
       // Time_counter();
    }


}
int Time::Time_counter() {
        return time;

}

void Time::harvest() {

    minerals+= workers * (0.7);

    }

int Time:: mineral_count() {
    return minerals;
}

int Time::worker_count() {
    //return workers.size();
    return workers;
}

int Time::supply_count() {
    return supply;
}

void Time:: update(int m, int w, int s, int t) {
    minerals = m;
    workers = w;
    supply = s;
    time = t;
    cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
    << "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}

void Time:: Display ()
{
 cout << "No. of Minerals: " <<minerals <<"\n" <<"No.of Workers: " <<workers <<"\n"
 << "No. of Supply: " <<supply <<"\n" <<"Current Time: " <<time << endl;
}

class Build: public Time {
public:
    void Build_create();            

};

void Build::Build_create() {
    int build_count=0;
    SCV obj;
    int m = obj.mineral_count();
    int w = obj.worker_count();
    int s = obj.supply_count();
    int t; // = obj.Time_counter();

    if (m >= 50 && biuld_count<2)
    {
        cout << "Build start....";
        m-= 50;

        obj.update(m,w,s,t);
        obj.Display ();
         w-=1;

        for (t = obj.Time_counter(); t <obj.Time_counter()+ 10; t++)
        {cout<< "Building...";}
        cout << " Build complete" << endl;
        scv_count++;
        w += 1;
        s += 1;
        t = obj.Time_counter();
        obj.update(m, w, s, t);
        obj.Display ();
    }

    else if (m<50)
    {
        cout << "Cannot build " << endl;

    }
    else if (build_count==2)
    {
        cout<<"two already built.." <<endl;
    }

0 个答案:

没有答案