初学者c ++程序员:坚持写一堂课

时间:2016-02-03 01:35:31

标签: c++

我必须在课堂上为一个练习写一个程序,但我完全迷失了。我有一些java编程经验,所以切换到c ++语法让我很困惑。

提示符是:写一个类的定义,游泳池,以实现游泳池的正确性。你的类应该有实例变量来存储长度(以英尺为单位),宽度(以英尺为单位),深度(以英尺为单位),水填充池的速率(以加仑每分钟为单位)和速率(在水从池中排出的每分钟加仑数。添加适当的构造函数以初始化实例变量。还要添加成员函数以执行以下操作:确定填充空池或部分填充池所需的水量,确定完全或部分填充或清空池所需的时间,以及在特定时间内添加或排水。

我收到了几个构建错误。明白我现在所拥有的并不完整。到目前为止,这是我的进展。

#include <iostream>
using namespace std;
int main()
{
    swimmingPool mySwimmingPool;
    int length, width, depth, rateToFill, rateToDrain;

    cout << "Pool Data: " << endl;
    cout << mySwimmingPool.getPoolData(5, 12,6) << endl;

}


class swimmingPool
{
public:
    int getPoolData(int, int, int);
    int getAmountOfWater();
    int getTime();
    int incrementWater(double time);
    int decrementWater(double time);
    int poolCapacity();
    swimmingPool();
    swimmingPool(int l, int w, int d, int rateFill, int rateDrain);

private:
    int length;
    int width;
    int depth;
    int rateToFill;
    int rateToDrain;
};

swimmingPool::swimmingPool()
{
    length = 0;
    width = 0;
    depth = 0;
    rateToFill = 0;
    rateToDrain = 0;
}

swimmingPool::swimmingPool(int l, int w, int d, int rateFill, int rateDrain)
{
    length = l;
    width = w;
    depth = d;
    rateToFill = rateFill;
    rateToDrain = rateDrain;
}

int swimmingPool::getPoolData(int l, int w, int d)
{
    length = l;
    width = w; 
    depth = d;

    cout << "length: " << l << endl;
    cout << "width: " << w << endl;
    cout << "depth: " << d << endl;
}

int swimmingPool::getAmountOfWater()
{
    return (length * width * depth);
}

int swimmingPool::getTime()
{
    return getAmountOfWater() / rateToFill;
}

int swimmingPool::incrementWater(double time)
{
    return rateToFill * time;
}

int swimmingPool::decrementWater(double time)
{
    return rateToDrain * time;
}

1 个答案:

答案 0 :(得分:1)

如果您的程序返回函数类型不正确。请参阅修改后的代码。

#include <iostream>
using namespace std;
class swimmingPool
{
public:
    void getPoolData(int, int, int);
    int getAmountOfWater();
    double getTime();
    double incrementWater(double time);
    double decrementWater(double time);
    int poolCapacity();
    swimmingPool();
    swimmingPool(int l, int w, int d, int rateFill, int rateDrain);

private:
    int length;
    int width;
    int depth;
    int rateToFill;
    int rateToDrain;
};

swimmingPool::swimmingPool()
{
    length = 0;
    width = 0;
    depth = 0;
    rateToFill = 0;
    rateToDrain = 0;
}

swimmingPool::swimmingPool(int l, int w, int d, int rateFill, int rateDrain)
{
    length = l;
    width = w;
    depth = d;
    rateToFill = rateFill;
    rateToDrain = rateDrain;
}

//If function do not return any value then return type should be void
void swimmingPool::getPoolData(int l, int w, int d)
{
    length = l;
    width = w; 
    depth = d;

    cout << "length: " << l << endl;
    cout << "width: " << w << endl;
    cout << "depth: " << d << endl;
}

int swimmingPool::getAmountOfWater()
{
    return (length * width * depth);
}

double swimmingPool::getTime()
{
    return getAmountOfWater() / rateToFill; //This value is double not int. if you convert double to int data maybe loss 
}

double swimmingPool::incrementWater(double time)
{
    return rateToFill * time; //This value is double not int. if you convert double to int data maybe loss 
}

double swimmingPool::decrementWater(double time)
{
    return rateToDrain * time; //This value is double not int. if you convert double to int data maybe loss 
}


int main()
{
    swimmingPool mySwimmingPool;
    //You don't need this variables
    //int length, width, depth, rateToFill, rateToDrain;

    cout << "Pool Data: " << endl;
    mySwimmingPool.getPoolData(5, 12,6);
    return 0;
}