| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 394 人关注过本帖
标题:返回类型不同可以覆盖,这里怎么不行?请高手指教~~
只看楼主 加入收藏
韩雨航
Rank: 1
等 级:新手上路
帖 子:21
专家分:0
注 册:2012-1-12
结帖率:57.14%
收藏
 问题点数:0 回复次数:0 
返回类型不同可以覆盖,这里怎么不行?请高手指教~~
//==============
//idate.h
//===============
class Idate
{
protected:
    virtual int ymd2i()const=0;
public:
    virtual ~Idate(){}
    virtual Idate& operator+(int n)=0;
    virtual Idate& operator+=(int n)=0;
    virtual Idate& operator++()=0;
    int operator-(const Idate& d){return ymd2i()-d.ymd2i();}            //无法纯虚
    virtual void print(std::ostream& o)const=0;                        //与后面不对应    error C2662: 'print' : cannot convert 'this' pointer from 'const class Idate' to 'class Idate &'
};//------------------------
Idate& createDate(const std::string s);
Idate& createDate(int y, int m,int d);
Idate& createDate(int n);
//--------------------------
inline std::ostream& operator<<(std::ostream o,const Idate& d)
{
    d.print(o);
    return o;
}
//================
//date.h
//================
#include<iostream>
#include"idate.h"
class Date:public Idate
{
    int year,month,day;
protected:
    int ymd2i()const;                        //返回天数
    void i2ymd(int n);                            //'初始化'
    static const int tians[];
    bool isLeapyear()const{return !((year%4)&&!(year%100)||(year%400));}
public:
    Date(const std::string s);
    Date(int n){i2ymd(n);}
    Date(int y,int m,int d):year(y),month(m),day(d){}
    ~Date(){}
    Date& operator+(int n){return *new Date(ymd2i()+n);}            //*new 不是new* ,引用是值~!!
    Date& operator+=(int n){i2ymd(ymd2i()+n);return *this;}
    Date& operator++(){return *this+=1;}
    void print(std::ostream o)const;
};
//===================
//date.cpp
//===================
#include<iostream>
#include"date.h"
#include<iomanip>
using namespace std;
//-----------------
const int Date::tians[]={0,31,59,90,120,151,181,212,243,273,304,334};
//-----------------
Date::Date(const std::string s)
{
    year=atoi(s.substr(0,4).c_str());
    month=atoi(s.substr(5,2).c_str());
    day=atoi(s.substr(8,2).c_str());
}
void Date::i2ymd(int absday)
{
    absday=absday<1||absday>3650000 ? 1:absday;
    int n=absday;
    for(year=1;n>isLeapyear()+365;n-=isLeapyear()+365,year++);                //n-=isLeapyear    for(year=1;n>isLeapyear()+365;n-=isLeapyear+365,year++);                //

    for(month=1;(month<12&&n>(month>2&&isLeapyear())+tians[month-1]);month++);
    day=n-tians[month-1]-(month>2&&isLeapyear());
}
int Date::ymd2i()const
{
    int absday=(year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400;
    absday+=tians[month-1]+day+(month>2&&isLeapyear());
}
void Date::print(std::ostream o)const
{
    o<<setfill('0')<<setw(4)<<year<<setw(2)<<month<<setw(2)<<day<<setfill(' ');
}
Idate& createDate(const string s)            //在date.cpp里实现,在idate.h里声明
{return *new Date(s);}
Idate& createDate(int y, int m,int d)
{return *new Date(y,m,d);}
Idate& createDate(int n)
{return *new Date(n);}
搜索更多相关主题的帖子: class void convert public return 
2012-03-18 17:18
快速回复:返回类型不同可以覆盖,这里怎么不行?请高手指教~~
数据加载中...
 
   



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

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