用c++编写一个时间类Time,包含时,分,秒3个数据成员
另外要求:1.time类的定义包含在头文件time.h中;
2.成员函数的定义包含在源程序文件time.cpp中;
3.主函数的定义包含在源程序文件client.cpp中。 展开
#include<iostream.h>
classTimer
{
longsecond;
public:
Timer(intsecond)
{
this->second=second;
}
Timer(intminute,intsecond)
{
this->second=minute*60+second;
}
Timer(inthour,intminute,intsecond)
{
this->second=hour*60+minute*60+second;
}
intgetSecond()
{
returnsecond;
}
};
voidmain()
{
Timert1(45),t2(10,25),t3(1,4,10),t4(2,16,0);
cout<<"Fromt1tot2:"<<t2.getSecond()-t1.getSecond()<<endl;
cout<<"Fromt3tot4:"<<t4.getSecond()-t3.getSecond()<<endl;
}
扩展资料
定义一个日期类:包括年、月、日三个成员变量,显示日期的方法
publicclassDemo{
publicstaticvoidmain(String[]args){
Datedate1=newDate(1994,5,22);
date1.showInfo();
Datedate2=newDate();
date2.year=1995;
date2.month=6;
date2.day=29;
date2.showInfo();
}
}
//日期类:
publicclassDate{
intyear;
intmonth;
intday;
//构造方法
publicDate(intyear,intmonth,intday){
this.year=year;
this.month=month;
this.day=day;
}
publicDate(){
}
publicvoidshowInfo(){
System.out.println(year+"年"+month+"月"+day+"日");
}
}
#include<iostream>
using namespace std;
class Time
{
public:
Time(int a,int b, int c);
void display();
private:
int hour;
int min;
int sec;
};
2:time.cpp
#include<iostream>
#include"time.h"
Time::Time(int a,int b,int c)
{
hour=a;
min=b;
sec=c;
}
void Time::display()
{
cout<<hour<<":"<<min<<":"<<sec<<endl;
}
3:client.cpp
#include<iostream>
#include"time.h"
using namespace std;
void main()
{
Time t(12,13,45);
t.display();
}
运行过了,不懂可追问。