芷子诺 2022-10-23 11:09 采纳率: 100%
浏览 18
已结题

请问这个怎么改错呢?尤其是访问私有那个部分

#include
using namespace std;
class Date
{
public:
Date(int y=0,int m=0,int d=0)
{
year=y;
month=m;
day=d;
}
void operator=(Date &date);
void output=(){cout<<year<<","<<month<<","<<day<<endl;}
bool operator>(Date d1,Date d2);
private:
int year,month,day;
};
void operator=(Date &date)
{
year=date.year;
month=date.month;
day=date.day;
}
bool operator>(Date d1,Date d2)
{
bool flag=false;
if(d1.year>d2.year)flag=true;
else if(d1.year==d2.year)
if(d1.month>d2.month)flag=true;
else if(d1.month==d2.month)
if(d1.day>d2.day)flag=true;
return flag;

}
void main()
{
Date date1(2017,4,27);
Date date2(2018,4,27),date3;
date3=date1;
cout<<"date3;";
date3.output();
cout<<"date2>date3 is";
cout<<boolalpha<<(date2>date3);

  • 写回答

1条回答 默认 最新

  • 浪客 2022-10-23 11:31
    关注
    
    class Date
    {
    public:
        Date(int y = 0, int m = 0, int d = 0)
        {
            year = y;
            month = m;
            day = d;
        }
        Date &operator=(const Date &date); //
        void output() { cout << year << "," << month << "," << day << endl; }
        bool operator>(const Date &d2); //
    
    private:
        int year, month, day;
    };
    Date &Date::operator=(const Date &date) //
    {
        year = date.year;
        month = date.month;
        day = date.day;
        return *this;
    }
    bool Date::operator>(const Date &d2) //
    {
        if (year != d2.year)
            return year > d2.year;
        if (month != d2.month)
            return month > d2.month;
        return day > d2.day;
    }
    int main()
    {
        Date date1(2017, 4, 27);
        Date date2(2018, 4, 27), date3;
        date3 = date1;
        cout << "date3;";
        date3.output();
        cout << "date2>date3 is";
        cout << boolalpha << (date2 > date3);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 10月31日
  • 已采纳回答 10月23日
  • 创建了问题 10月23日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度