64,663
社区成员
发帖
与我相关
我的任务
分享
//Date.h
#ifndef DATE
#define DATE
#include <iostream>
using namespace std;
#include <string>
class Date { //父类Date
public:
int year, month, day;
void check(); //检验所创建的日期对象是否合法函数
public:
Date (int y = 2000, int m = 1, int d = 1); //构造函数
Date (const string & s); //拷贝构造函数
bool isLeapYear () const; //判断是否为闰年函数
};
#endif
//Date.cpp
#include <iomanip>
#include "Date.h"
void Date::check () {
if (year>5000 || year<1 || month<1 || month>12 || day<1 || day>31)
exit(1);
}
Date::Date(const 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());
check();
}
Date::Date(int y, int m, int d) {
year = y, month = m, day = d;
check();
}
bool Date::isLeapYear () const {
return (year%4 == 0 && year%100 != 0)|| (year%400 == 0) ;
}
//WDate.h
//WDate类
#include "Date.h"
class WDate : public Date {
int weekday,y,c,m,d;
public:
WDate(int y, int m, int d); //构造函数
int GetWeekDay (); //获得星期几
friend ostream & operator << (ostream & o, const WDate & w); //输出流操作(输出包括星期几在内的信息)
};
//WDate.cpp
#ifndef WDATE
#define WDATE
#include "WDate.h"
#include <iomanip>
#include <string>
static const char* weekName[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday" };
WDate::WDate(int y, int m, int d):Date(y,m,d) {}; //构造函数
int WDate::GetWeekDay(){ //根据Zeller公式获得星期几
y = year%100;
c = year/100;
(month == 1 || month == 2) ? m = month + 12 : m = month;
d = day;
weekday = y + y/4 + c/4 -2*c + 26*(m+1)/10 + d -1;
return weekday%7;
}
ostream & operator << (ostream & o,const WDate & w) {
o << setfill ('0') << setw(4) << w.year << '-' << setw(2) << w.month << '-' << setw(2) << w.day << " " << weekName[w.GetWeekDay()] << setfill(' ');
return o;
}
#endif
//ex04.cpp
#include "WDate.h"
#include <iostream>
int main () {
WDate d(2005,8,21);
cout << "创建日期:" << d;
WDate e(2009,4,29);
cout << "创建日期:" << e;
}
//Date.h
#ifndef DATE
#define DATE
#include <iostream>
using namespace std;
#include <string>
class Date { //父类Date
int year, month, day;
void check(); //检验所创建的日期对象是否合法函数
public:
Date (int y = 2000, int m = 1, int d = 1); //构造函数
Date (const string & s); //拷贝构造函数
bool isLeapYear () const; //判断是否为闰年函数
};
#endif
//Date.cpp
#include <iomanip>
#include "Date.h"
void Date::check () {
if (year>5000 || year<1 || month<1 || month>12 || day<1 || day>31)
exit(1);
}
Date::Date(const 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());
check();
}
Date::Date(int y, int m, int d) {
year = y, month = m, day = d;
check();
}
bool Date::isLeapYear () const {
return (year%4 == 0 && year%100 != 0)|| (year%400 == 0) ;
}
//WDate.h
//WDate类
#include "Date.h"
class WDate : public Date {
public:
WDate(int y, int m, int d); //构造函数
int GetWeekDay () const ; //获得星期几
friend ostream & operator << (ostream & o, const WDate & w); //输出流操作(输出包括星期几在内的信息)
};
//WDate.cpp
#ifndef WDATE
#define WDATE
#include "WDate.h"
#include <iomanip>
#include <string>
static const char* weekName[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thusday", "Friday", "Saturday" };
WDate::WDate(int y, int m, int d):Date(y,m,d) {}; //构造函数
int WDate::GetWeekDay() const { //根据Zeller公式获得星期几
int weekday,y,c,m,d;
y = year%100;
c = year/100;
(month == 1 || month == 2) ? m = month + 12 : m = month;
d = day;
weekday = y + y/4 + c/4 -2*c + 26*(m+1)/10 + d -1;
return weekday%7;
}
ostream & operator << (ostream & o,const WDate & w) {
o << setfill ('0') << setw(4) << w.year << '-' << setw(2) << w.month << '-' <<
setw(2) << w.day << " " << weekName[w.GetWeekDay()] << endl << setfill(' ');
return o;
}
#endif
//ex04.cpp
//创建2005.8.21和2008.8.8两个日期,分别显示这两个日期
#include "WDate.h"
#include <iostream>
int main () {
WDate d(2005,8,21);
cout << "创建日期:" << d;
WDate e(2009,4,29);
cout << "创建日期:" << e;
system("pause");
}
class WDate : public Date {
int weekday,y,c,m,d;
public:
WDate(int y, int m, int d); //构造函数
int GetWeekDay () const ; //获得星期几
friend ostream & operator << (ostream & o, const WDate & w); //输出流操作(输出包括星期几在内的信息)
};
friend ostream & operator << (ostream & o, const WDate & w);
修改为:
friend ostream & operator << (ostream & o, WDate & w);
没有const引用~