[英]Why can't I change the class variable “name” from my setMonth function?
這是一個作業問題。 我可以使其正常工作,但我不想在每個重載的運算符函數中都放置一個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
對象上設置月份名稱。
您需要將其實現為成員函數,以設置當前對象的成員。
就是說,通常,月份對象不需要保留名稱,因為可以根據需要給定月份號來檢索它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.