【问题标题】:I'm trying to store values in variables in a class, from main() by using accessor and mutator functions我正在尝试使用访问器和修改器函数从 main() 将值存储在类中的变量中
【发布时间】:2016-05-26 16:16:23
【问题描述】:

我的老师要我使用三个访问函数 getMonth()、getDate() 和 getYear() 以及三个突变函数 setMonth()、setDate() 和 setYear()。请忽略我程序中的 cmets,因为它们可能是错误的。到目前为止,我已经编写了三个 mutator 函数(集合)。我对这些感到困惑,因此还没有编写访问器(get)函数。主要问题是,为什么它运行时只检索日期,而月份和年份显示为 0?提前致谢!

#include <iostream>
using namespace std;

class Date
{
public :
void setDate(int,int,int); //behavior
void displayDate();        //behavior
    Date();         // default constructor prototype
    Date(int, int, int); // parameterized contructor prototype
int setMonth(int); // mutator function prototype
int setDay(int);   // mutator function prototype
int setYear(int);  // mutator function prototype

private:
int month; //attributes
int day;   //attributes
int year;  //attributes
};

Date::Date() {month = 1; day = 01; year = 2012;}   // default constructor 

void Date::setDate(int num1, int num2,int num3) 
{ month = num1; //instance
day = num2;   //instance
year = num3;  //instance
}

void Date::displayDate()
{  cout<<month<<'-'<<day<<'-'<<year;
}

int Date::setMonth(int month) //accessor function to return month
{
cout<<"Enter the month: ";
cin>>month;
return month;
}

int Date::setDay(int day) //accessor function to return day
{
cout<<"Enter the day: ";
cin>> day;
return day;
}

int Date::setYear(int year) //accessor function to return year 
{
cout<<"Enter the year: ";
cin>> year;
return year;
}


main()
{
 Date anvsy;
 int month, day, year;
 cout<<"Please Enter the information for your anniversary" << endl << endl;

anvsy.setMonth(month); //call to accessor function to return month

anvsy.setDay(day); //call to accessor function to return day

anvsy.setYear(year); //call to accessor function to return year

anvsy.setDate( month, day, year);
cout<<endl;
cout<<"The Anniversary Date is on ";
anvsy.displayDate();

cin.get();
cin.get();
return 0;
}

【问题讨论】:

  • 我刚刚删除了我的答案,因为它太长并且一次解决了太多问题^^ RyanP的答案已经提到了很多:-)
  • 谢谢FrozenTarzan,哈哈:D

标签: c++ class object


【解决方案1】:

int Date::setYear(int year)

对于这些函数,您传入一个名为“year”、“month”等的变量,然后是在该函数中修改的变量(而不是修改同名的私有类变量)。

另外值得注意的是,按照您的布局方式,您不必在主函数中保留年/月/日的副本,因为这是在课堂上拥有它的重点。

也就是说,通常您希望您的类表示一个对象,并且在类中使用 cin/cout 所做的不仅仅是表示该对象。如果您想将此类带到另一个不需要您输入时输入/计算变量的项目,或者要求您从不同的源(如文件)输入它们,则必须更改所有该代码.我会将您的输入/输出与您的类分开(在这种情况下,可能在 main() 中),并让您的 set 函数接受输入并使用该输入来更新内部私有变量,而 get 函数只需返回该值。

【讨论】:

  • 我确实在尝试将我从用户那里获得的输入存储到内部私有变量月、日和年中。我离得那么远吗?我知道我正在使用额外的错误变量。谢谢!
  • 离你不远了。只是注意到这个日期类应该能够有多种用途。当你设计它时,你应该让它通用,这样如果你在未来的另一个项目中需要它,你就可以使用它。为了使其可重用,您应该尝试使类的接口尽可能通用(换句话说,不要做出假设......您知道您会想要设置天/月/年,但是您不想将自己与课堂中使用的 cin 联系在一起。其他项目可能需要来自文件或其他地方的输入)这是我的意思的一个例子:ideone.com/e1RPYJ
  • 哇,非常感谢。我脑子里真的有很多复杂的事情,而且很困惑。这真的很清楚。感谢您抽出宝贵的时间。再次感谢!!!我最后的困惑是:我们创建的三个函数(setYear、setDay、setMonth)会被认为是 mutator 还是 accessor 函数?我将它们命名为 mutator 函数,但也许它们应该命名为 getYear 等。
  • 我想你可以将它们称为 mutator/accessor... 我总是称它们为 get/set 函数。您需要一个设置年份的函数void setYear( int _year) 和一个返回年份的函数int getYear()。
  • 顺便说一句,如果你想找点乐子,你应该查找“C++ 重载流操作符”,你实际上可以这样做,以便在main() 而不是anvsy.displayDate(); 你可以写cout &lt;&lt; "The Anniversary Date is on " &lt;&lt; anvsy &lt;&lt; endl;它会做你想做的事。然后,如果您正在写入文件,filestream &lt;&lt; anvsy 仍然有效!
【解决方案2】:

RyanP 是绝对正确的,类和数据的输入方式可以通过重构来更好地分离职责。

也就是说,您的代码无法正常工作的主要原因是当输入日、月和年时,您会返回这些值。但在main 中,您似乎打算通过引用传递变量。这些方法中的任何一种都是有效的,但您必须选择其中一种。请参阅下面的两个示例,您可以使用其中任何一个:

通过引用传递:

// Note the & after int - this indicates the paramater is now passed by reference
// No need for return type
void Date::setMonth(int& month)
{
  cout<<"Enter the month: ";
  cin>>month;
  //return month; // Don't need this line. Function now returning void
}

// Pass month by reference
anvsy.setMonth(month);

或者这个,使用返回值:

// No need for any parameters
int Date::setMonth()
{
  cout<<"Enter the month: ";
  cin>>month;
  return month;
}

// Use return value
month = anvsy.setMonth();

您提到 cmets 可能不正确 - 我认为您对什么是访问器和什么是突变器有点困惑,因为这些函数没有明确定义的职责。 mutator 应该只设置成员变量的值,而 accessor 应该只返回成员变量的值。任何其他职责(例如用户输入)应在其他地方处理

【讨论】:

    猜你喜欢
    • 2019-04-06
    • 1970-01-01
    • 2014-08-30
    • 2020-03-01
    • 2018-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多