2010-02-03 38 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