时间类-类的定义,类作为“零件”的载体,有内部属性,有对外接口

发布时间 2023-04-20 23:15:44作者: PeitongShi

类作为"零件"的载体,有内部属性(private),有对外接口(public), 内部属性的数据成员或函数成员,仅仅供给class内部函数成员使用,不对外开放,public规定的对外开放的接口。

设置Cmytime类。

具有两个成员函数

int Set(int h,int m,int s)

Show()

输入  23  25 38

输出:  23:25:38

#include <iostream>
using namespace std;

class Cmytime
{
    public:
        void Set(int h,int m,int s)
        {
            hour=h;
            minute=m;
            second=s;
        }
        void Show()
        {
            cout << hour << ":" << minute << ":" << second;
        }
    private:
        int hour,minute,second;
};
//StudybarCommentBegin
int main(void) {
    int h,m,s;
   cin>>h>>m>>s;
  Cmytime t1;
t1.Set(h,m,s);
t1.Show();
    return 0;
}

//StudybarCommentEnd

-END