yohhhhhoyo 2022-11-12 10:50 采纳率: 84.6%
浏览 35
已结题

c++相关的面向对象编程

img


#include
#include
using namespace std;

class Time
{
public:
int hour;
int minute;
int second;

    Time()
    {
    int totalSeconds=time(0);
    int currentSecond=totalSeconds%60;
    int totalMinutes=totalSeconds/60;
    int totalMinute=int totalMinutes%60;
    int totalHours=totalMinutes/60;
    int currentHour=totallHours%24;
    
    cout<<"Current time is"<<currentHour<<":"<<currentMinute<<":"<<currentSecond<<endl;
    
    return 0;
    }    
    
    Time (int second)
    {
        cin>>second;
            int totalSeconds=second;
    int currentSecond=totalSeconds%60;
    int totalMinutes=totalSeconds/60;
    int totalMinute=int totalMinutes%60;
    int totalHours=totalMinutes/60;
    int currentHour=totallHours%24;
    
    cout<<"New time is 1970 year 1.1"<<currentHour<<":"<<currentMinute<<":"<<currentSecond<<endl;
    return 0;
    
    }
    Time(int hour,int minute,int second)
    {
        cin>>hour>>minute>>second;
        cout<<"The time is"<<hour<<":"<<minute<<":"<<second<<endl;
        return 0;
    }
    
    int get_hour()
    {
        return hour;
    }
    
    int get_minute()
    {
        return minute;
    }
    
    int get_second()
    {
        return second;
    }
    setTime(int elapseTime)
    {
    
    int totalSeconds=time(0);
    int currentSecond=totalSeconds%60;
    int totalMinutes=totalSeconds/60;
    int totalMinute=int totalMinutes%60;
    int totalHours=totalMinutes/60;
    int currentHour=totallHours%24;
    
    
    int a=elapseTime;
    int da=a%60;
    int b=a/60;
    int db=int b%60;
    int c=b/60;
    int dc=c%24;
    
    cout<<"Current time is"<<(currentHour+dc)<<":"<<(currentMinute+db)<<":"<<(currentSecond+da)<<endl;
    
    return 0;    
}    
    

};
int main()
{
cout<<Time()<<endl;
cout<<Time(555550)<<endl;

return 0;

}

  • 写回答

4条回答 默认 最新

  • 浪客 2022-11-12 11:54
    关注
    
    #include <iostream>
    using namespace std;
    
    class Time
    {
    private:
        int hour;
        int minute;
        int second;
    
        void sot(int seconds)
        {
            second = seconds % 60;
            int totalMinutes = seconds / 60;
            minute = totalMinutes % 60;
            int totalHours = totalMinutes / 60 + 8; //东8区
            hour = totalHours % 24;
        }
    
    public:
        Time()
        {
            sot(time(0));
        }
    
        Time(int second)
        {
            sot(second);
        }
        Time(int hour, int minute, int second) : hour(hour), minute(minute), second(second)
        {
        }
    
        int get_hour()
        {
            return hour;
        }
    
        int get_minute()
        {
            return minute;
        }
    
        int get_second()
        {
            return second;
        }
    
        void setTime(int elapseTime)
        {
            second += elapseTime;
            minute += second / 60;
            second %= 60;
            hour += minute / 60;
            minute %= 60;
            hour %= 24;
        }
    
        friend ostream &operator<<(ostream &out, Time &time)
        {
            out << time.hour << ":" << time.minute << ":" << time.second;
            return out;
        }
    };
    
    int main()
    {
        Time time1;
        cout << "Current time is: " << time1 << endl;
        Time time2(555550);
        cout << "New time is 1970 year 1.1: " << time2 << endl;
        time2.setTime(1200);
        cout << "The time is: " << time2 << endl;
    
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 11月20日
  • 已采纳回答 11月12日
  • 创建了问题 11月12日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分