#ifndef预处理

来源:互联网 发布:iphone照片导出软件 编辑:程序博客网 时间:2024/06/15 14:12

这是#ifndef,#define和#endif预处理指令的一个实例应用。可以瞧一瞧这样使用有没有问题吗?

main.c中#include 'time1.cpp'应该不对吧。

//time1.h-----------------------------------------
#ifndef TIME1_H
#define TIME1_H
 
class Time
{
public:
Time();
void setTime(int,int,int);
void printMilitary();
void printStandard();//便于封装
private:
int hour;
int minute;
int second;
};
#endif
//time1.cpp---------------------------
#include <iostream.h>
#include <stdio.h>
#include "time1.h"
 
Time::Time(){hour=minute=second=0;}//时间初值
 
void Time::setTime(int h,int m,int s)
{
hour=(h>=0&&h<24)?h:0;
minute=(m>=0&&m<60)?m:0;
second=(s>=0&&s<60)?s:0;
}
 
void Time::printMilitary()//不用传入类的内部参数,可以直接访问
{
printf("%02d:%02d/n",hour,minute);
}
 
void Time::printStandard()
{
printf("%02d:%02d:%02d ",hour,minute,second);
cout<<(hour<12?"AM":"PM")<<endl;
}
//main.cpp-----------------------------------
#include <iostream.h>
#include <stdio.h>
#include "time1.cpp"
 
int main()
{
Time t;
 
t.setTime(1,3,14);
t.printMilitary();
t.printStandard();
 
return 0;
}