| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
共有 2070 人关注过本帖
标题:如何运用重载运算符解决这个问题
取消只看楼主 加入收藏
正气
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-5-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:1 
如何运用重载运算符解决这个问题
要求增加成员函数实现:
⑴ 能比较两个日期的大小(重载“= =”、“>”、“<”、“>=”、“<=”、“!=”);
⑵ 编写main( )函数测试,内容自定。
以下是我的原程序,该如何添加
#include <iostream.h>
#include <string.h>
class time
{
private:
    int hour,min,sec;
public:
    time(int h,int m,int s)
    {hour=h;min=m,sec=s;}
    void cool ()
    {cout<<hour<<":"<<min<<":"<<sec;
    if (hour>12)
        cout<<"pm"<<endl;
    else
        cout<<"am"<<endl;
    }
   
};
#include < iostream.h >
#include < string.h >
class date
{private:
int year,month,day;
public:
    date(int y,int M,int d)
    {year=y;month=M;day=d;}
    void cool ()
    {cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;
   
    if((year%4==0)&&(year%100!=0)||(year%400==0))
        cout<<"是闰年"<<endl;   
    else
        cout<<"不是闰年"<<endl;
    }
};

class datetime:public date,public time
{public:
datetime(int y,int M,int d,int h,int m,int s):date( y, M, d),time( h, m, s)
{}
void cool ()
{
    date:: cool ();
    time:: cool ();
}
};

void main()
{
   
    datetime A(1998,8,17,9,51,30);
    A.cool();
}
2017-06-20 20:25
正气
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-5-30
收藏
得分:0 
回复 2楼 GBH1
没学啊老铁帮忙写一段我自己模仿模仿
2017-06-20 21:10
快速回复:如何运用重载运算符解决这个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064762 second(s), 9 queries.
Copyright©2004-2025, BC-CN.NET, All Rights Reserved