24小时热门版块排行榜    

查看: 1830  |  回复: 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 的主题更新
信息提示
请填处理意见