【问题标题】:cout a variable with a class definitioncout 具有类定义的变量
【发布时间】:2013-10-20 19:34:32
【问题描述】:

所以在我的程序中,我有两个变量,分别称为今天和生日。这两个变量都是 DayOfYear 类型。 DayOfYear 类同时接受日期和月份。因此,当我同时调用 today.setday 和 today.setmonth 以及birthday.setday 和birthday.setmonth 时,它会将值分配给类为每个变量创建的私有变量。我的老师希望我能够输入主函数 cout<<today 并让它打印这个变量的日期和月份我将如何设置它?

 #include <iostream>
using namespace std;

class DayOfYear
{
    public:
        DayOfYear( );
        void setDay( );
        void setMonth( );
        void setYear( );
        void output( );
        int getMonth( );
        int getDay( );

    private:
        int tempday;
        int month;
        int day;
        int year;
        int leapyear;
        int cont;
};

int main()
{
    DayOfYear today, birthday;

    cout <<"what year were you born" <<endl;
    birthday.setYear( );
    cout <<"what year is it now" <<endl;
    today.setYear( );

    cout << "Enter today's date:\n";
        today.setMonth( );
        today.setDay( );

    cout << "Enter your birthday:\n";
        birthday.setMonth();
        birthday.setDay( );

    cout << "Today's date is ";
    today.output( );
    cout << "Your birthday is ";
    birthday.output( );

    if (today.getMonth( ) == birthday.getMonth( ) && today.getDay( ) == birthday.getDay( )) 
    // remove .operators (make boolean friend function that takes care of everything
    //this is where i need the help

        cout << "Happy Birthday!\n";
    else
        cout << "Happy Unbirthday!\n";

    return 0;
}
DayOfYear::DayOfYear( )
{
    month = 1;
    day = 1;
    year = 1970;
    leapyear = 0;
    tempday = 0;
}

void DayOfYear::output( )
{
    cout << "month = " << month
         << ", day = " << day << endl;
}

void DayOfYear::setYear( )
{
    cin >> year;

    if (year%400 == 0 || year%4 == 0 && year%100 != 0)
    {
        leapyear = 1; //set leapyear to true if conditions are met
    }

}

void DayOfYear::setDay()
{

    do 
    {

        cout << "Enter the day of the month: ";
        cin >> tempday;

        if(tempday > 28 && month == 2 && leapyear == 0)
        {
            cout << "invlaid day" << endl;
            tempday = 0;
        }

        else if(tempday > 30 && month == 4 ||tempday > 30 &&  month == 6 || tempday > 30 && month == 9 || tempday > 30 && month == 11)
        {
            cout << "invlaid day" << endl;
            tempday = 0;
        }

        else if(tempday > 31 && month == 1 || tempday > 31 && month == 3 ||tempday > 31  && month == 5 || tempday > 31 && month == 7 ||
                tempday > 31 && month == 8 || tempday > 31 && month == 10 || tempday > 31 && month == 12)
        {
            cout << "invlaid day" << endl;
            tempday = 0;
        }

        else
        {
            day = tempday;
        }

    } while(tempday == 0);
}

void DayOfYear::setMonth( )
{
    double invalid;

    do 
    {
        cout << "Enter month as a number: ";
        cin >> month;
        if (month > 12 )
        {
            cout << "invalid month" << endl;
            invalid = 1;
        }
        else if (month <= 12 && month >= 1)
        {
            invalid = 0;
        }

    } while (invalid == 1);

}
int DayOfYear::getMonth( ) 
{
    return month;
}

int DayOfYear::getDay( ) 
{
    return day;
}

【问题讨论】:

    标签: c++ class object cout


    【解决方案1】:

    你需要重载操作符

    std::ostream& operator<<(std::ostream& os, const DayOfYear& d)
    {
        os << "month = " << d.month << ", day = " << d.day << std::endl;
        return os;
    }
    

    有关更多信息,另请参阅this question

    也将其设为DayOfYear 的朋友:

    friend std::ostream& operator<<(std::ostream&, const DayOfYear&);
    

    这是确保操作员可以访问您的 DayOfYear 类的私有成员所必需的。

    我还看到,在源代码中,您提到您还需要重载operator==。原理是一样的,我链接的问题也应该对此有所帮助。

    【讨论】:

      【解决方案2】:
      class DayOfYear
      {
      public:
          friend bool operator ==(const DayOfYear& lhs, const DayOfYear& rhs) const
          {
              return lhs.getMonth() == rhs.getMonth()
                  && lhs.getDay()   == rhs.getDay();
          }
      
          ...
      };
      
      ...
      
      if (today == birthday)
      {
          // ...
      }
      

      【讨论】:

        猜你喜欢
        • 2017-07-18
        • 2015-07-20
        • 2020-03-06
        • 2023-03-15
        • 1970-01-01
        • 2020-03-18
        • 1970-01-01
        • 1970-01-01
        • 2011-04-29
        相关资源
        最近更新 更多