编辑!!
我正在自学 C++,并且只学习类。为了练习,我创建了一个 Date
类的一些方法。我目前正在尝试练习这个问题:
Use the
Date
class to create a method that compares two Date objects and returns the larger of the two.
所以我已经在下面制作了一些代码和方法。这是我感到困惑的地方:当你调用一个方法时,它的形式是:<object>.<method>
.假设我有两个 Date 对象 date1
和 date2
.我不明白如何创建一个采用两个日期对象的日期方法,因为该方法已经作用于其中一个对象。换句话说,我希望有这样的东西:date1.compareDate(date2)
并且此方法会返回具有更大值的对象。有人可以解释一下我是如何解决这个问题的吗?
我试图在我的方法中这样做:compareDate()。我做了另一种方法转换,它创建了一个 YYYYMMDD 形式的整数,我可以只使用简单的 bool 逻辑来比较更大的值。
请注意我要退回较大的 Date
对象而不是 bool 值。
提前谢谢你。
#include <iostream>
#include <iomanip>
using namespace std;
// Class declaration statement
class Date
{
private:
int month;
int day;
int year;
public:
Date(int = 7, int = 4, int = 2012); // Constructor
void setDate(int, int, int); // Member to copy a date
void showDate(); // Member method to display date
int convert();
bool leapYear();
string dayOfWeek();
void nextDay();
void priorDay();
Date compareDate(Date, Date);
};
// Class Implementation section
Date::Date(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::setDate(int mm, int dd, int yyyy)
{
month = mm;
day = dd;
year = yyyy;
}
void Date::showDate()
{
cout << "The date is ";
cout << setfill('0')
<< setw(2) << month << '/'
<< setw(2) << day << '/'
<< setw(2) << year % 100;
cout << endl;
}
int Date::convert()
{
// Convert date to the integer format: YYYYMMDD
return year*10000 + month*100 + day;
}
bool Date::leapYear()
{
if( (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) )
return true;
else
return false;
}
string Date::dayOfWeek()
{
int dayInt;
int mm, yyyy, dd, YYYY;
int century, T, dayOfWeek;
yyyy = year;
mm = month;
dd = day;
if( mm < 3 )
{
mm = mm + 12;
yyyy = yyyy - 1;
}
century = int( yyyy/100 );
YYYY = yyyy%100;
T = dd + int( 26 * (mm + 1)/10) + YYYY + int(YYYY/4) + int( century/4)
-2*century;
dayOfWeek = T % 7;
if( dayOfWeek < 0 )
dayOfWeek += 7;
if( dayOfWeek == 0 )
return "Saturday";
else if( dayOfWeek == 1)
return "Sunday";
else if( dayOfWeek == 2)
return "Monday";
else if( dayOfWeek == 3)
return "Tuesday";
else if( dayOfWeek == 4)
return "Wednesday";
else if( dayOfWeek == 5)
return "Thursday";
else if( dayOfWeek == 6)
return "Friday";
else
{
cout << "dayOfWeek = " << dayOfWeek << endl;
return "Bad dayOfWeek Calc";
}
}
void Date::nextDay()
{
// Increment month and day if at 31 days in month
// Skip December because have to increment year too
if( (month == 1 || month == 3 || month == 5 || month == 7 ||
month == 8 || month == 10) && day == 31 )
{
month++;
day = 1;
}
// Increment month and day if at 30 days in month
else if( (month == 4 || month == 6 || month == 9 || month == 11)
&& day == 30)
{
month++;
day = 1;
}
// New year
else if( month == 12 && day == 31 )
{
month = 1;
day = 1;
year++;
}
// Leap year
else if( leapYear() && month == 2 && day == 29 )
{
month = 3;
day = 1;
}
// Not leap year
else if( !leapYear() && month == 2 && day == 28)
{
month = 3;
day = 1;
}
// Regular day
else
day++;
}
void Date::priorDay()
{
// Increment month and day if at 31 days in month
// Skip December because have to increment year too
if( (month == 5 || month == 7 ||
month == 8 || month == 10 || month == 12) && day == 1 )
{
month--;
day = 30;
}
// Increment month and day if at 30 days in month
else if( (month == 2 || month == 4 || month == 6 || month == 9 || month == 11)
&& day == 1)
{
month--;
day = 31;
}
// beginning year
else if( month == 1 && day == 1 )
{
month = 12;
day = 31;
year--;
}
// Leap year
else if( leapYear() && month == 3 && day == 1 )
{
month--;
day = 29;
}
// Not leap year
else if( !leapYear() && month == 3 && day == 1)
{
month--;
day = 28;
}
// Regular day
else
day--;
}
Date Date::compareDate(Date date1, Date date2)
{
// Return the greater date
if( date1.convert() > date2.convert() )
return date1;
else
return date2;
}
int main()
{
Date c(4,1,2000);
Date d(11, 1, 2013);
b.setDate(12,25,2009);
cout << Date.compareDate(c,d) << endl;
return 0;
}
最佳答案
您可以使用运算符(您应该自己编写)比较您的 Date 对象
friend bool operator> (const Date& D1, const Date& D2)
{
return D1.convert() > D2.convert();
}
当且仅当 D1 > D2 时,此运算符应该返回 true。如果此运算符需要访问 Date 对象的私有(private)部分,则必须在 Date 类中将其声明为 friend。
关于c++ - 创建比较两个对象的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20206396/