| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3930 人关注过本帖
标题:输出结果和书上的不一样,求解(新手不知道如何累积点数,20点是我的家底,还 ...
只看楼主 加入收藏
陈小昱
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-5-30
结帖率:0
收藏
已结贴  问题点数:20 回复次数:9 
输出结果和书上的不一样,求解(新手不知道如何累积点数,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编辑过]

搜索更多相关主题的帖子: 海涵 public include private 如何 
2016-05-30 16:36
a9580643
Rank: 2
来 自:江西九江
等 级:论坛游民
帖 子:60
专家分:59
注 册:2011-4-21
收藏
得分:5 
图片附件: 游客没有浏览图片的权限,请 登录注册

运行结果

花有重开日,人无在少年。
2016-05-30 17:52
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:5 
最后打印出那两条信息也没什么妨碍啊。为什么非得不要?想丢掉那两句也很好办。把~Time里面的cout语句改成一个分号就是了
2016-05-30 21:29
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:5 
有构造当然就得有析构,也不知道你为什么说“输出结果应为……”?
2016-05-31 08:13
陈小昱
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-5-30
收藏
得分:0 
回复 2楼 a9580643
所以您的输出结果是正确的!!!!意思是输出结果不同只是因为编译器不同吗?
2016-05-31 09:08
陈小昱
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-5-30
收藏
得分:0 
回复 3楼 yangfrancis
请问您的意思是改成这样么? 可是我这样改了之后无法执行...
Time::~Time()
{call destructor!;}
2016-05-31 09:12
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:0 
Time::~Time()
{;}
这样
2016-05-31 09:13
陈小昱
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-5-30
收藏
得分:0 
回复 4楼 rjsp

"结果应为"是书上范例写的输出结果,
不好意思~我自学c++中"类别与物件"这个单元,还不太清楚,请问一下
析构是什么?
2016-05-31 09:18
陈小昱
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2016-5-30
收藏
得分:0 
回复 7楼 yangfrancis
成功了!!只是为什么我在int main()大括号中并没有呼叫~Time()却仍会列印出来啊?
2016-05-31 09:52
仰望星空的
Rank: 5Rank: 5
等 级:贵宾
威 望:16
帖 子:50
专家分:248
注 册:2015-9-28
收藏
得分:5 
析构函数是自动调用的,生成对象后,在对象消亡时候,就会调用到析构函数~
2016-06-01 19:19
快速回复:输出结果和书上的不一样,求解(新手不知道如何累积点数,20点是我的家 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018911 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved