24小时热门版块排行榜    

查看: 1947  |  回复: 7
本帖产生 1 个 程序强帖 ,点击这里进行查看

wuyan070987

金虫 (小有名气)

俺不是文化银

[求助] c++编写一个关于时间的程序,谢谢

试编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类DateTime,它是前面两个类为基类的派生类。
回复此楼

» 收录本帖的淘帖专辑推荐

科研相关资料

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

做简单事,立简单人
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军

【答案】应助回帖

★ ★ ★ ★ ★
jjdg(金币+2): 辛苦了 2011-06-25 11:22:21
余泽成(金币+3, 程序强帖+1): 谢谢参与应助! 2011-06-26 00:19:14
wuyan070987(金币+5): 2011-07-07 20:13:36
随便写下
CODE:
#include
using namespace std;

class Date
{
public:
    int year, month, day;

    Date()
    { this->year = this->month = this->day = 0; }

    Date(int y, int m, int d)
    {
        this->year = y;
        this->month = m;
        this->day = d;
    }

    void Show()
    {
        cout << "Date: " << this->day << "/" << this->month << "/" << this->year << endl;
    }
};

class Time
{
public:
    int hour,minute,second;

    Time()
    { this->hour = this->minute = this->second = 0; }

    Time(int h, int m, int s)
    {
        this->hour = h;
        this->minute = m;
        this->second = s;
    }

    void Show()
    {
        cout << "Time: " << this->hour << ":" << this->minute << ":" << this->second << endl;
    }
};

class DateTime : public Date, public Time
{
public:
    DateTime()
    { ; }

    DateTime(int y, int m, int d, int h, int min, int s)
    {
        this->year = y; this->month = m; this->day = d;
        this->hour = h; this->minute = min; this->second = s;
    }

    ~DateTime() {;}

    void Show()
    {
        cout << "DateTime: "
                << this->day << "/" << this->month << "/" << this->year << " "
                << this->hour << ":" << this->minute << ":" << this->second << endl;
    }
};

int main(int args, char* argv[])
{
    Date dt = Date(2011,6,25);
    Time tm = Time(10,10,10);
    DateTime dtm = DateTime(2011,6,25,10,10,10);

    dt.Show();
    tm.Show();
    dtm.Show();

    return 0;
}

结果
CODE:
Date: 25/6/2011
Time: 10:10:10
DateTime: 25/6/2011 10:10:10

Process returned 0 (0x0)   execution time : 0.328 s
Press any key to continue.

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
2楼2011-06-25 06:27:05
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

huycwork

金虫 (著名写手)

【答案】应助回帖


jjdg(金币+1): 感谢参与 2011-06-25 11:22:29
boost不是有现成的么?
漩涡的中心有一块空地,空空的。
3楼2011-06-25 09:00:28
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

夜色如水

新虫 (初入文坛)


微尘、梦想(金币+1): 鼓励交流,欢迎常来 2011-10-26 19:31:26
哥们为啥不调用time函数呢
然后再传递,这样每次都不一样 哈哈
4楼2011-10-26 17:42:20
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

wuyan070987

金虫 (小有名气)

俺不是文化银

做简单事,立简单人
5楼2011-10-27 13:07:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

gkf高

木虫 (著名写手)

世界和平

长知识了、呵呵
天空不留下鸟的痕迹,但是我已飞过。
6楼2011-10-27 16:48:53
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

tingzhensun

木虫 (正式写手)

引用回帖:
: Originally posted by libralibra at 2011-06-25 06:27:05:
随便写下
[code] #include<iostream>
using namespace std;

class Date
{
public:
    int year, month, day;

    Date()
    { this->year = this->month = this->day = 0; }

    ...

作为初学者,有问题要问,就是,您为何频繁使用this指针,有什么好处

[ 发自手机版 http://muchong.com/3g ]
7楼2011-11-06 23:16:25
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

libralibra

至尊木虫 (著名写手)

骠骑将军


jjdg(金币+1): 感谢参与 2011-11-07 03:09:55
引用回帖:
7楼: Originally posted by tingzhensun at 2011-11-06 23:16:25:
作为初学者,有问题要问,就是,您为何频繁使用this指针,有什么好处
[ 发自手机版 http://muchong.com/3g ]

我也是c++新手,只是知道this可以明确指明是类自身,有时候有同名变量的时候有用吧.
了解的朋友继续补充
matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
8楼2011-11-07 02:18:54
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 wuyan070987 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 085601材料工程专硕求调剂 +3 慕寒mio 2026-03-16 3/150 2026-03-17 13:46 by houyaoxu
[考研] 08工科 320总分 求调剂 +4 梨花珞晚风 2026-03-17 4/200 2026-03-17 13:38 by houyaoxu
[考研] 材料工程专硕274一志愿211求调剂 +6 薛云鹏 2026-03-15 6/300 2026-03-17 11:05 by 学员h26Tkc
[考研] 332求调剂 +5 Zz版 2026-03-13 5/250 2026-03-17 11:01 by JineShine
[考研] 271求调剂 +12 生如夏花… 2026-03-11 14/700 2026-03-17 10:56 by lovewei0727
[考研] 302求调剂 +4 小贾同学123 2026-03-15 8/400 2026-03-17 10:33 by 小贾同学123
[考研] 考研调剂 +3 淇ya_~ 2026-03-17 5/250 2026-03-17 09:25 by Winj1e
[考研] 328求调剂,英语六级551,有科研经历 +3 生物工程调剂 2026-03-16 4/200 2026-03-16 20:13 by Wangjingyue
[考研] 一志愿211 0703方向310分求调剂 +3 努力奋斗112 2026-03-15 3/150 2026-03-16 16:44 by houyaoxu
[考研] 中科院材料273求调剂 +4 yzydy 2026-03-15 4/200 2026-03-16 15:59 by Gaodh_82
[考研] 085600材料与化工 求调剂 +13 enenenhui 2026-03-13 14/700 2026-03-16 15:19 by 了了了了。。
[考研] 326求调剂 +3 mlpqaz03 2026-03-15 3/150 2026-03-16 07:33 by Iveryant
[基金申请] NSFC申报书里申请人简历中代表性论著还需要在申报书最后的附件里面再上传一遍吗 20+5 NSFC2026我来了 2026-03-10 14/700 2026-03-15 23:53 by 不负韶华的虎
[考博] 东华理工大学化材专业26届硕士博士申请 +6 zlingli 2026-03-13 6/300 2026-03-15 20:00 by ryzcf
[考研] 297一志愿上交085600求调剂 +5 指尖八千里 2026-03-14 5/250 2026-03-14 17:26 by a不易
[考研] 材料371求调剂 +9 鳄鱼? 2026-03-11 11/550 2026-03-13 22:53 by JourneyLucky
[考研] 332求调剂 +3 zjy101327 2026-03-11 6/300 2026-03-13 22:48 by JourneyLucky
[考研] 一志愿中科院,化学方向,295求调剂 +4 一氧二氮 2026-03-11 4/200 2026-03-13 22:35 by JourneyLucky
[考研] 333求调剂 +3 球球古力 2026-03-11 3/150 2026-03-13 21:27 by JourneyLucky
[考研] 26调剂/材料科学与工程/总分295/求收留 +9 2026调剂侠 2026-03-12 9/450 2026-03-13 20:46 by 18595523086
信息提示
请填处理意见