| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1888 人关注过本帖
标题:如何运用重载运算符解决这个问题
只看楼主 加入收藏
正气
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-5-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:3 
如何运用重载运算符解决这个问题
要求增加成员函数实现:
⑴ 能比较两个日期的大小(重载“= =”、“>”、“<”、“>=”、“<=”、“!=”);
⑵ 编写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();
}
搜索更多相关主题的帖子: 如何 cool public include private 
2017-06-20 20:25
GBH1
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:5
帖 子:112
专家分:510
注 册:2017-6-13
收藏
得分:0 
比较日期时间最简单的方法是将日期转化成毫秒直接进行比较。不知道你要怎么比较,如果只是简单的比较的话,感觉没有必要重载,因为日期直接转化成毫秒后就是数字之间的比较了,这样就没有必要重载。当然要是为了重载而重载的话,就直接在类定义中进行重载,具体参考C++运算符重载啊,老铁,稳住。。。。。
2017-06-20 20:55
正气
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2017-5-30
收藏
得分:0 
回复 2楼 GBH1
没学啊老铁帮忙写一段我自己模仿模仿
2017-06-20 21:10
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:20 

程序代码:
#include <iostream>

class datetime
{
public:
    datetime( unsigned year, unsigned month, unsigned day, unsigned hour=0, unsigned min=0, unsigned sec=0 )
        : year_(year), month_(month), day_(day), hour_(hour), min_(min), sec_(sec)
    {
    }

    bool operator< ( const datetime& dt ) const
    {
        if(year_<dt.year_) return true;
        if(year_>dt.year_) return false;

        if(month_<dt.month_) return true;
        if(month_>dt.month_) return false;

        if(day_<dt.day_) return true;
        if(day_>dt.day_) return false;

        if(hour_<dt.hour_) return true;
        if(hour_>dt.hour_) return false;

        if(min_<dt.min_) return true;
        if(min_>dt.min_) return false;

        return sec_<dt.sec_;
    }

    bool operator> ( const datetime& dt ) const
    {
        return dt<*this;
    }
    bool operator<= ( const datetime& dt ) const
    {
        return !(dt<*this);
    }
    bool operator>= ( const datetime& dt ) const
    {
        return !(*this<dt);
    }
    bool operator!= ( const datetime& dt ) const
    {
        return *this<dt || dt<*this;
    }
    bool operator== ( const datetime& dt ) const
    {
        return !(*this<dt || dt<*this);
    }

private:
    unsigned year_, month_, day_, hour_, min_, sec_;

    friend std::ostream& operator<< ( std::ostream& os, const datetime& dt );
};

std::ostream& operator<< ( std::ostream& os, const datetime& dt )
{
    return os << dt.year_ << '-' << dt.month_ << '-' << dt.day_ << ' ' << dt.hour_ << ':' << dt.min_ << ':' << dt.sec_;
}

using namespace std;

void test( const datetime& a, const datetime& b )
{
    ios_base::fmtflags flags_saved = cout.flags();
    cout.setf( ios_base::boolalpha );

    cout << a << "  < " << b << " ? " << (a <b) << '\n';
    cout << a << "  > " << b << " ? " << (a >b) << '\n';
    cout << a << " <= " << b << " ? " << (a<=b) << '\n';
    cout << a << " >= " << b << " ? " << (a>=b) << '\n';
    cout << a << " != " << b << " ? " << (a!=b) << '\n';
    cout << a << " == " << b << " ? " << (a==b) << '\n';

    cout.setf( flags_saved, ios_base::boolalpha );
}

int main( void )
{
    test( datetime(1998,8,17,9,51,30), datetime(1998,8,17,9,51,31) );
    cout << '\n';
    test( datetime(1998,8,17,9,51,30), datetime(1998,8,17,9,51,30) );
}

2017-06-21 09:09
快速回复:如何运用重载运算符解决这个问题
数据加载中...
 
   



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

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022291 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved