下面是問題:如何在返回值爲該類的對象類型的類中創建成員函數?
定義一個名爲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++或編程。
謝謝。
我不'不明白你對這個問題不瞭解,所以我不確定從哪裏開始解釋。 – melpomene
我如何創建一個函數,返回下個月作爲類型月的值?我真的沒有得到什麼意思返回作爲類型月的值,因爲月是一個類與許多類型和功能。我怎樣才能返回下一個月,這是一個整數或字符串,作爲類的月份類型的值? –
一個類是一個類型。你應該返回一個對象,而不是整數或字符串。 – melpomene