一个C++关于类使用的问题!
2009-10-25 17:57
573 查看
这是我的头文件:
#ifndef MYTIME3_H_INCLUDED
#define MYTIME3_H_INCLUDED
#include <iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void Addmin(int m);
void Addhou(int h);
void Reset(int h=0,int m=0);
Time operator+(const Time & t)const;
Time operator-(const Time & t)const;
Time operator*(double n)const;
friend Time operator*(double m,const Time & t)
{
return t*m;
}; //inline definition.
friend std::ostream & operator<<(std::ostream & os,const Time & t);
//overload the operator"<<" and return a object of ostream.
#endif // MYTIME3_H_INCLUDED
这是头文件中函数的定义:
#include "mytime3.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h,int m)
{
hours = h;
minutes = m;
}
void Time::Addmin(int m)
{
minutes += m;
hours += minutes/60;
minutes %= 60;
}
void Time::Addhou(int h)
{
hours += h;
}
void Time::Reset(int h,int m)
{
hours = h;
minutes = m;
}
Time Time::operator+(const Time & t)const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours;
sum.monutes %= 60;
return sum;
}
Time Time::operator-(const Time & t)const
{
Time diff;
int tot1,tot2;
tot1 = t.minutes + 60*t.hours;
tot2 = minutes + 60*hours;
diff.minutes = (tot2-tot1)%60;
diff.hours = (tot2-tot1)/60;
return diff;
}
Time Time::operator*(double mult)const
{
Time result;
long totalminutes = hours * mult * 60 + minutes *mult;
result.hours = totalminutes/60;
result.minutes = totalminutes %60;
return result;
}
std::ostream & operator<<(std::ostream &os,const Time & t)
{
os<<t.hours<<" hours, "<<t.minutes<<
" minutes."<<std::endl;
return os;
}
这是包含main()的文件:
#include <iostream>
#include "mytime3.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3,35);
Time tosca(2,48);
Time temp;
cout <<"Aida and Tosca:/n";
cout <<aida<<tosca;
temp = aida + tosca;
cout <<"Aida+Tosca: "<<temp;
temp = aida * 2;
cout <<"Aida*2: "<<temp;
cout <<"10*tosca: "<<10*tosca;
return 0;
}
哪个给看一下,我用VC++ 9.0编译老出错啊啊,谢谢!
#ifndef MYTIME3_H_INCLUDED
#define MYTIME3_H_INCLUDED
#include <iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void Addmin(int m);
void Addhou(int h);
void Reset(int h=0,int m=0);
Time operator+(const Time & t)const;
Time operator-(const Time & t)const;
Time operator*(double n)const;
friend Time operator*(double m,const Time & t)
{
return t*m;
}; //inline definition.
friend std::ostream & operator<<(std::ostream & os,const Time & t);
//overload the operator"<<" and return a object of ostream.
#endif // MYTIME3_H_INCLUDED
这是头文件中函数的定义:
#include "mytime3.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h,int m)
{
hours = h;
minutes = m;
}
void Time::Addmin(int m)
{
minutes += m;
hours += minutes/60;
minutes %= 60;
}
void Time::Addhou(int h)
{
hours += h;
}
void Time::Reset(int h,int m)
{
hours = h;
minutes = m;
}
Time Time::operator+(const Time & t)const
{
Time sum;
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours;
sum.monutes %= 60;
return sum;
}
Time Time::operator-(const Time & t)const
{
Time diff;
int tot1,tot2;
tot1 = t.minutes + 60*t.hours;
tot2 = minutes + 60*hours;
diff.minutes = (tot2-tot1)%60;
diff.hours = (tot2-tot1)/60;
return diff;
}
Time Time::operator*(double mult)const
{
Time result;
long totalminutes = hours * mult * 60 + minutes *mult;
result.hours = totalminutes/60;
result.minutes = totalminutes %60;
return result;
}
std::ostream & operator<<(std::ostream &os,const Time & t)
{
os<<t.hours<<" hours, "<<t.minutes<<
" minutes."<<std::endl;
return os;
}
这是包含main()的文件:
#include <iostream>
#include "mytime3.h"
int main()
{
using std::cout;
using std::endl;
Time aida(3,35);
Time tosca(2,48);
Time temp;
cout <<"Aida and Tosca:/n";
cout <<aida<<tosca;
temp = aida + tosca;
cout <<"Aida+Tosca: "<<temp;
temp = aida * 2;
cout <<"Aida*2: "<<temp;
cout <<"10*tosca: "<<10*tosca;
return 0;
}
哪个给看一下,我用VC++ 9.0编译老出错啊啊,谢谢!
相关文章推荐
- C++学习中关于cout遇到的一个小问题
- 关于MIUI6下使用Widget调用Toast的一个问题
- 关于Hibernate中fatch=eager的bag集合(一个java List)使用Criteria查询出现重复记录的问题
- C++中关于标准输出流cout中使用递增运算符的问题
- php关于使用iconv(...)函数对字符进行中文转换时,遇到的一个问题
- 求解 关于 套汇问题 要求 使用 C++
- C/C++ 设计模式:关于使用typeid时要注意的问题
- 实用程序软件包(关于使用静态函数报错问题“Static function declared but not defined in C+++ "eh.h is only for C++!"报错问题)
- 2017-6-14 关于使用history.go的问题!配合原生应用嵌入H5页面(只有一个webview的的单页应用)返回历史列表记录的问题
- cocos2dx使用tolua关于字符串处理的一个问题
- libmongoc关于\$pullAll和\$addToSet的一个使用问题记录
- 关于jquery版本为1.6+使用checkbox的checked属性来判断一个checkbox是否被选中问题
- 关于一个sql语句需要使用约等于的问题
- 一个关于AVPlayer蛋疼的释放问题(iOS 9.0 AVPlayer的使用与内存的释放问题)
- 关于android webview 图片使用同一个src导致只加载第一张的问题
- VC中一个关于宏的使用问题(转载)
- VC中一个关于宏的使用问题
- C++关于动态申请空间的一个问题
- C++库研究笔记——使用函数模板还是类模板?+ 一个类型重复问题的两种解决方法
- 关于c++下使用mysql的一些问题