类与继承1.2

/*2.声明一个时间类,时间类中有3个私有数据成员(Hour,Minute,Second)和
两个公有成员函数(SetTime和PrintTime)。SetTime根据传递的3个参数为对象设置时间;
PrintTime负责将对象表示的时间显示输出,输出格式为“Hour:Minute:Second”。
  (1)在主函数中,建立一个时间类的对象,设置时间为9点20分30秒并显示该时间。
  (2)使用构造函数代替上面的SetTime成员函数,并在主函数中使用构造函数设置时间为10点40分50秒,
并显示该时间。
  (3)重载时间类的构造函数(不带参数)使小时、分、秒均为0。
  (4)在时间类的析构函数中输出"Good bye!”
  (5)定义拷贝构造函数并调用。*/
#include<iostream>
using namespace std;

class Time
    {
    private :
        int Hour,Minute,Second;
    public:
        Time(int hour,int minute,int second);
        void PrintTime();
    };

Time::Time(int hour,int minute,int second)
{
    Hour = hour;
    Minute = minute;
    Second = second;
}


void Time::PrintTime()
{
    cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}

int main()
{
    Time A(10,40,50);
    A.PrintTime();
    return 0;
}






#include<iostream>
using namespace std;

class Time
    {
    private :
        int Hour,Minute,Second;
    public:
        void SetTime(int hour,int minute,int second);
        void PrintTime();
    };

void Time::SetTime(int hour,int minute,int second)
{
    Hour = hour;
    Minute = minute;
    Second = second;
}

void Time::PrintTime()
{
    cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}

int main()
{
    Time A;
    A.SetTime(9,20,30);
    A.PrintTime();
    return 0;
}



#include<iostream>
using namespace std;

class Time
    {
    private :
        int Hour,Minute,Second;
    public:
        Time(int hour,int minute,int second);
        Time();
        void PrintTime();
        ~Time();
    };

Time::Time(int hour,int minute,int second)
{
    Hour = hour;
    Minute = minute;
    Second = second;
}

Time::~Time()
{

}

Time::Time()
{
    Hour = 0;
    Minute = 0;
    Second = 0;
}

void Time::PrintTime()
{
    cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}

int main()
{
    Time A;
    A.PrintTime();
    return 0;
}






#include<iostream>
using namespace std;

class Time
    {
    private :
        int Hour,Minute,Second;
    public:
        Time(int hour,int minute,int second);
        Time();
        void PrintTime();
        ~Time();
        Time(const Time &A);
    };

Time::Time(int hour,int minute,int second)
{
    Hour = hour;
    Minute = minute;
    Second = second;
}

Time::~Time()
{
    cout << "Good bye" << endl;
}

Time::Time(const Time &A)
{
    Hour = A.Hour;
    Minute = A.Minute;
    Second = A.Second;
}

Time::Time()
{
    Hour = 0;
    Minute = 0;
    Second = 0;
}

void Time::PrintTime()
{
    cout << "Hour:" << Hour << " " << "Minute:" << Minute << " " << "second:" << Second <<endl;
}

int main()
{
    Time A(9,20,40);
    Time A1(A);
    A1.PrintTime();
    return 0;
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值