65,208
社区成员
发帖
与我相关
我的任务
分享
#ifndef DATE_H
#define DATE_H
#include<iostream>
using namespace std;
template<class T>
class Date
{
public:
Date(T = 1,T= 1,T=1900);
void print() const;
~Date();
private:
T month;
T day;
T year;
T checkDate(T) const;
};
#endif
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include"Date.h"
template<class T>
class Employee
{
public:
Employee(const char* const, const char* const, const Date<T>& , const Date<T>& );
void print() const;
~Employee()
{
cout<<"Employee object destructor: "<<lastName<<", "<<firstName<<endl;
}
private:
char firstName[25];
char lastName[25];
const Date<T> birthDay;
const Date<T> hireDate;
};
#endif
#include"Date.h"
template<class T>
Date<T>::Date(T mn, T dy, T yr)
{
if(mn>0&&mn<=12)
month=mn;
else
{
month=1;
cout<<"InValid month("<<mn<<")set to 1"<<endl;
}
year=yr;
day=checkDate(dy);
cout<<"date object constructor for date"<<endl;
print();
cout<<endl;
}
template<class T>
void Date<T>::print() const
{
cout<<month<<'-'<<day<<'-'<<'year'<<endl;
}
template<class T>
Date<T>::~Date()
{
cout<<"Date 的析构函数正在运行......"<<endl;
print();
cout<<"Date 的析构函数运行结束..end.."<<endl;
}
template<class T>
T Date<T>::checkDate(T testday) const
{
static const T daypermonth[13]=
{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(testday>0&&testday<=daypermonth[month])
return testday;
if((2==month)&&(29==testday)&&((0==year%400)||(0==year%4)&&(0!=year%100)))
return testday;
cout<<"Invalid day ("<<testday<<")set to 1"<<endl;
return 1;
}
#include"Employee.h"
#include<string>
//using std::strlen;
//using std::strncpy;
#include<iostream>
using namespace std;
#include"Date.h"
template<class T>
Employee<T>::Employee(const char* const first, const char* const last, const Date<T>& dateOfBirth, const Date<T>& dateOfHire)
:birthDay(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);
firstName[length]='\0';
cout<<"Employee object constructor:"<<firstName<<' '<<lastname<<endl;
}
template<class T>
void Employee<T>::print() const
{
cout<<lastName<<", "<<firstName<<" Hired: ";
hireDate.print();
cout<<" Birthday: ";
birthDay.print();
cout<<endl<<endl;
}
/*template<class T>
Employee<T>::~Employee()
{
cout<<"Employee object destructor: "<<lastName<<", "<<firstName<<endl;
}*/
#include<iostream>
using namespace std;
#include"Employee.h"
int main()
{
Date<int>birth(7,24,1949);
Date<int>hire(3,12,1988);
Employee<int> manager("bob","blue",birth,hire);
cout<<endl<<endl;
manager.print();
cout<<"\nTest Date constructor with invalid value : "<<endl;
Date<int>lastDayOff(14,35,1994);
cout<<endl;
system("pause");
return 0;
}