gpt4 book ai didi

c++ - 为什么我不能从我的 setMonth 函数中更改类变量 "name"?

转载 作者:行者123 更新时间:2023-11-28 07:08:04 26 4
gpt4 key购买 nike

这是一道作业题。我可以让它工作,但我不想在每个重载的运算符函数中都放置一个 switch 语句。我正在处理的是 Month Month::operator--(int) 函数。当我调用 setMonth() 并尝试更改名称时,它并没有改变。

// Name: Rocky Eadie
// Date: 1/29/2014
// Description: this program sets and retreives data from a class file

//includes
#include <iostream>
#include <string>
#include "Header.h"

using namespace std;

//begin main function
int main()
{

//create month object
Month MonthObject;

//print contents of month object
cout << MonthObject;

//overloaded operator call
cin >> MonthObject;

//print contents of month object
cout << MonthObject;

//overloaded prefix increment operator
++MonthObject;

//print contents of month object
cout << MonthObject;

//overloaded postfix increment operator
MonthObject++;

//print contents of month object
cout << MonthObject;

//overloaded prefix decrement operator
--MonthObject;

//print contents of month object
cout << MonthObject;

//overloaded postfix increment operator
MonthObject--;

//print contents of month object
cout << MonthObject;

system("pause");
return 0;

//end main function
}

//functions



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

//prototypes
void setMonth(int);

class Month
{

//function prototypes

private:
//variables
string name;
int monthNumber;
public:
//constructors
Month()
{
monthNumber = 1;
name = "January";
}

Month(string n)
{
name = n;
}

Month(int mN)
{
monthNumber = mN;
}

//accessors and mutators
int getMonth()
{
return monthNumber;
}

void setMonth(int mN)
{
monthNumber = mN;
}

string getName()
{
return name;
}

void setName(string n)
{
name = n;
}

//prefix overload addition
Month Month::operator++()
{

++monthNumber;

//month limit test
if (monthNumber>12)
{
monthNumber = 1;
}

//sets month name
switch (monthNumber)
{
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
break;
}

return *this;
//end prefix overload addition
}

//postfix overload addition
Month Month::operator++(int)
{

Month temp(monthNumber);

monthNumber++;

//month limit test
if (monthNumber>12)
{
monthNumber = 1;
}

//sets month name
switch (monthNumber)
{
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
break;
}

return temp;

}

//prefix overload subtraction
Month Month::operator--()
{

--monthNumber;

//month limit test
if (monthNumber<1)
{
monthNumber = 12;
}

//sets month name
switch (monthNumber)
{
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
break;
}

return *this;
//end prefix overload addition
}

//postfix overload subtraction
Month Month::operator--(int)
{

Month temp(monthNumber);

monthNumber--;

//month limit test
if (monthNumber<1)
{
monthNumber = 12;
}

setMonth(monthNumber);

return temp;

//end post overload subtraction
}

//cout operator overload
friend ostream &operator << (ostream &strm, const Month &obj)
{
strm << "Month name: " << obj.name << endl << "Month number: " << obj.monthNumber << endl;
return strm;
}

//cin operator overload
friend istream &operator >> (istream &strm, Month &obj)
{
cout << "Enter month name: " ;
strm >> obj.name;

cout << "Enter month number: " ;
strm >> obj.monthNumber;

return strm;
}




//end class Month
};

void setMonth(int n)
{

//local variables
string name;
Month obj;

//sets month name
switch (n)
{
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
break;
}

//sets month name in Month class
obj.setName(name);



}

最佳答案

您的 setMonth() 函数是一个免费函数,它在名为 obj 的临时 Month 对象上设置月份名称。

您需要将其实现为成员函数以设置当前对象的成员。

也就是说,通常月份对象不需要保存名称,因为它可以根据需要给定月份编号来检索。

关于c++ - 为什么我不能从我的 setMonth 函数中更改类变量 "name"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com