2014-04-03 29 views
0
#include <iostream> 

using namespace std; 

class NumDays 
{ 
private: 
    int hour; 
    int day; 
    void simplify(); 

public: 
    NumDays() 
    { 
     day = 0; 
     hour = 0; 
    } 

    void setData(int d, int h) 
    { 
     hour = h; 
     day = d; 
     simplify(); 
    } 

    int getHour() 
    { 
     return hour; 
    } 

    int getDay() 
    { 
    return day; 
    } 

    NumDays operator++(int); 
    NumDays operator--(int); 

}; 

void NumDays::simplify() 
{ 
    hour = 8*day + hour; 
    day = hour/8; 
    hour = hour % 8; 
} 

NumDays NumDays::operator++(int) 
{ 
    NumDays obj1; 

    hour++; 
    simplify(); 
    return obj1; 
    } 

NumDays NumDays::operator--(int) 
{ 
    NumDays obj1; 

    hour--; 
    simplify(); 
    return obj1; 
} 

void setFirst(NumDays &); 
void setSecond(NumDays &); 

void addData(NumDays &, NumDays &, NumDays &); 

int main() 
{ 
    NumDays first, second, third; 

    setFirst(first); 
    setSecond(second); 

    addData(first, second, third); 
} 

void setFirst(NumDays &obj1) 
{ 
    int day, hour = 0; 
    cout << "Please enter the amount of days followed by hours." << endl; 
    cin >> day >> hour; 
    obj1.setData(day, hour); 
} 

void setSecond(NumDays &obj2) 
{ 
    int day, hour = 0; 
    cout << "Please enter the amount of days followed by hours again." << endl; 
    cin >> day >> hour; 
    obj2.setData(day, hour); 
} 

void addData(NumDays &obj1, NumDays &obj2, NumDays &obj3) 
{ 
    for (int k = 0; k < 8; k++) 
    { 
    obj3 = obj1++; 
    cout << " First Data: " << obj3.getDay() << " day(s), " 
     << obj3.getHour() << " hour(s)."; 
    cout << " First Data: " << obj1.getDay() << " day(s), " 
     << obj1.getHour() << " hour(s).\n"; 
    } 

    cout << endl; 

    for (int l = 0; l < 8; l++) 
    { 
    obj3 = obj2++; 
    cout << " Second Data: " << obj3.getDay() << " day(s), " 
     << obj3.getHour() << " hour(s)."; 
    cout << " Second Data: " << obj2.getDay() << " day(s), " 
     << obj2.getHour() << " hour(s).\n"; 
    } 
} 

當我運行該文件時,obj3有0天0小時,obj2增加。這是怎麼回事? 當我嘗試它作爲obj3 = obj2沒有任何後綴的跡象時,它複製它。所以我假設從obj2獲取數據沒有問題。但是,當我包括後綴運算符,當修改操作員的內部小時的數據變爲0和0重載postfix操作符不起作用

+0

這可能幫助:http://stackoverflow.com/questions/4421706/operator-overloading?rq=1 –

+3

你的運營商都斷了。出於某種原因,它們返回一個默認構造的對象。 –

+0

好吧,我修好了...... 我把(* this)添加到NumDays obj1; 並使其NumDays obj1(* this); 它現在就是這樣工作的。 – user3347541

回答

0

++()你是改變的這 - 值>小時。完全獨立於*的obj1的內容沒有改變。你想擺脫obj1。

我不知道你想回到這裏......如果返回小時的「簡化」的價值,因爲我們預計++返回比它始於一個價值1時這將是非常直觀的。這違反了最小驚訝原則。 :)

那麼你真的應該創建一個新的功能,而不是覆蓋運營++。或者,如果你重寫operator ++,那麼不要在裏面調用simplify()。

1

你已經明白了。首先,我對你的班級做了一些改變。

class NumDays 
{ 
private: 
    int hour; 
    int day; 
    void simplify(); 

public: 
    NumDays(int dy = 0, int hr = 0) : hour(hr), day(dy) { simplify(); } 
    void setData(int d, int h) 
    { 
     hour = h; 
     day = d; 
     simplify(); 
    } 
    int getHour() const { return hour; } 
    int getDay() const { return day; } 
    friend ostream& operator<<(ostream& out, const NumDays &nd) { 
    return out << " First Data: " << nd.getDay() << " day(s), " 
     << nd.getHour() << " hour(s)."; 
    } 
    NumDays operator++(int); 
}; 

我做的第一件事是重寫構造函數以使其具有默認參數並使用現代風格。

我做的第二件事是添加const到不(也不應該)修改對象的各種成員函數。

我做的第三件事是添加一個ostream提取器作爲friend函數。這純粹是爲了方便解決問題。

下面介紹一下後置運營商的新版本是這樣的:

NumDays NumDays::operator++(int) 
{ 
    NumDays obj1(*this); 
    hour++; 
    simplify(); 
    return obj1; 
} 

唯一的區別是,obj1創建現在使用默認的編譯器生成的拷貝構造函數。這很重要,因爲我們需要爲postincrement運算符返回未增量值的副本。 (你的後綴減量運營商需要一個類似的修復。)

我不需要做任何改變你的日常simplify()

我然後用這個代碼,測試它:

int main() 
{ 
    NumDays nd(2,3); 
    cout << nd++ << endl; // prints 2, 3 
    cout << nd << endl;  // prints 2, 4 
    return 0; 
} 

現在一切正常。