13.3.3 应用程序界面(Application Interface)
//用DateMid_New类做界面 ^-^有点不够意思了,这个都还写出来有点错,自己还差点找不到
#include "datemid_new.h" #include <iostream> int main() { DateMid_New d(2005,9,9), e(42211); std::cout<<d; std::cout<<++d<<e; system("pause"); return 0; }
//DateMid_New作为应用程序界面 #ifndef HEADER_DATEMID_NEW #define HEADER_DATEMID_NEW #include <iostream> class Date_new; //先声明一下Date_new类 class DateMid_New { Date_new* m_p; //声明一个私有的Date_new类型的指针m_p public: DateMid_New(const std::string s); //构造函数,参数为string //DateMid(const std::string s); DateMid_New(int n); //重载构造函数,参数为int DateMid_New(int y, int m, int d); //重载构造函数,参数为年月日 DateMid_New(const Date_new& d); //重载构造函数,参数为一个Date_new类型的引用 ~DateMid_New(); //析构函数 DateMid_New operator+(int n)const; //重载运算符 DateMid_New& operator+=(int n); //重载运符符加等,返回的类型为DateMid_New的一个地址 DateMid_New& operator++(); //重载自加 int operator-(DateMid_New& d)const; //重载减操作,返回一个整形 friend std::ostream& operator<<(std::ostream& o, const DateMid_New& d);/**/ }; #endif;
#include "date_new.h" #include "datemid_new.h" #include <iostream> //直接去操作Date_new的构造函数 DateMid_New::DateMid_New(const std::string s):m_p(new Date_new(s)){} DateMid_New::DateMid_New(int n):m_p(new Date_new(n)){}; DateMid_New::DateMid_New(int y, int m, int d):m_p(new Date_new(y,m,d)){}; DateMid_New::DateMid_New(const Date_new& d):m_p(new Date_new(d)){}; DateMid_New::~DateMid_New(){ delete m_p; } DateMid_New DateMid_New::operator+(int n)const{ return *m_p + n; } DateMid_New& DateMid_New::operator+=(int n){ *m_p + n; return *this; } DateMid_New& DateMid_New::operator++(){ *m_p +=1; return *this; } int DateMid_New::operator-(DateMid_New& d)const{ return *m_p - *(d.m_p); } std::ostream& operator<<(std::ostream& o, const DateMid_New & d){ return o<<*(d.m_p); }