24小时热门版块排行榜    

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

wuyan070987

金虫 (小有名气)

俺不是文化银

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

试编写一个有关日期(年、月、日)和时间(时、分、秒)的程序。该程序建立三个类,其中一个是日期的类Date,一个是时间的类Time,另一个是日期和时间类DateTime,它是前面两个类为基类的派生类。
回复此楼
做简单事,立简单人
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

夜色如水

新虫 (初入文坛)


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

huycwork

金虫 (著名写手)

【答案】应助回帖


jjdg(金币+1): 感谢参与 2011-06-25 11:22:29
boost不是有现成的么?
漩涡的中心有一块空地,空空的。
3楼2011-06-25 09:00:28
已阅   回复此楼   关注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的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿 江南大学 085602 化工专硕 338分求调剂 +16 路痴小琪 2026-04-05 16/800 2026-04-10 08:08 by kangsm
[考研] 本科211 工科085400 280分求调剂 可跨专业 +3 LZH(等待调剂中 2026-04-09 3/150 2026-04-09 21:29 by wutongshun
[考研] 301求调剂 +5 静静想想 2026-04-05 5/250 2026-04-09 20:58 by 运气yunqi
[考研] 287求调剂 +12 Fnhc 2026-04-07 18/900 2026-04-09 19:46 by vgtyfty
[考研] 291分调剂 +3 上岸小莹加油 2026-04-09 4/200 2026-04-09 17:28 by 1753564080
[考研] 348求调剂 +3 candyyyi 2026-04-09 3/150 2026-04-09 17:20 by 段伟艳
[考研] 070300化学279求调剂 +17 哈哈哈^_^ 2026-04-08 18/900 2026-04-09 10:49 by 三七七想上岸
[考研] 270求调剂 +3 031127 2026-04-06 4/200 2026-04-08 21:00 by 逆水乘风
[考研] 293分求调剂,外语为俄语 +7 加一一九 2026-04-07 10/500 2026-04-08 20:14 by yutian743
[考研] 土木水利专硕276分求调剂 +6 我想上学!!6 2026-04-05 9/450 2026-04-08 17:45 by 宋小宝HQ
[考研] 274求调剂求调剂 +10 Jachenbingoo 2026-04-06 13/650 2026-04-08 14:25 by zhq0425
[考研] 344求调剂 +11 魏子per 2026-04-07 11/550 2026-04-07 23:01 by JourneyLucky
[考研] 333求调剂 +6 合乘杨习夕 2026-04-06 6/300 2026-04-07 09:44 by 猪会飞
[考研] 287分求调剂 有专利国奖一志愿哈工大085406 +6 白易辰 2026-04-06 7/350 2026-04-06 22:46 by 875465
[考研] 专硕304找调剂,一线城市最好 +3 李lsl李 2026-04-05 3/150 2026-04-06 12:16 by ffffjjjj
[考研] 材料调剂 +6 一样YWY 2026-04-05 6/300 2026-04-05 20:30 by 南航~万老师
[考研] 081200-11408-276学硕求调剂 +4 崔wj 2026-04-04 5/250 2026-04-05 14:06 by imissbao
[考研] 272求调剂 +4 松柏常青5 2026-04-03 4/200 2026-04-04 17:03 by babysonlkd
[考研] 268求调剂 +8 你好tg 2026-04-03 9/450 2026-04-04 05:08 by gswylq
[考研] 求调剂 +3 usbdndj 2026-04-03 3/150 2026-04-03 14:10 by dxiaoxin
信息提示
请填处理意见