C ++成员变量没有更新 - Noob在这里

时间:2013-09-15 14:45:13

标签: c++ class member

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

我现在已经编了几个月了,所以还是很新的。现在,我正在尝试实现一个非常基本的停车计时器类,只有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);
    }



}

1 个答案:

答案 0 :(得分: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;
}