//Date.h #ifndef DATE_H #define DATE_H class Date { public: Date(int = 1,int = 1,int = 1900);//参数分别为月日年 ~Date(); void print() const ; private: int month; int day; int year; int checkDay(int) const ;//检验每月的天数是否符合逻辑 }; #endif
//Date.cpp #include "Date.h" #include <iostream> using namespace std; Date::Date(int mh,int dy,int yr) { if(mh >0 && mh <= 13) month = mh; else{ month = 1; cout << "Invalid month (" << mh << ") set to 1.\n"; } year = yr; day = checkDay(dy); cout << "Date object constructor for date "; print(); } Date::~Date() { cout << "Date object destructor for date "; print(); } void Date::print() const { cout << month << '/' << day << '/' << year << endl; } int Date::checkDay(int testDay) const { //平年中的每月天数1-12月分别对应daysPerMonth[1]-[12] static const int daysPerMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; //一般情况 if(testDay>0 && testDay <= daysPerMonth[month]) return testDay; //闰年情况 if(month==2 && testDay==29 &&(year%400==0 || (year%4==0 && year%100!=0))) return testDay; //其他情况 cout << "Invalid day (" << testDay << ") set to 1.\n"; return 1; }
//Employee.h #ifndef EMPLOYEE_H_ #define EMPLOYEE_H_ #include "Date.h" class Employee { public: //构造函数参数分别为名,姓,出生日期,开始工作的日期 Employee(const char * const,const char * const,const Date &,const Date &); ~Employee(); void print() const; private: char firstName[25]; char lastName[25]; const Date birthDate; const Date hireDate; }; #endif
//Employee.cpp #include "Employee.h" #include "Date.h" #include <iostream> #include <cstring>/*using std::strlen using std::strncpy*/ using namespace std; Employee::Employee(const char * const first,const char * const last, const Date &dateOfBirth,const Date &dateOfHire) : birthDate(dateOfBirth), hireDate(dateOfHire) //成员初始化器 { int length = strlen(first); length = (length < 25 ? length : 24); strncpy(firstName,first,length); firstName[length] = '\0'; length = strlen(last); length = (length < 25 ? length : 24); strncpy(lastName,last,length); lastName[length] = '\0'; cout << "Employee object constructor: " << firstName << " " << lastName << endl; } Employee::~Employee() { cout << "Employee object destructor:" << firstName << " " << lastName << endl; } void Employee::print() const { cout << firstName << "," << lastName << endl; cout << " Hired:"; hireDate.print(); cout << "BirthDay:"; birthDate.print(); }
//main.cpp #include "Employee.h" #include <iostream> using namespace std; int main() { {//这个大括号仅仅为了测试析构函数是否正常工作,可以去掉,, Date birth(7,24,1959); Date hire(3,12,1988); Employee manager("Bob","Lanester",birth,hire); cout << endl; manager.print(); cout << "\nTest Date constructor with invalid values:\n"; Date lastDayOff(14,35,1976); } system("pause >> cout"); return 0; }
相关推荐
C++大学基础教程_12767412.pdf
C++基础教程_完整版 - 副本.pdf
C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版 C++基础教程_完整版
c++基础教程 c++内容ppt教程并附课程模板
C++课程-2_类与对象 C++课程-2_类与对象
C++_MFC_基础教程_(初学者) ppt
东南大学电类C++08_09_3机试试卷
C++大学基础教程(第五版)(C++ How to Program中文版).pdf
VS2010_C++开发指导教程,包含IDE介绍,创建windows应用等
C++模板教程C++_Templates_TheCompleteGuide,C++模板教程C++_Templates_TheCompleteGuide,C++模板教程C++_Templates_TheCompleteGuide
C++教程_谭浩强版 清华大学 C++教程_谭浩强 C++教程_谭浩强
C++ 大学自学教程(第7版) 目录 第一部分 C++语言 1,C++程序设计概述 2,编写简单的C++程序 3,函数 4,程序流程控制 5,C++数据类型结构 6,结构和联合 7,C++数据进阶 8,指针、地址和动态存储器 9,引用变量...
18,类成员 19,友元 20,类和堆 21,重载运算符 22,类库 23,类的继承 24,多重继承 25,类模板 第三部分 使用类 26,标准C库函数 27,标准C++库 28,格式化IOStream和stringstream类 29,文件I/O流 第四部分 标准...
C++ 大学自学教程(第7版) 目录 第一部分 C++语言 1,C++程序设计概述 2,编写简单的C++程序 3,函数 4,程序流程控制 5,C++数据类型结构 6,结构和联合 7,C++数据进阶 8,指针、地址和动态存储器 9,引用变量 ...
东南大学电类C++08_09_3机试参考答案
_02:C++基础语法 _03:运算符与表达式 _04:C++容器 _05:指针与引用 _06:基础句法 _07:高级语法 _08:编程思想 _09:进阶编程 _10:桌面项目的设计与架构 _11:桌面播放器通用模块开发 _12:编写UI界面 _13:编写音频功能 _...
C++入门基础_30分钟入门教程 C++入门基础_30分钟入门教程
本书号称『高速上手』,从内容上对二十一世纪二十年代之前产生C++的相关特性做了非常相对全面的介绍,读者可以自行根据下面的目录选取感兴趣的内容进行学习,快速熟悉需要了解的内容。这些特性并不需要全部掌握,只...
C++_MFC基础教程(初学者)_