修改代码,使其正确
#include<iostream>using namespace std;
class Date {
public:
Date() {};//构造函数不能指定返回类型
int date(int year, int month, int day);
~Date() {};//析构函数不能指定返回类型
int& GetYear() { return year; }
int& GetMonth() { return month; }
int& GetDay() { return day; }
private:
int year = 2000;
int month = 12;
int day = 31;
static bool IsLeapyear;//是否闰年
};
bool Date::IsLeapyear = true;
int Date::date(int year, int month, int day)//函数名不能和类名一样,所以要讲函数名改写,此处是将Date改为了date
{
(*this).year = year;
(*this).month = month;
(*this).day = day;
}
int main()
{
int year, month, day;
cin >> year >> month >> day;
Date mydate(int myyear, int mymonth, int myday);
int& myyear = mydate.GetYear();
int& mymonth = mydate.GetMonth();
int& myday = mydate.GetDay();
cout << myyear << endl << mymonth << endl << myday << endl;
myyear = 8888;
cout << mydate.GetYear();
} #include <iostream>
using namespace std;
class Date {
public:
Date() {}
void setDate(int year, int month, int day);
~Date() {}
int& getYear() { return year; }
int& getMonth() { return month; }
int& getDay() { return day; }
private:
int year = 2000;
int month = 12;
int day = 31;
static bool isLeapyear;
};
bool Date::isLeapyear = true;
void Date::setDate(int year, int month, int day)
{
this->year = year;
this->month = month;
this->day = day;
}
int main()
{
int myyear, mymonth, myday;
cin >> myyear >> mymonth >> myday;
Date mydate;
mydate.setDate(myyear, mymonth, myday);
int& year = mydate.getYear();
int& month = mydate.getMonth();
int& day = mydate.getDay();
cout << year << "/" << month << "/" << day << endl;
return 0;
}
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 这是一个包含一些错误的日期类,如函数日期和类名同名,函数没有返回值等。这是修复后的代码。
#include<iostream>
using namespace std;
class Date {
public:
Date(int y=2000, int m=1, int d=1) :year(y), month(m), day(d){}
~Date() {}
void set_date(int y, int m, int d)
{
year = y;
month = m;
day = d;
}
int& GetYear() { return year; }
int& GetMonth() { return month; }
int& GetDay() { return day; }
private:
int year;
int month;
int day;
};
int main()
{
int year, month, day;
cin >> year >> month >> day;
Date mydate(year, month, day);
cout << mydate.GetYear() << endl
<< mydate.GetMonth() << endl
<< mydate.GetDay() << endl;
mydate.GetYear() = 8888;
cout << mydate.GetYear();
return 0;
}
此代码读取用户输入的年、月、日并创建“Date”对象。然后通过“get”函数打印年、月、日,然后更改年份并打印新的年份。
求最佳答案{:10_254:}{:10_254:}{:10_254:}
页:
[1]