简单的定义类 定义对象以及在程序中使用对象

#include<iostream>
#include<cmath>
using namespace std;

/*定义类*/
class time
{
public:
    void SetTime(int newH = 0, int newM = 0, int newS = 0);
    void ShowTime();
private:
    int hour;
    int minute;
    int second;
}; 
//类方法的实现 方法名称相当于一个接口
void time::SetTime(int newH , int newM , int newS )
{
hour = newH;
minute = newM;
second = newS;
}

void time::ShowTime()
{
cout<<hour<<":"<<minute<<":"<<second;
} 

int main(){
time mytime;//定义一个time类的对象 mytime
mytime.SetTime(20 , 47 ,50);
mytime.ShowTime();
return 0;

}

猜你喜欢

转载自blog.csdn.net/qq_41814721/article/details/82502506