Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ I';我试图使用accessor和mutator函数从main()将值存储在类的变量中_C++_Class_Object - Fatal编程技术网

C++ I';我试图使用accessor和mutator函数从main()将值存储在类的变量中

C++ I';我试图使用accessor和mutator函数从main()将值存储在类的变量中,c++,class,object,C++,Class,Object,我的老师希望我使用三个访问器函数getMonth()、getDate()和getYear(),以及三个mutator函数setMonth()、setDate()和setYear()。请忽略我的程序中的注释,因为它们可能是错误的。到目前为止,我已经编写了三个mutator函数(集合)。我把这些弄糊涂了,所以还没有编写访问器(get)函数。主要的问题是,为什么它运行时只检索日期,而月份和年份显示为0?提前谢谢 #include <iostream> using namespace std

我的老师希望我使用三个访问器函数getMonth()、getDate()和getYear(),以及三个mutator函数setMonth()、setDate()和setYear()。请忽略我的程序中的注释,因为它们可能是错误的。到目前为止,我已经编写了三个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;
}
#包括
使用名称空间std;
上课日期
{
公众:
void setDate(int,int,int);//行为
void displayDate();//行为
Date();//默认构造函数原型
日期(int,int,int);//参数化构造函数原型
int setMonth(int);//mutator函数原型
int setDay(int);//mutator函数原型
int setYear(int);//mutator函数原型
私人:
int month;//属性
int day;//属性
int year;//属性
};
Date::Date(){month=1;day=01;year=2012;}//默认构造函数
作废日期::设置日期(整数m1、整数m2、整数m3)
{month=num1;//实例
day=num2;//实例
year=num3;//实例
}
作废日期::显示日期()
{cout
int-Date::setYear(int-year)

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

同样值得注意的是,按照你的安排方式,你不应该在你的主要函数中保留一份年/月/日的副本,因为这是在课堂上使用它的意义所在

也就是说,一般来说,您希望您的类表示一个对象,在类中使用cin/cout不仅仅是表示对象。如果您希望将该类带到另一个项目中,而该项目不要求您在输入变量时cin/cout,或者要求您从其他源(如文件)输入变量您必须更改所有这些代码。我会将您的输入/输出与类分开(在本例中,可能是在main()中),并让您的set函数接受输入并使用该输入更新内部私有变量,get函数只返回该值。

int-Date::setYear(int-year)

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

同样值得注意的是,按照你的安排方式,你不应该在你的主要函数中保留一份年/月/日的副本,因为这是在课堂上使用它的意义所在


也就是说,一般来说,您希望您的类表示一个对象,在类中使用cin/cout不仅仅是表示对象。如果您希望将该类带到另一个项目中,而该项目不要求您在输入变量时cin/cout,或者要求您从其他源(如文件)输入变量您必须更改所有这些代码,我会将您的输入/输出与类分开(在本例中,可能是在main()中)让set函数接受一个输入并使用该输入更新内部私有变量,get函数只返回该值。

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);
//注意int之后的&after-这表示参数现在通过引用传递
//不需要返回类型
作废日期::setMonth(整数和月份)
{
每月一次;
//return month;//不需要此行。函数现在返回void
}
//通过引用传递月份
anvsy.setMonth(月);
或者,使用返回值:

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

// Use return value
month = anvsy.setMonth();
//不需要任何参数
int Date::setMonth()
{
每月一次;
返回月份;
}
//使用返回值
month=anvsy.setMonth();

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

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);
//注意int之后的&after-这表示参数现在通过引用传递
//不需要返回类型
作废日期::setMonth(整数和月份)
{
每月一次;
//return month;//不需要此行。函数现在返回void
}
//通过引用传递月份
anvsy.setMonth(月);
或者,使用返回值:

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

// Use return value
month = anvsy.setMonth();
//不需要任何参数
int Date::setMonth()
{
每月一次;
返回月份;
}
//使用返回值
month=anvsy.setMonth();
您提到的注释可能不正确-我认为您对什么是访问器和什么是变异器有点困惑,因为