时间设置程序,关于所用到的句柄类问题
我在下面时间设置程序中用到句柄类,但最后在main函数中创建对象时,当在一个块中创建,即一起创建多个对象,输出的总是最后一个对象的数据,前面的被覆盖了!各位帮忙看一下,谢谢了#include <ctime>
#include <iostream>
using namespace std;
class Date {
private:
struct tm* timer;
public:
Date();
Date(int DDD,int YYYY);
Date(int MM,int DD,int YY);
Date(char* MM,int DD,int YY);
~Date();
print1();
print2();
print3();
};
Date :: ~Date() {}
Date :: Date() {
time_t ti = time(0);
timer = localtime(&ti);
}
Date :: Date(int DDD,int YYYY) {
time_t ti = time(0);
timer = localtime(&ti);
timer->tm_mon = 0;
timer->tm_year = YYYY - 1900;
timer->tm_yday = DDD - 1;
for (int i=1;i <= 12;i++) {
if(i == 2) {
if ((YYYY % 4 == 0 && YYYY % 100 != 0) || (YYYY % 400 == 0)){
if(DDD <= 29) {
timer->tm_mday = DDD;
break;
}else{
DDD -= 29;
timer->tm_mon++;
}
}else{
if(DDD <= 28) {
timer->tm_mday = DDD;
break;
}else{
DDD -= 28;
timer->tm_mon++;
}
}
}else if(i==4 || i==6 || i==9 || i==11) {
if(DDD <= 30) {
timer->tm_mday = DDD;
break;
}else {
DDD -= 30;
timer->tm_mon++;
}
}else {
if(DDD <= 31) {
timer->tm_mday = DDD;
break;
}else {
DDD -= 31;
timer->tm_mon++;
}
}
}
}
Date :: Date(int MM,int DD,int YY) {
time_t ti = time(0);
timer = localtime(&ti);
timer->tm_mday = DD;
timer->tm_mon = MM - 1;
timer->tm_year = YY - 1900;
timer->tm_yday = -1;
}
Date :: Date(char* MM,int DD,int YY) {
time_t ti = time(0);
timer = localtime(&ti);
timer->tm_year = YY -1900;
timer->tm_mday = DD;
timer->tm_yday = -1;
char* month[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
for(int i=0;i <= 12;i++)
if(!strcmp(MM,month[i])) {
timer->tm_mon = i;
break;
}
}
Date :: print1() {
if(timer->tm_yday == -1) {
int a[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
for(int i=0;i < timer->tm_mon;i++) timer->tm_yday += a[i];
if((i > 1) && ((timer->tm_year % 4 == 0 && timer->tm_year % 100 != 0) || (timer->tm_year % 400 == 0)))
timer->tm_yday += (timer->tm_mday + 1);
else timer->tm_yday += timer->tm_mday;
}
char str[20];
strftime(str,20,"%j %Y\n",timer);
cout << str;
}
Date :: print2() {
char str[20];
strftime(str,20,"%m\/%d\/%Y\n",timer);
cout << str;
}
Date :: print3() {
char str[20];
strftime(str,20,"%B %d, %Y\n",timer);
cout << str;
}
int main(void) {
cout << "The system time :" << endl;
Date date1;
date1.print1();
date1.print2();
date1.print3();
cout << endl;
Date date2(108,2008);
date2.print1();
date2.print2();
date2.print3();
cout << endl;
Date date3(4,17,2008);
date3.print1();
date3.print2();
date3.print3();
cout << endl;
Date date4("April",17,2008);
date4.print1();
date4.print2();
date4.print3();
cout << endl;
Date date5(61,1640);
date5.print1();
date5.print2();
date5.print3();
cout << endl;
Date date6(3,1,1640);
date6.print1();
date6.print2();
date6.print3();
cout << endl;
Date date7("March",1,1640);
Date date8(89,1341);
Date date9(3,23,1648);
Date date10("March",1,2341);
date7.print1();
date7.print2();
date7.print3();
cout << endl;
//Date date8(60,1641);
date8.print1();
date8.print2();
date8.print3();
cout << endl;
//Date date9(3,1,1641);
date9.print1();
date9.print2();
date9.print3();
cout << endl;
//Date date10("March",1,1641);
date10.print1();
date10.print2();
date10.print3();
cout << endl;
return 0;
}
最后