如何在类中创建一个成员函数,该类返回一个值,该值是该类对象的类型?

时间:2015-07-18 22:10:16

标签: c++ class oop object

以下是问题:

定义一个名为Month的类,它是一个月的抽象数据类型。你的课会 有一个类型为int的成员变量来表示一个月(1表示1月,2表示1月 二月,等等)。包括以下所有成员函数:构造函数 使用月份名称中的前三个字母将月份设置为三 arguments,一个构造函数,用于使用整数作为参数设置月份(1表示 1月,2月2日,等等),默认构造函数,输入函数 将月份作为整数读取,输入函数将月份作为第一个读取 月份名称中的三个字母,输出月份的输出函数 作为整数,输出函数,输出月份作为前三个字母 在月份的名称,以及返回下个月的成员函数 类型为Month的值。将您的类定义嵌入测试程序中。

这是我的代码:

    #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);
    }

对不起,它缩进了所有奇怪但这就是我所拥有的。我的代码运行得很好。我唯一不知道该怎么办的问题的最后一部分要求创建“一个成员函数,它将下个月作为 一个类型为Month的值。“我根本不理解这一点。所以在代码中我只输出下个月的字符串。请帮我这个部分。在课堂上我们只是涵盖了课程,这就是作业。请成为尽可能简单和基本。我对c ++或编程不太熟悉。

感谢。

2 个答案:

答案 0 :(得分:0)

“和一个成员函数,它返回下个月作为Month类型的值

所以基本上你的类Month需要有一个函数,它将下个月作为Month类型的对象返回。例如,如果我有Month的对象等于“June”,并且我调用了一个函数...调用它getNextMonth()它应该返回一个Month对象等于“七月”。

所以你的月课需要有一个函数,大概在public部分定义为Month getNextMonth();我会让你弄清楚如何定义这个函数,但我建议你阅读“复制构造函数”以及按值返回类实例时会发生什么。您的类不需要任何变量的“深层副本”,因此默认的复制构造函数应该没问题,但是知道代码运行时实际发生的情况是很好的。

答案 1 :(得分:0)

尽可能基本: 返回类型:在定义或声明函数ex-int main();时,在函数名前写入。而不是使用void或int您的问题要求您使用返回类型作为您的类,即“月份”。你已经定义了。所以你不会返回任何东西,也不会返回一个int,而是返回一个Month类的对象