创建一个比较两个对象的方法
Creating a method that compares two objects
编辑
我在自学C++,只是在学习课程。为了练习,我用一些方法创建了一个Date
类。我目前正在尝试练习这个问题:
使用
Date
类创建一个方法,该方法比较两个Date对象并返回两者中较大的一个。
所以我已经有了一些我在下面制作的代码和方法。这里是我感到困惑的地方:当你调用一个方法时,它的形式是:<object>.<method>
。假设我有两个Date对象date1
和date2
。我不知道如何创建一个接受两个日期对象的日期方法,因为该方法已经作用于其中一个对象。换句话说,我本来希望有这样的东西:date1.compareDate(date2)
和这个方法会返回具有更大值的对象。有人能解释一下我是怎么做这个问题的吗?
我试图在我的方法compareDate()中做到这一点。我做了另一个方法convert,它创建了一个YYYYMMDD形式的整数,我可以使用简单的布尔逻辑来比较较大的值。
请注意,我希望返回较大的Date
对象,而不是布尔值。
提前谢谢。
#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对象的私有部分,则必须在Date类中将其声明为friend。
首先,让所有不会改变类常量的函数:
int convert() const;
将以下操作员添加到您的类中:
bool operator< (const Date &other) const
{
return convert() < other.convert();
}
Alexey的代码也可以工作,但我更喜欢这个,原因有两个:第一,当可以添加成员函数时,不应该引入友元函数;如果要使用stl容器或算法,则比operator>更重要。stl::set、stl:;map、stl:::sort均使用运算符<。
如前所述,您可以创建一个需要两个Date
的(独立的)函数。
您还可以创建一个Date
方法,该方法忽略this
,获取并比较两个日期,然后返回其中一个日期。这就是你所做的。但是您仍然必须通过实例调用此方法。同时补偿<<
过载的缺失:
c.compareDate(c,d).showDate();
工作。但当然,这将是一种糟糕的做法,因为您要的是一个不需要任何实例的实例(c.
)。
你想做什么,
Date.compareDate(c,d)
表明您正在寻找静态方法。你需要这样申报:
class Date {
...
static Date compareDate(Date, Date);
};
并这样称呼他们:
Date::compareDate(c,d).showDate();
(您也可以有一个方法Date compareDate(Date)
,将this
与参数进行比较,但这也是Bad。)
--
您应该为compareDate
选择一个更好的名称,因为目前还不清楚它是否返回了一个全新的Date
对象。
请注意,这里之前的所有答案(以及我的第一个版本)都错误地认为您想要返回bool
。这是因为应该这样做比较,最好是在operator<
过载的情况下。
- 使用std::函数映射对象方法
- 正在调用shared_ptr对象方法
- 检查哪个对象调用了另一个对象的对象方法
- 如何在C++中循环访问未知对象方法?
- c++ 替换调用对象方法的宏函数
- 我能否通过将函数实现为类对象方法来避免使用互斥锁
- 我可以制作一个对象方法,如果单独调用,它将自行修改,但如果在复制初始化期间调用,则会返回一个新对象?
- 如何让两个不同的对象方法相互用作参数
- 在创建 c++ 期间调用对象方法
- 扩展中的日期时间对象方法C++问题
- 将对象方法从向量应用于C 的第二个向量中的对象
- 将 C++/CLI 对象指针传递给本机对象方法
- 多态性的面向对象方法
- 将对象方法传递到变量
- C :使用Main中定义的对象方法的函数
- 有关使用矢量迭代器访问对象方法的问题
- 定义可以执行对象方法和独立函数的函数时出现问题
- 调用列表中子对象方法的最佳方法
- main.cpp:(.text+0x8f):对[对象方法]的未定义引用
- cpp 从需要超类对象的函数访问子类对象方法