返回类型不同可以覆盖,这里怎么不行?请高手指教~~
//==============//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);}