2010-02-03 41 views
5

我正在为作业写一个“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)

预先感谢您!

回答

10
int Date :: Compare (const Date& d) { 

    if (year<d.year) { 
     return -1; 
    } 
    else if (year>d.year) { 
     return 1; 
    } 
    else if (month<d.month) { 
     return -1; 
    } 
    else if (month>d.month) { 
     return 1; 
    } 
    // same for day 

    return 0; 
} 

通常,you'lll还希望提供重载比较运算符,例如(也内类的定义):

bool operator == (const Date& d) const { 
    return !Compare(d); 
} 

bool operator < (const Date& d) const { 
    return Compare(d)<0; 
} 

... // consider using boost::operators 

PS:有的Compare()聪明实现 - 只需检查其它答案。这一个非常直接和可读,但完全符合您的规范。

+0

谢谢,这正是我想要做的。 – 2010-02-03 20:32:17

5

到类的public

bool operator==(const Date& rhs) const { 
    return 
     year == rhs.year 
     && month == rhs.month 
     && day == rhs.day 
    ; 
} 
+1

请注意,该方法是const。你的Compare()方法(和其他)也应该是const。如果你的Compare()是const,我可以从运算符==():-) – Notinlist 2010-02-03 19:24:39

4

通过内容比较对象,即你的情况的日期是相等的日,月,年的是相等的(也许format - 根据您的语义) 。

另外,C++已经包含了很好的对象比较功能:operator ==它允许编写比调用Compare方法更清晰的代码。

顺便说一句,请注意这一点:

if (d1 == d2) 
    cout << "The dates are the same"; 
    return (0); 

如果条件为真,cout线将被执行。即使条件为假,return也会执行。

+1

+1中调用它,很好地捕获返回语句。 – Void 2010-02-03 19:36:57

0

你不能这样做d1 === d2,因为我相信它会比较内存地址(在一段时间内还没有完成C++)。

你需要做的是编写一个函数,它会比较Date类的每个成员并返回负数,0或正数。负数表示较小,0表示相同,正数表示较大。

例如,在Java:

public int compareTo(Date date) { 
    int returnValue = 0; 

    returnValue = this.getYear() - date.getYear(); 

    if(returnValue == 0) { 
     returnValue = this.getMonth() - date.getMonth(); 

     if(returnValue == 0) { 
     returnValue = this.getDay() - date.getDay(); 
     } 
    } 
} 
+2

d1 == d2不会编译,除非该操作在某处执行。在这种情况下,它没有实现。 – 2010-02-03 19:33:45

+0

OP显式声明“...函数应该像这样调用d1.Compare(d2)”,这意味着d1和d2都不是内存地址(指针)。在这种情况下,比较内存地址将需要类似“&d1 ==&d2”,而不是“d1 == d2”。创建一个相等运算符,如发布的一个@Notinlist提供所需的d1 == d2语义。 – Void 2010-02-03 19:39:26

+0

谢谢你的“这个”指针,我没有想到这一点。 – 2010-02-03 19:40:18

1

为了使用==操作符用户定义的类型,你必须实现它。此外,你的比较函数应该被标记为const成员函数:

class Date 
{ 
... 
int Compare(const Date& d) const;  

bool operator==(const Date& rhs) const 
{ 
    return 0 == Compare(rhs); 
} 
4

C++的||化妆的语义这有点混乱:

static inline int cmp(int a, int b) 
{ 
    return a < b ? -1 : a == b ? 0 : 1; 
} 

int Date::Compare(const Date& d) 
{ 
    int result; 
    (result = cmp(year, d.year))  || 
    (result = cmp(month, d.month)) || 
     (result = cmp(day, d.day)); 

    return result; 
} 
+0

谢谢,这也有帮助。 – 2010-02-03 20:32:37

+0

@jualin不客气! – 2010-02-03 20:39:14

+0

但是,如果您确实必须以下划线开头,请确保它不在全局名称空间中 - 这是不允许的。 – 2010-02-03 22:23:26

7

下面是我会实现你的比较功能,虽然格式需要一段时间来适应:

int Date::Compare(const Date& d) const { 
    return 
    (year < d.year) ? -1 : 
    (year > d.year) ? 1 : 
    (month < d.month) ? -1 : 
    (month > d.month) ? 1 : 
    (day < d.day)  ? -1 : 
    (day > d.day)  ? 1 : 
         0; 
} 

或许:

template<typename T> 
int Compare(T a, T b) { 
    if (a < b) return -1; 
    if (b < a) return 1; 
    return 0; 
} 

int Date::Compare(const Date& d) const { 
    int a = Compare(year, d.year); 
    if (a == 0) a = Compare(month, d.month); 
    if (a == 0) a = Compare(day, d.day); 
    return a; 
} 

在比较中,我不会使用operator==,但如果您也想这样,告诉你如何实施operator==的答案也可以。原因是operator==显然要看看相同的字段比较,如果它返回false,比较将再次做非常类似的工作。效率可能不是问题,但它重复了逻辑。

为了实现它的价值,惯用的C++实现operator<,也可能是一致的operator==operator>,而不是一个全功能的比较函数。运算符是标准算法用于搜索和排序的内容,以及其他所有内容。 Java选择以不同的方式做事。

+0

+1。真棒回答。 – ChadNC 2010-02-03 20:29:50

+0

谢谢,这是一个很好的解释。 – 2010-02-03 20:34:17