【问题标题】:Having trouble with class constructors printing打印类构造函数时遇到问题
【发布时间】:2017-06-14 21:00:53
【问题描述】:

我试图通过创建一个包含小时、分钟和秒的 Time 类来了解类及其构造函数的工作原理。我想使用默认构造函数打印一次,通过用户输入打印一次。当我的程序编译时,它不要求用户输入,很可能是因为我调用类函数 getHour 的方式(如果我只打印输入时间)。我也不确定如何通过默认构造函数打印时间(0,0,0)。

任何帮助将不胜感激!

主要:

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

int main(){

    std::cout << "Enter the hour, minute, and second: " << std::endl;
    int hour, minute, second;
    std::cin >> hour >> minute >> second;

    Time time1(hour, minute, second);

    std::cout << time1.getHour() << std::endl;

    return 0;
}

类实现:

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

//default constructor
Time::Time() {
    hour = 0;
    minute = 0;
    second = 0;
}

//construct from hour, minute, second
Time::Time(int theHour, int theMinute, int theSecond) {
    hour = theHour;
    minute = theMinute;
    second = theSecond;
}

int Time::getHour() const {
    return hour;
}

int Time::getMinute() const {
    return minute;
}

int Time::getSecond() const {
    return second;
}

【问题讨论】:

  • 它不要求用户输入,很可能是因为我调用类函数 getHour 的方式(如果我只打印输入时间) 你确定你正在运行当前代码的构建吗?输入在main(),与getHour()无关

标签: c++ class oop constructor


【解决方案1】:

对我来说很好,这是我的输出:

Enter the hour, minute, and second:
2
45
32
2
Press any key to continue . . .

确保您正在重建代码并运行新的可执行文件。

在构造函数中你可以这样做:

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

    std::cout << hour << " " << minute << " " << second << std::endl;
}

只要您使用默认构造函数调用 Time,就会调用它:

    std::cout << "Enter the hour, minute, and second: " << std::endl;
    int hour, minute, second;
    std::cin >> hour >> minute >> second;

    Time t; //<--- this prints 0,0,0
    Time time1(hour, minute, second);

    std::cout << time1.getHour() << std::endl;
    system("pause");
    return 0;

将导致:

Enter the hour, minute, and second:
11
19
59
0 0 0
11
Press any key to continue . . .

【讨论】:

    【解决方案2】:

    我也不确定如何通过默认构造函数打印时间(0,0,0)。

    除非我遗漏了一些微妙的东西,

    // Construct a Time object using the default constructor.
    Time time2;
    
    // Print it's hour
    std::cout << time2.getHour() << std::endl;
    

    【讨论】:

    • 我认为问题在于 + 运行旧的可执行文件。
    • 编译时,它仍然不打印任何东西。
    • @Mariankka,你的编程环境是什么?你是如何运行程序的?
    【解决方案3】:

    您共享的代码不会尝试打印使用默认构造函数构造的对象,因为getHour()time1 调用。正在做你告诉它做的事情。如果你想要time1的全职工作,你必须打电话给所有的吸气剂,例如:std::cout &lt;&lt; time1.getHour() &lt;&lt; " : " &lt;&lt; time1.getMinute() &lt;&lt; " : " &lt;&lt; time1.getSecond() &lt;&lt; std::endl

    【讨论】:

      【解决方案4】:

      “我也不确定如何通过默认构造函数打印时间(0,0,0)。”

      考虑一个可以调用的displayTime() 方法来显示任何实例的时间

      int _tmain(int argc, _TCHAR* argv[]) 
      {
          std::cout << "Enter the hour, minute, and second: " << std::endl;
          int hour, minute, second;
          std::cin >> hour >> minute >> second;
      
          Time time1(hour, minute, second);         // overloaded constructor
          Time time2;                               // default constructor
      
      //  std::cout << time1.getHour() << std::endl;
      //  std::cout << time2.getHour() << std::endl;
      
          time1.displayTime();
          time2.displayTime();
      
          getch();
          return 0;
      }
      

      添加 displayTime() 方法

      void Time::displayTime() const{
      std::cout << this->hour << ","
                << this->minute << ","
                << this->second << std::endl;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-07
        相关资源
        最近更新 更多