输出结果和书上的不一样,求解(新手不知道如何累积点数,20点是我的家底,还望各位海涵)
目前正在自学c++,照著书中范例打完如下#include<iostream>
using namespace std;
class Time
{
public:
Time();
Time(int,int,int);
void settime(int,int,int);
void sethour(int);
void setminute(int);
void setsecond(int);
void printtime();
~Time();
private:
int hour;
int minute;
int second;
};
Time::Time()
{
cout<<"call constructor."<<endl;
hour=minute=second=0;
}
Time::Time(int h,int m,int s)
{
cout<<"call constructor with reference."<<endl;
hour=(h>0&&h<24)?h:0;
minute=(m>0&&m<60)?m:0;
second=(s>0&&s<60)?s:0;
}
void Time::settime(int h,int m,int s)
{
hour=(h>0&&h<24)?h:0;
minute=(m>0&&m<60)?m:0;
second=(s>0&&s<60)?s:0;
}
void Time::sethour(int h)
{hour=(h>0&&h<24)?h:0;}
void Time::setminute(int m)
{minute=(m>0&&m<60)?m:0;}
void Time::setsecond(int s)
{second=(s>0&&s<60)?s:0;}
void Time::printtime()
{cout<<(hour<10?"0":"")<<hour<<":"<<(minute<10?"0":"")<<minute<<
":"<<(second<10?"0":"")<<second<<endl;}
Time::~Time()
{cout<<"call destructor!"<<endl;}
int main()
{
Time t1,t2(13,20,25);
cout<<"t1 is:";
t1.printtime();
cout<<"t2 is:";
t2.printtime();
t1.settime(13,30,0);
cout<<"reset t1 is:";
t1.printtime();
t2.sethour(8);
cout<<"reset t2 is:";
t2.printtime();
return 0;
}
输出结果应为:
call constructor.
call constructor with reference.
t1 is:00:00:00
t2 is:13:20:25
reset t1 is:13:30:00
reset t2 is:08:20:25
可是我自己执行的结果是:
call constructor.
call constructor with reference.
t1 is:00:00:00
t2 is:13:20:25
reset t1 is:13:30:00
reset t2 is:08:20:25
call destructor!
call destructir!
本以为把
Time::~Time()
{cout<<"call destructor!"<<endl;}
删除之后即可
结果删除之后根本无法执行
这又是为什么呢?
[此贴子已经被作者于2016-5-30 16:37编辑过]