24小时热门版块排行榜    

查看: 1955  |  回复: 7
本帖产生 1 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

wuyan070987

金虫 (小有名气)

俺不是文化银

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

试编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类DateTime,它是前面两个类为基类的派生类。
回复此楼
做简单事,立简单人
已阅   回复此楼   关注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的回帖
查看全部 8 个回答

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的回帖

夜色如水

新虫 (初入文坛)


微尘、梦想(金币+1): 鼓励交流,欢迎常来 2011-10-26 19:31:26
哥们为啥不调用time函数呢
然后再传递,这样每次都不一样 哈哈
4楼2011-10-26 17:42:20
已阅   回复此楼   关注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的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 0703化学调剂 +3 pupcoco 2026-03-17 6/300 2026-03-18 10:11 by macy2011
[教师之家] 焦虑 +8 水冰月月野兔 2026-03-13 10/500 2026-03-18 09:21 by 咪呜喵呜
[考研] 08工科 320总分 求调剂 +4 梨花珞晚风 2026-03-17 4/200 2026-03-17 13:38 by houyaoxu
[考研] 211本,11408一志愿中科院277分,曾在中科院自动化所实习 +6 Losir 2026-03-12 7/350 2026-03-17 12:09 by danranxie
[考研] 333求调剂 +3 文思客 2026-03-16 7/350 2026-03-16 18:21 by 文思客
[考研] 一志愿985,本科211,0817化学工程与技术319求调剂 +5 Liwangman 2026-03-15 5/250 2026-03-16 17:10 by 我的船我的海
[考研] 283求调剂 +10 小楼。 2026-03-12 14/700 2026-03-16 16:08 by 13811244083
[考研] 中科院材料273求调剂 +4 yzydy 2026-03-15 4/200 2026-03-16 15:59 by Gaodh_82
[考研] 材料与化工一志愿南昌大学327求调剂推荐 +7 Ncdx123456 2026-03-13 8/400 2026-03-16 12:15 by karry wen
[考研] 材料与化工 323 英一+数二+物化,一志愿:哈工大 本人本科双一流 +4 自由的_飞翔 2026-03-13 5/250 2026-03-14 19:39 by hmn_wj
[考研] 材料080500调剂求收留 +3 一颗meteor 2026-03-13 3/150 2026-03-14 10:54 by peike
[考研] 266求调剂 +4 学员97LZgn 2026-03-13 4/200 2026-03-14 08:37 by zhukairuo
[考研] 一志愿西南交大,材料专硕317求调剂 +5 lx8568 2026-03-11 5/250 2026-03-13 21:43 by peike
[考研] 工科,求调剂 +3 我887 2026-03-11 3/150 2026-03-13 21:39 by JourneyLucky
[考研] 26调剂/材料科学与工程/总分295/求收留 +9 2026调剂侠 2026-03-12 9/450 2026-03-13 20:46 by 18595523086
[考研] 求调剂 +3 程雨杭 2026-03-12 3/150 2026-03-13 15:06 by JourneyLucky
[考研] 0817化学工程与技术考研312分调剂 +3 T123 tt 2026-03-12 3/150 2026-03-13 10:49 by houyaoxu
[考研] 化工学硕306求调剂 +9 42838695 2026-03-12 9/450 2026-03-13 10:16 by houyaoxu
[考研] 270求调剂 085600材料与化工专硕 +3 YXCT 2026-03-11 3/150 2026-03-13 10:13 by houyaoxu
[考博] 福州大学杨黄浩课题组招收2026年专业学位博士研究生,2026.03.20截止 +3 Xiangyu_ou 2026-03-12 3/150 2026-03-13 09:36 by duanwu655
信息提示
请填处理意见