0

我已经在这里潜伏和浏览了一段时间,但这是我第一次发帖。希望我已经掌握了规则和格式。

我已经编码了几个月了,所以在这方面还是很新的。现在,我正在尝试实现一个非常基本的停车计时器类,只有 2 个功能。一个插入宿舍,一个检查剩余时间。成员变量是 maxTime、rate 和 time。

我启动并运行了一些功能,但我的 checkTime 功能一直给我奇怪的结果。我几乎在每一行都进行了测试,我意识到在我退出构造函数后,我输入的值消失了,并被一个非常长的数字替换。我无法弄清楚为什么会这样。这不是我第一次使用类、构造函数、实例化对象等,而且我看不出这次我做了什么不同的事情。

哪位专家可以告诉我我在哪里搞砸了?

这是我的头文件:

#ifndef PARKINGMETER_H_INCLUDED
#define PARKINGMETER_H_INCLUDED

#ifndef PARKINGMETER_H_INCLUDED
#define PARKINGMETER_H_INCLUDED

class ParkingMeter {
private:
    int maxTime;
    double time, rate;

public:
    ParkingMeter();
    ParkingMeter(int, double);
    ~ParkingMeter();
    void insertQtrs(int);
    double checkTime(double);
    double getTime();
    void setTime(double);

};

这是我的实现:

#include <iostream>
#include <stdexcept>
#include <ctime>
#include "ParkingMeter.h"

using namespace std;

ParkingMeter::ParkingMeter() {       //default constructer
    maxTime = 120;
    rate = .25;
    time = 0;
}

ParkingMeter::ParkingMeter (int maxTime, double rate) {       //constructor
    maxTime = maxTime;
    rate = rate;
    cout<<"maxTime is "<<maxTime<<endl;
    cout<<"rate is "<<rate<<endl;
}

ParkingMeter::~ParkingMeter(){      //destructor
}

void ParkingMeter:: insertQtrs(int quarters){
    ParkingMeter test(this->maxTime, this->rate);
    cout<<"maxTime is "<<test.maxTime<<endl;
    cout<<"rate is "<<test.rate<<endl;
    cout<<"You have inserted: "<<quarters<<" quarters."<<endl;
    double time = quarters * (rate * 60);
    if ( time > 120)
        time = 120;
    this ->setTime(time);

}

double ParkingMeter:: checkTime (double startTime){
    ParkingMeter test(this->maxTime, this->rate);
    double elapsed = clock() - startTime;
 //   test.maxTime = this->maxTime;
    cout<<"test: "<<test.maxTime<<endl;
    cout<<"elapsed time: "<<elapsed<<endl;
    cout<<"meter time: "<<time<<endl;
    cout <<"Your remaining time is: "<< (time - (elapsed / ((double)CLOCKS_PER_SEC)));
}

/*double ParkingMeter:: getTime (){
    int time = this-> maxTime;
    cout<<"time: "<<time<<endl;
    return time;
}*/

void ParkingMeter:: setTime (double time){
    this ->time = time;
}

int main () {
    double maxTime, rate;
    int quarters;
    char y;

    cout<<"Please enter the max parking time and rate, separated by a space: "<<endl;
    cin>>maxTime>>rate;
    ParkingMeter meter(maxTime, rate);
    cout<<"Please enter the amount of quarters you wish to enter: "<<endl;
    cin>>quarters;
    clock_t start = clock();
    meter.insertQtrs(quarters);
    cout<<"Please enter Y to check remaining time: "<<endl;
    cin>>y;

    if (y == 'y'){
       double startTime = start;
        cout<<"starttime: "<<startTime<<endl;
        meter.checkTime (startTime);
    }



}
4

1 回答 1

1

这是你的问题:

ParkingMeter::ParkingMeter (int maxTime, double rate) {       //constructor
   this->maxTime = maxTime;
   this->rate = rate;
// ^^^^^^ note this!

您可能想要使用初始化列表,在这种情况下,您可以(并且必须)删除this->. 为避免出现问题,我建议您为成员变量使用不同的名称,例如,添加_

ParkingMeter::ParkingMeter (int maxTime, double rate)
    : maxTime_( maxTime ), rate_( rate )
{
    cout<<"maxTime is "<<maxTime_<<endl;
    cout<<"rate is "<<rate_<<endl;
}
于 2013-09-15T14:47:20.840 回答