24小时热门版块排行榜    

查看: 1964  |  回复: 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的回帖
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿华中科技大学,080502,354分求调剂 +4 守候夕阳CF 2026-03-18 4/200 2026-03-18 22:16 by li123456789.
[考研] 321求调剂 +3 何润采123 2026-03-18 3/150 2026-03-18 21:27 by li123456789.
[考研] 085601专硕,总分342求调剂,地区不限 +5 share_joy 2026-03-16 5/250 2026-03-18 14:48 by haxia
[考研] 288求调剂,一志愿华南理工大学071005 +4 ioodiiij 2026-03-17 4/200 2026-03-18 12:36 by Linda Hu
[考研] 材料,纺织,生物(0856、0710),化学招生啦 +3 Eember. 2026-03-17 9/450 2026-03-18 10:28 by Eember.
[基金申请] 被我言中:新模板不强调格式了,假专家开始管格式了 +4 beefly 2026-03-14 4/200 2026-03-17 22:04 by 黄鸟于飞Chao
[硕博家园] 湖北工业大学 生命科学与健康学院-课题组招收2026级食品/生物方向硕士 +3 1喜春8 2026-03-17 5/250 2026-03-17 17:18 by ber川cool子
[考研] 211本,11408一志愿中科院277分,曾在中科院自动化所实习 +6 Losir 2026-03-12 7/350 2026-03-17 12:09 by danranxie
[考研] 一志愿南京大学,080500材料科学与工程,调剂 +4 Jy? 2026-03-16 4/200 2026-03-17 11:02 by gaoqiong
[考研] 275求调剂 +4 太阳花天天开心 2026-03-16 4/200 2026-03-17 10:53 by 功夫疯狂
[考研] 278求调剂 +3 Yy7400 2026-03-13 3/150 2026-03-17 08:24 by laoshidan
[基金申请] 国自科面上基金字体 +6 iwuli 2026-03-12 7/350 2026-03-16 21:18 by sculhf
[考研] 0854控制工程 359求调剂 可跨专业 +3 626776879 2026-03-14 9/450 2026-03-16 17:42 by 626776879
[考研] 288求调剂 +4 奇点0314 2026-03-14 4/200 2026-03-14 23:04 by JourneyLucky
[考研] 中科大材料与化工319求调剂 +3 孟鑫材料 2026-03-14 3/150 2026-03-14 20:10 by ms629
[考研] 材料与化工 323 英一+数二+物化,一志愿:哈工大 本人本科双一流 +4 自由的_飞翔 2026-03-13 5/250 2026-03-14 19:39 by hmn_wj
[考研] 308 085701 四六级已过求调剂 +7 温乔乔乔乔 2026-03-12 14/700 2026-03-14 10:49 by JourneyLucky
[考研] 材料工程调剂 +9 咪咪空空 2026-03-12 9/450 2026-03-13 22:05 by 星空星月
[考研] 土木第一志愿276求调剂,科研和技能十分丰富,求新兴方向的导师收留 +3 土木小天才 2026-03-12 3/150 2026-03-13 15:01 by JourneyLucky
[考研] 290求调剂 +3 ADT 2026-03-13 3/150 2026-03-13 10:19 by peike
信息提示
请填处理意见