【发布时间】: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