一个类从定义到调用

来源:互联网 发布:js 一段html 获取 img 编辑:程序博客网 时间:2024/05/21 02:37

TEST.H文件:




#include<iostream>
#include<string>
#ifndef TIME_H
#define TIME_H 1


class Time{
int h,m,s;
public:
Time(){h = m = s = 0;}//此处自动申请内联 即 inline
Time(int h,int m,int s);
void tick();
void show();
};


#endif






TEST.CPP文件:





#include<iostream>
#include<string>
#include<iomanip>


#include"test.h"
using namespace std;


Time::Time(int h,int m,int s)
{
Time::h = h;
Time::m = m;
Time::s = s;
}
void Time::tick()
{
if(++s>=60)
{
s = 0;
if(++m>=60)
{
m = 0;
if(++h>=24)
h = 0;
}
}
}
void Time::show()
{
cout<<setw(2)<<setfill('0')<<h<<":"<<setw(2)<<m<<":"<<setw(2)<<s<<endl;
}







TESTMAIN.CPP文件






#include"test.h"


int main()
{
Time t1;
Time t2(12,34,56);
t1.tick();
t2.tick();
t1.show();
t2.show();
}

0 0
原创粉丝点击