一个C++关于类使用的问题!
来源:互联网 发布:调查问卷怎么分析数据 编辑:程序博客网 时间:2024/05/16 08:18
这是我的头文件:
#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的 一个问题
- 一个C++关于类使用的问题!
- 一个关于C++的问题
- 一个关于C语言编程的问题
- 关于自学C语言的一个问题
- 问一个关于c的问题
- 一个关于C语言输入的问题
- 一个关于C语言输入的问题
- 关于C语言的一个问题
- C关于字符串的一个小问题
- 关于C语言的一个小问题
- c语言的一个关于%用法问题
- 关于java中的URL类的使用一个问题
- 关于typedef使用的一个问题
- 关于postgresql索引使用的一个问题
- 一个关于memset使用的问题
- 关于使用strtok的一个小问题
- 关于RubyMine使用的一个问题
- 反射备忘
- 求:html注册界面加功能
- 逻辑读和物理读
- 配置文件的读取(.config)
- 判断是否为2的n次方
- 一个C++关于类使用的问题!
- c# NameValueCollection类读取配置信息
- 递归n的阶乘
- ‘求数组的第几大数’修改后的程序
- 浅谈数据库索引
- 开源游戏引擎介绍
- aotorun专杀工具(U盘病毒专杀)
- 架构
- 二道读程序题目