我正在为作业写一个“Date”类,并且在执行一个函数时遇到问题。如何比较一个类中的两个对象(调用对象和参数)?
这是该类的头文件。
class Date
{
public:
Date(); // Constructor without parameters
Date(int m, int d, int y); // Constructor with parameters.
// accessors
int GetMonth(); // returns the size of the diamond
int GetDay();
int GetYear();
// mutators
bool Set(int m, int d, int y);
bool SetFormat(char f);
// standard input and output routines
void Input();
void Show();
void Increment(int numDays = 1);
int Compare(const Date& d);
private:
int month, // month variables
day, // day variable
year; // year variable
char format;
};
我想说明的成员函数是INT比较(常量日期& d)功能。我需要此函数来比较两个Date对象(调用对象和 参数),并且应该返回:-1如果调用对象按时间顺序排在前 ,如果对象是相同日期则为0,如果参数对象为 按时间顺序排列。
我试着做一个简单的if语句与==操作符,但我得到错误。
if (d1 == d2)
cout << "The dates are the same";
return (0);
对象被创建后,函数应该被称为像这样d1.Compare(D2)
预先感谢您!
谢谢,这正是我想要做的。 – 2010-02-03 20:32:17