一个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编译老出错啊啊,谢谢!

 

 

原创粉丝点击