简体   繁体   English

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

[英]How do I create a member function in a class that returns a value that is the type of an object of that class?

Here's the question: 这是问题:

Define a class called Month that is an abstract data type for a month. 定义一个名为Month的类,该类是一个月的抽象数据类型。 Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). 您的班级将有一个类型为int的成员变量来表示一个月(1表示1月,2表示2月,依此类推)。 Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month . 包括以下所有成员函数:构造函数,使用月份名称中的前三个字母作为三个参数设置月份;构造函数,使用整数作为参数设置月份(1表示1月,2表示2月,依此类推),默认构造函数,将月份作为整数读取的输入函数,将月份作为月份名称中的前三个字母读取的输入函数,将月份作为整数输出的输出函数,输出函数,输出月作为月名称中的前三个字母,成员函数返回下个月作为月类型的值。 Embed your class definition in a test program. 将类定义嵌入测试程序中。

Here's my code: 这是我的代码:

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

Sorry its indented all strange but this is what I have. 对不起,它缩进了所有奇怪的东西,但这就是我所拥有的。 My code runs just fine. 我的代码运行正常。 The only thing I don't get how to do is the last part of the question which asks to create "a member function that returns the next month as a value of type Month." 我唯一不知道怎么做的是问题的最后一部分,该问题要求创建“一个成员函数,该成员函数将下个月作为Month类型的值返回。” I am not understanding this at all. 我一点都不明白。 So in code I just output the string that is the next month. 所以在代码中,我只输出下个月的字符串。 Please help me with this part. 请帮助我这一部分。 In class we just covered classes and this is the assignment. 在课堂上,我们只讨论了课堂,这是作业。 Please be as simple and basic as possible. 请尽可能简单和基本。 I'm not too familiar with c++ or programming in general. 我不太熟悉c ++或程序设计。

Thanks. 谢谢。

"and a member function that returns the next month as a value of type Month" “以及一个成员函数,该函数将下个月作为月类型的值返回”

So basically your class Month needs to have a function that returns the following month as an object of type Month . 因此,基本上,您的类Month需要具有一个返回下个月的函数,作为Month类型的对象。 For instance, if I had an object of Month that was equal to "June", and I called a function... call it getNextMonth() it should return a Month object equal to "July". 例如,如果我有一个等于“ June”的Month对象,并且我调用了一个函数...调用它getNextMonth()它应该返回一个等于“ July”的Month对象。

So your month class needs to have a function, presumably in the public section defined as Month getNextMonth(); 因此,您的month类需要有一个函数,大概在定义为Month getNextMonth();public部分中Month getNextMonth(); I'll leave you to figure out how to define that function, but I would suggest reading up on "Copy constructors" and what happens when you return a class instance by value. 我将让您弄清楚如何定义该函数,但是我建议您阅读“复制构造函数”,以及按值返回类实例时会发生什么。 Your class doesn't require any "deep copies" of any variables, so the default copy constructor should be fine, but it's good to know what is actually happening when your code runs. 您的类不需要任何变量的任何“深层副本”,因此默认的副本构造函数应该可以,但是最好知道代码运行时实际发生的情况。

As Basic As Possible : Return types:-written before the function name when you are defining or declaring a function ex- int main();. 尽可能基本:返回类型:在定义或声明函数exmain int main();时,在函数名称之前编写。 Instead of using void or int your question demands you to use the return type as your class ie 'Month' that you have defined.So instead of returning nothing or an int you will be returning an object of class Month 而不是使用void或int,您的问题要求您使用返回类型作为您的类,即您已定义的“ Month”。因此,将不返回任何内容或int,而是返回Month类的对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写一个返回仅存在于类中的类型的成员函数? - How do I write a member function that returns a type that only exists in the class? 如何创建一个返回对该类对象的引用的静态方法? - How do I create a static method that returns a reference to an object of that class? 如何通过pthreads将类成员函数与类的对象绑定以创建线程? - How to bind a class member function with an object of the class to create a thread by pthreads? 如何在返回嵌入类型的泛型类之外实现成员函数? - How to implement a member function outside the generic class that returns an embedded type? 我如何获得一个类成员函数来访问另一个类成员函数的私有成员? - How do i get a class member function to have access to another class member function's private member? 嵌套类的成员函数返回嵌套类的类型 - Member function of nested class returns type of nested class 如何将类成员函数的返回类型设置为私有结构的对象 - How to set the Return Type of a Class Member Function as the object of a Private Struct 如何使用该对象的 static 成员 function 初始化 object? - How do I initialize an object using a static member function of that object's class? 如何在成员函数中的C ++中创建优雅的for_each(),而操作函数是同一类中的另一个成员函数? - How do I create an elegant for_each() in C++ inside a member function where the operating function is another member function in the same class? 如何通过将类的对象和成员函数传递给C ++中的另一个函数来调用类? - How do I call a class by passing it's object and member function to another function in c++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM