【问题标题】:How to update instances by updating static variables?如何通过更新静态变量来更新实例?
【发布时间】:2017-01-31 08:20:13
【问题描述】:

我的班级时间可以根据静态变量以一天中的小时数和一小时内的分钟数显示时间。我希望能够通过简单地更改我的静态变量来打印更新的时间。

在我的 main() 中,我想根据默认的静态变量(一天 24 小时,一小时 60 分钟)声明一些 Time 实例。

Time a;
Time b(5);
Time c(61);

cout << "a = " << a << "\t";
cout << "b = " << b << "\t";
cout << "c = " << c << "\n";

// output is a =  0:00  b =  0:05 c =  1:01

Time::set_hr_in_day(60);

Time::set_min_in_hr(24);
cout << "a = " << a << "\t";
cout << "b = " << b << "\t";
//output should be a =  0:00    b =  0:05   c =  2:13

但是,代码仍然会打印出默认静态变量的数字。

关于如何解决此问题的任何想法?顺便说一句,我正在尝试创建一个类来测试给定的驱动程序,因此不能更改我的主要功能。

这是我的其余代码:

头文件

#ifndef TIME_H
#define TIME_H

#include <iostream>

using namespace std;

class Time {
private:
    int hour;
    int minute;
    static int dayhrs;
    static int hrsmin;

public:

    Time();
    Time(int min);
    Time(int hr, int min);
    Time(double hrs);
    int minutes() { return minute; };
    int hours() { return hour; };

    static void set_hr_in_day(int hr);
    static void set_min_in_hr(int min);
    static int dailyhr() { return dayhrs; };
    static int hourlymin() { return hrsmin; };

friend ostream& operator<<(ostream& os, const Time& t);


};



#endif

实现

#include "time.h"
#include <iostream>

using namespace std;

int Time::dayhrs = 24;//default
int Time::hrsmin = 60;

void Time::set_hr_in_day(int hr) {
    dayhrs = hr;
}

void Time::set_min_in_hr(int min) {
    hrsmin = min;
}


Time::Time() {
    hour = 0;
    minute = 0;
}

Time::Time(int min) {
    if (min > (Time::hourlymin() - 1)) {
        hour = min / Time::hourlymin();
        minute = min % Time::hourlymin();
    }
    else {
        hour = 0;
        minute = min;
    }

}

Time::Time(int hr, int min) {
    if (min > (Time::hourlymin() - 1)) {
        hour = min / Time::hourlymin() + hr;
        minute = min % Time::hourlymin();
    }
    else {
        hour = hr;
        minute = min;
    }

    if (hour > (Time::dailyhr() - 1))
        hour = hour % (Time::dailyhr());
}

Time::Time(double hrs) {
    double fraction = 0;
    fraction = hrs - (int)hrs;
    minute = fraction * Time::hourlymin();
    if (fraction * Time::hourlymin() - (int)(fraction * Time::hourlymin()) >= 0.5)
        minute += 1;
    hour = hrs - fraction;
    if (hour > (Time::dailyhr() - 1))
        hour = hour % (Time::dailyhr());
}



ostream& operator<<(ostream& os, const Time& t)
{
    if (t.minute > 9)
        os << t.hour << ":" << t.minute;
    else
        os << t.hour << ":0" << t.minute;
    return os;
}

司机

#include <iostream>
#include "time.h"

using std::cout;

int main() {
    cout << "*****************************************\n";
    cout << "Welcome to 'San Angel'!\n";
    cout << "[ 1 day = 24 hours, 1 hour = 60 minutes ]\n\n";

    Time a;
    Time b(5);
    Time c(61);
    Time d(47, 59);
    Time X(5.0);
    Time Y(1.5);
    Time Z(25.1);

    cout << "a = " << a << "\t";
    cout << "b = " << b << "\t";
    cout << "c = " << c << "\n";
    cout << "d = " << d << "\t";
    cout << "X = " << X << "\t";
    cout << "Y = " << Y << "\n";
    cout << "\t\tZ = " << Z << "\n";


    Time::set_hr_in_day(60);
    Time::set_min_in_hr(24);
    cout << "*****************************************\n";
    cout << "Welcome to the land of the remembered!\n";
    cout << "[ 1 day = 60 hours, 1 hour = 24 minutes ]\n\n";

    cout << "a = " << a << "\t";
    cout << "b = " << b << "\t";
    cout << "c = " << c << "\n";
    cout << "d = " << d << "\t";
    cout << "X = " << X << "\t";
    cout << "Y = " << Y << "\n";
    cout << "\t\tZ = " << Z << "\n";

return 0;

}
/**

OUTPUT:

*****************************************
Welcome to 'San Angel'!
[ 1 day = 24 hours, 1 hour = 60 minutes ]

a =  0:00   b =  0:05   c =  1:01
d = 23:59   X =  5:00   Y =  1:30
Z =  1:06
*****************************************
Welcome to the land of the remembered!
[ 1 day = 60 hours, 1 hour = 24 minutes ]

a =  0:00   b =  0:05   c =  2:13
d = 59:23   X = 12:12   Y =  3:18
Z =  2:18 */

【问题讨论】:

  • 调试器是解决此类问题的正确工具。 询问 Stack Overflow 之前,您应该逐行浏览您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
  • 好的,谢谢,我去看看。刚学语言,对调试器的使用不太熟悉。

标签: c++ class static instance cout


【解决方案1】:

您只在构造函数中执行计算。当您稍后修改静态变量时,您不会自动重新计算结果。

一种可能的解决方案是将值存储在构造函数中,然后在写入流时进行实际计算。

【讨论】:

  • 我会试试的。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-30
  • 1970-01-01
  • 1970-01-01
  • 2021-02-07
  • 2015-08-14
相关资源
最近更新 更多