2015-07-18 56 views
1

下面是問題:如何在返回值爲該類的對象類型的類中創建成員函數?

定義一個名爲Month的類,它是一個月的抽象數據類型。你的班級 有一個int類型的成員變量來代表一個月份(一月份爲1,二月份爲 ,等等)。包含以下所有成員函數:使用構造函數 將月份名稱中的前三個字母設置爲三個 參數,使用整數作爲參數設置月份的構造函數(1代表 January,2代表二月等等),一個默認的構造函數,一個輸入函數,該函數將月份讀取爲一個整數,一個輸入函數將該月份作爲第一個月份的三個字母讀取月份,一個輸出函數輸出該月份 作爲整數,輸出函數將月份作爲月份的前三個字母 輸出,而將下一個月的月份返回爲 的成員函數。將你的類定義嵌入到測試程序中。

這裏是我的代碼:

#include <iostream> 
    #include <cstdlib> 
    #include <string> 
    using namespace std; 

    class Month 
    { 
    public: 
Month(char firstLetter, char secondLetter, char thirdLetter); 
//Initializes month according to arguments for first 3 letters 

Month(int month_number); 
//Initializes month according to argument for integer between 1 and 12 

Month(); 
//Initializes month to January 

void input_integer();//Reads month as integer 
void input_letter();//Reads month as first 3 letters in the name of month 
int letters_to_integer(char firstLetter, char secondLetter, char thirdLetter); 
//Converts first 3 letters of the month to integer equivalent of the month 
string integer_to_letters(int month_number); 
//Converts month integer to the first 3 letters of the name of the month 
void output_integer();//Outputs the month as an integer 
void output_letter();//Outputs the month as first 3 letters of the name of the month 

void next_month();//Returns the next month in letters 
    private: 
int month_number;//Represents the month as an integer 
string month_letters;//Represents the month in terms of its first 3 letters 
char firstLetter, secondLetter, thirdLetter; 
int next_month_number;//Represents the next month as an integer 
    }; 

    int main() 
    { 
Month month1('m', 'a', 'y'), month2(7), month3; 
cout<<"Object month1 is initialized as follows:\n"; 
month1.output_letter(); 
cout<<"Object month2 is initialized as follows:\n"; 
month2.output_letter(); 
cout<<"Object month3 is initialized as follows:\n"; 
month3.output_letter(); 

cout<<"Enter the first 3 letters of the month\n"; 
month3.input_letter(); 
cout<<"That month expressed as in integer is:\n"; 
month3.output_integer(); 
cout<<endl; 


cout<<"Enter the month as an integer between 1 and 12:\n"; 
month3.input_integer(); 
cout<<"That is equivalent to the month of:\n"; 
month3.output_letter(); 
cout<<"in letters\n"; 
cout<<"The next month is: \n"; 
month3.next_month(); 
cout<<endl; 

system("pause"); 
return 0; 
    } 

    Month::Month(char firstLetter, char secondLetter, char thirdLetter) 
    { 
month_number = letters_to_integer(firstLetter, secondLetter, thirdLetter); 
month_letters = integer_to_letters(month_number); 
    } 

    Month::Month(int month_number) 
    { 
month_letters = integer_to_letters(month_number); 
    } 

    Month::Month() 
    { 
month_number = 1; 
month_letters = "Jan\n"; 
    } 

    void Month::input_integer() 
    { 
cin>>month_number; 
month_letters = integer_to_letters(month_number); 
next_month_number = month_number + 1; 
    } 

    void Month::input_letter() 
    { 
cin>>firstLetter>>secondLetter>>thirdLetter; 
month_number = letters_to_integer(firstLetter, secondLetter, thirdLetter); 
    } 

    void Month::output_letter() 
    { 
cout<<month_letters; 
    } 

    void Month::output_integer() 
    { 
cout<<month_number; 
    } 

    int Month::letters_to_integer(char firstLetter, char secondLetter, char  thirdLetter) 
    { 
if ((firstLetter == 'j') && (secondLetter == 'a') && (thirdLetter == 'n')) 
{ 
    month_number = 1; 
} 
else if ((firstLetter == 'f') && (secondLetter == 'e') && (thirdLetter == 'b')) 
{ 
    month_number = 2; 
} 
else if ((firstLetter == 'm') && (secondLetter == 'a') && (thirdLetter == 'r')) 
{ 
    month_number = 3; 
} 
else if ((firstLetter == 'a') && (secondLetter == 'p') && (thirdLetter == 'r')) 
{ 
    month_number = 4; 
} 
else if ((firstLetter == 'm') && (secondLetter == 'a') && (thirdLetter == 'y')) 
{ 
    month_number = 5; 
} 
else if ((firstLetter == 'j') && (secondLetter == 'u') && (thirdLetter == 'n')) 
{ 
    month_number = 6; 
} 
else if ((firstLetter == 'j') && (secondLetter == 'u') && (thirdLetter == 'l')) 
{ 
    month_number = 7; 
} 
else if ((firstLetter == 'a') && (secondLetter == 'u') && (thirdLetter == 'g')) 
{ 
    month_number = 8; 
} 
else if ((firstLetter == 's') && (secondLetter == 'e') && (thirdLetter == 'p')) 
{ 
    month_number = 9; 
} 
else if ((firstLetter == 'o') && (secondLetter == 'c') && (thirdLetter == 't')) 
{ 
    month_number = 10; 
} 
else if ((firstLetter == 'n') && (secondLetter == 'o') && (thirdLetter == 'v')) 
{ 
    month_number = 11; 
} 
else if ((firstLetter == 'd') && (secondLetter == 'e') && (thirdLetter == 'c')) 
{ 
    month_number = 12; 
} 
return month_number; 
    } 

    string Month::integer_to_letters(int month_number) 
    { 
string month_letters; 

switch (month_number) 
{ 
    case 1: 
     month_letters = "Jan\n"; 
     break; 
    case 2: 
     month_letters = "Feb\n"; 
     break; 
    case 3: 
     month_letters = "Mar\n"; 
     break; 
    case 4: 
     month_letters = "Apr\n"; 
     break; 
    case 5: 
     month_letters = "May\n"; 
     break; 
    case 6: 
     month_letters = "Jun\n"; 
     break; 
    case 7: 
     month_letters = "Jul\n"; 
     break; 
    case 8: 
     month_letters = "Aug\n"; 
     break; 
    case 9: 
     month_letters = "Sep\n"; 
     break; 
    case 10: 
     month_letters = "Oct\n"; 
     break; 
    case 11: 
     month_letters = "Nov\n"; 
     break; 
    case 12: 
     month_letters = "Dec\n"; 
     break; 
    default: 
     month_letters = "Invalid integer for month"; 
     break; 

} 
return month_letters; 
    } 

    void Month::next_month() 
    { 
cout<<integer_to_letters(next_month_number); 
    } 

