| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 517 人关注过本帖
标题:关于<<重载问题
只看楼主 加入收藏
a99875984
Rank: 2
等 级:论坛游民
帖 子:188
专家分:24
注 册:2012-2-11
结帖率:94.64%
收藏
已结贴  问题点数:20 回复次数:4 
关于<<重载问题
程序代码:
#include <cmath>
#include <iostream>
using namespace std;

class Date
{
private:
    int year;
    int month;
    int day;
public:
    Date(int a=0,int b=0,int c=0);//
    ~Date(){}
    //int monthdays(int a,int b);
    //Date operator +(const Date &t)const;
    Date operator +(const int a)const;
    int operator -(const Date &t)const;
    Date operator -(const int a)const;
    void operator<<(ostream & os);
};

Date::Date(int a,int b,int c)
{
    year=a;
    month=b;
    day=c;
}

int monthdays(int a,int b)
{
    if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
        return 31;
    else if(b==2)
    {
        if(a%4==0&&a%100!=0||a%400==0)
            return 29;
        else
            return 28;
    }
    else return 30;
}

//似乎用不到日期加日期
/*Date Date::operator+(const Date &t)const
{
    Date sum;
    Date count(t.year,t.month);
    for(int i=0;i<(year-t.year)*12+month-t.month;i++)
    {
        sum.day=(day+t.day)%monthdays(count.year,count.month);
        sum.month=(month+t.month+(day+t.day)/monthdays(count.year,count.month))%12;
        sum.year=(year+t.year+(month+t.month+(day+t.day)/monthdays(count.year,count.month)))/12;
        count.year++;
        count.month++;
    }
    return sum;
}*/

Date Date::operator+(const int a)const
{
    Date sum;
    Date count(year,month);
    int b=a,i=0;
    while (b>monthdays(count.year,count.month))
    {
        i++;
        b-=monthdays(count.year,count.month);
        count.month;
        if(count.month>12)
        {
            count.month%=12;
            count.year++;
        }
    }
    sum.day=day+b;
    sum.month=month+i+sum.day/monthdays(count.year,count.month);
    sum.year=year+month/12;
    sum.month%=12;
    sum.day%=monthdays(count.year,count.month);
    return sum;
}

int Date::operator-(const Date &t)const
{
    Date count(t.year,t.month);
    int sum=0;
    while (year!=count.year||month!=count.month)
    {
        sum+=monthdays(count.year,count.month);
        count.month++;
        if(count.month>12)
        {
            count.month%=12;
            count.year++;
        }
    }
    sum+=day-t.day;
    return sum;
}

Date Date::operator-(const int a)const
{
    Date count(year,month);
    Date sum;
    int b=a;
    while(b>monthdays(count.year,count.month))
    {

    }
    return sum;
}

void Date::operator<<(ostream & os)
{
    os<<year<<'-'<<month<<'-'<<day<<endl;
}

//#include <iostream>
//using namespace std;
//#include "Date.h"
int main()
{
    Date t1(2012,1,1);
    int a=366;
    cout<<t1+a<<endl;
    system("pause");
    return 0;
}
如果想要想在主函数里这样输出成员对象,该怎么重载<<啊?谢谢了。
搜索更多相关主题的帖子: color 
2013-04-17 14:33
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:20 
将<<重载为友元函数!

o(∩∩)Linux & Python 群:187367181
2013-04-17 15:44
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
程序代码:
friend ostream operator<<(ostream & os, Date & date);
...
ostream operator << (ostream & os, Date & date)
{
    return os << date.year <<'-'<< date.month <<'-'<< date.day << endl;
}

o(∩∩)Linux & Python 群:187367181
2013-04-17 15:50
a99875984
Rank: 2
等 级:论坛游民
帖 子:188
专家分:24
注 册:2012-2-11
收藏
得分:0 
回复 2楼 thlgood
谢谢了哈
2013-04-17 15:51
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
如果你实在想让 << 重载为成员函数,那么你可以这样:
程序代码:
ostream operator<<(ostream & os);
{
    return os << "something";
}

//在调用的时候要把ostream放到 << 的右边;
Date *ptr = new Date(...);
ptr << cout;

要注意一点的是ostream不能用const修饰。因为 << 操作符会改动ostream

我是C++新手,关于以上的答案,我没有验证过。
收到的鲜花
  • Susake2013-04-17 15:55 送鲜花  1朵   附言:....

o(∩∩)Linux & Python 群:187367181
2013-04-17 15:54
快速回复:关于<<重载问题
数据加载中...
 
   



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

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