今天学习了源文件与头文件的功能与区别,本文以一个简单的time类的定义与实现来剖析
头文件stdafx.h内容 类的定义
class Time{
private:
int hour;
int minute;
int second;
public:
void setTime(int,int,int);
void showTime(); //类里定义了两个函数,类的数据成员称为成员变量,成员函数称为方法
};
源文件stdafx.cpp内容 类的实现
void Time ::setTime(int h,int m,int s)//
{
hour=(h>=0&&h<24)?h:0;//经典a>b?a:b模型
minute=(m>=0&&m<60)?m:0;
second=(s>=0&&s<60)?s:0;
}
void Time :: showTime()
{
cout<<hour<<':'<<minute<<':'<<second<<endl;
}
源文件自己命名的.cpp 只放进去一个main主函数就可以
int _tmain(int argc, _TCHAR* argv[])
{
Time EndTime;
EndTime.setTime(12,23,36);
cout<<"The end time is: ";
EndTime.showTime();
system("pause");
return 0;
}