• C++日期类、时间类、日期时间类的设计


    C++日期类、时间类、日期时间类的设计

    DateTimeHeader.h

    1.//日期类  
    2.class Date{  
    3.    public:  
    4.        int day;  
    5.        int month;  
    6.        int year;  
    7.    public:  
    8.        void show();  
    9.        // Date():year(1900),month(1),day(1){}  
    10.        Date(){};  
    11.        void init(int,int,int);  
    12.        // Date operator +(Date&);  
    13.        // Date operator =(Date);  
    14.        Date operator ++();  
    15.        Date operator ++(int);  
    16.};  
    17.//时间类  
    18.class Time{  
    19.    public:  
    20.        int Hour;  
    21.        int Minute;  
    22.        int Second;  
    23.        void show();  
    24.        Time():Hour(12),Minute(0),Second(0){}  
    25.        void init(int x,int y,int z);  
    26.        Time operator +(Time&);  
    27.        Time operator =(Time);  
    28.        Time operator ++();  
    29.};  
    30.  
    31.//时间日期类  
    32.class Calender{  
    33.    private:  
    34.        Time time;  
    35.        Date date;  
    36.    public:  
    37.        ~Calender();  
    38.        Calender(Time time,Date date);  
    39.        Calender operator +(Calender&);  
    40.        Calender operator =(Calender);  
    41.        Calender operator ++();  
    42.        void show();  
    }; 
    

    DateTimeFunction.cpp

    1.#include "DateTimeHeader.h"  
    2.#include<iostream>  
    3.using namespace std;  
    4.  
    5.  
    6.  
    7.void Date::init(int y,int m,int d){  
    8.    this->year = (y>1900 && y<=2200) ? y : 1900;  
    9.    this->month = (m>= 1 && m<=12) ? m : 1;  
    10.    this->day = (d>=1 && d<=31) ? d : 1;  
    11.}  
    12.  
    13.void Time::init(int h,int m,int s){  
    14.    this->Hour = (h>=1 && h<=24) ? h : 12;  
    15.    this->Minute = (m>=0 && m<=59) ? m : 0;  
    16.    this->Second = (s>=0 && s<=59) ? s : 0;  
    17.}  
    18.  
    19.void Date::show(){  
    20.    cout<<year<<"年"<<month<<"月"<<day<<"日"<<endl;  
    21.}  
    22.  
    23.void Time::show(){  
    24.    cout<<Hour<<"时"<<Minute<<"分"<<Second<<"秒"<<endl;  
    25.}  
    26.  
    27.void Calender::show(){  
    28.    cout<<"时间是:";  
    29.    time.show();  
    30.    cout<<"日期是:";  
    31.    date.show();  
    32.}  
    33.  
    34.Time Time::operator +(Time& t){  
    35.    Time tt;  
    36.    tt.Hour = this->Hour +t.Hour;  
    37.    tt.Minute = this->Minute + t.Minute;  
    38.    tt.Second = this->Second + t.Second;  
    39.    if(tt.Second>=60){  
    40.        tt.Second = tt.Second - 60;  
    41.        tt.Minute ++;  
    42.    }  
    43.    if(tt.Minute>=60){  
    44.        tt.Minute =tt.Minute -60;  
    45.        tt.Hour++;  
    46.    }  
    47.    if(tt.Hour>=24){  
    48.        tt.Hour = tt.Hour -24;  
    49.    }  
    50.    return tt;  
    51.}  
    

    main2.cpp

    1.#include "DateTimeFunction.cpp"  
    2.#include<iostream>  
    3.  
    4.using namespace std;  
    5.  
    6.int main(){  
    7.    Calender cal;  
    8.    Date date;  
    9.    Time time;  
    10.    date.init(2020,4,5);  
    11.    time.init(10,27,56);  
    12.    cal.date = date;  
    13.    cal.time = time;  
    14.    cal.show();  
    15.    return 0;  
    16.}  
    

    运行结果:

    1587021110378

  • 相关阅读:
    客户端负载均衡Feign之一:申明式服务调用Feign入门示例
    客户端负载均衡Ribbon之一:Spring Cloud Netflix负载均衡组件Ribbon介绍
    客户端负载均衡Ribbon之二:Loadbalance的源码
    spring4.0之九:websocket简单应用
    spring4.0之八:Groovy DSL
    spring4.0之七:Ordering Autowired Collections
    spring4.0之六:Generic Qualifier(泛型限定)
    spring4.0之五:@Conditional在满足特定条件下,才会实例化对象
    spring4.0之三:@RestController
    Spring4.0之四:Meta Annotation(元注解)
  • 原文地址:https://www.cnblogs.com/lightice/p/12713084.html
Copyright © 2020-2023  润新知