對不起它的縮進一切奇怪,但是這是我的。我的代碼運行得很好。我唯一不知道該怎麼做的是問題的最後一部分,它要求創建「一個會員函數,該函數返回下一個月的月份爲 的值。」我根本不理解這一點。所以在代碼中我只輸出下個月的字符串。請幫助我這部分。在課堂上,我們只講課,這是作業。請儘可能簡單和基本。我一般不太熟悉C++或編程。

謝謝。

+2

我不'不明白你對這個問題不瞭解,所以我不確定從哪裏開始解釋。 – melpomene

+1

我如何創建一個函數,返回下個月作爲類型月的值?我真的沒有得到什麼意思返回作爲類型月的值,因爲月是一個類與許多類型和功能。我怎樣才能返回下一個月,這是一個整數或字符串,作爲類的月份類型的值? –

+0

一個類是一個類型。你應該返回一個對象,而不是整數或字符串。 – melpomene

回答

0

「並返回下個月類型月份的值的成員函數」

所以基本上你的類Month需要有一個返回下一個月的Month類型的對象的功能。例如,如果我有一個Month等於「June」的對象,並且我調用了一個函數...將其稱爲getNextMonth()它應該返回一個Month對象等於「July」。

所以,你每月類需要有一個功能,想必定義爲Month getNextMonth();public節中,我將離開你弄清楚如何定義的功能,但我會建議「複製構造函數」會發生什麼讀了當你通過值返回一個類實例時。你的類不需要任何變量的「深層拷貝」,所以默認的拷貝構造函數應該沒問題,但是當你的代碼運行時知道實際發生了什麼是很好的。

+0

我們還沒有在課堂上了解複製構造函數,所以我認爲我應該沒有這個問題來解決這個問題。但是如果你看看我的代碼,在這裏粘貼的時間太長了,但是指的是輸入函數之一: void Month :: input_integer() 在這個輸入函數中,我確定下個月是什麼樣的整數。我可以添加另一行,將其轉換爲月份的字符串。隨後,我怎麼能把這個字符串放進月份getNextMonth();函數,然後在主函數中調用Month getNextMonth()函數並輸出該值? –

0

儘可能基本: 返回類型: - 當您定義或聲明函數ex-int main();時,在函數名稱前寫入。而不是使用void或int你的問題要求你使用返回類型作爲你的類,即你已經定義的'月',所以不要返回任何東西或int,你將返回一個類的對象。Month

+0

感謝您的直接答案。但在這種情況下的下個月是一個整數或字符串。我怎樣才能把它轉換成類的月份對象返回的東西?從概念上講,當你返回一個對象時會發生什麼?它是否輸出由對象中的函數確定的變量和值的所有值? –

+0

看到這是你所做的,你通過初始化一個對象來保存類的數據成員中的值。說月月;並以m爲單位保存數據。 datavariable進一步返回m。並在下個月印刷,讓你自己去想那個部分。 –

相關問題