书籍:C++ Primer Plus (第六版)(中文版)
工具:Dev-C++ 5.11
电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz
系统类型:64位操作系统,基于X64的处理器 windows10 专业版
第11章 使用类
11.1 运算符重载
11.2 计算时间:一个运算符重载实例
实例11.1
mytime0.h
cpp
#ifndef MYTIME0_H_
#define MYTIME0_H_
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h=0,int m=0);
Time Sum(const Time & t) const;
void Show() const;
};
#endif
mytime0.cpp
cpp
#include <iostream>
#include "mytime0.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;
}
void Time::AddHr(int h)
{
hours += h;
}
void Time::Reset(int h,int m)
{
hours = h;
minutes = m;
}
Time Time::Sum(const Time & t) const
{
Time sum;
sum.minutes=minutes+t.minutes;
sum.hours=hours+t.hours+sum.minutes/60;
sum.minutes %=60;
return sum;
}
void Time::Show() const
{
std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}
usemytime0.cpp
cpp
#include <iostream>
#include "mytime0.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2,40);
Time fixing(5,55);
Time total;
cout<<"Planning time = ";
planning.Show();
cout<<endl;
cout<<"coding time = ";
coding.Show();
cout<<endl;
cout<<"fixing time = ";
fixing.Show();
cout<<endl;
total = coding.Sum(fixing);
cout<<"coding.Sum(fixing)= ";
total.Show();
cout<<endl;
return 0;
}
编译运行结果:
cpp
Planning time = 0 hours, 0 minutes.
coding time = 2 hours, 40 minutes.
fixing time = 5 hours, 55 minutes.
coding.Sum(fixing)= 8 hours, 35 minutes.
--------------------------------
Process exited after 1.519 seconds with return value 0
请按任意键继续. . .
实例11.2
mytime1.h
cpp
#ifndef MYTIME1_H_
#define MYTIME1_H_
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void AddMin(int m);
void AddHr(int h);
void Reset(int h=0,int m=0);
Time operator+(const Time & t) const;
void Show() const;
};
#endif
mytime1.cpp
cpp
#include <iostream>
#include "mytime1.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::AddHr(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.minutes/60;
sum.minutes %=60;
return sum;
}
void Time::Show() const
{
std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}
usemytime1.cpp
cpp
#include <iostream>
#include "mytime1.h"
int main()
{
using std::cout;
using std::endl;
Time planning;
Time coding(2,40);
Time fixing(5,55);
Time total;
cout<<"Planning time = ";
planning.Show();
cout<<endl;
cout<<"coding time = ";
coding.Show();
cout<<endl;
cout<<"fixing time = ";
fixing.Show();
cout<<endl;
total = coding + fixing;
cout<<"coding + fixing = ";
total.Show();
cout<<endl;
Time morefixing(3,28);
cout<<" more fixing time = ";
morefixing.Show();
cout<<endl;
total=morefixing.operator+(total);
cout<<"morefixing.operator+(total) = ";
total.Show();
cout<<endl;
return 0;
}
编译运行结果:
cpp
Planning time = 0 hours, 0 minutes.
coding time = 2 hours, 40 minutes.
fixing time = 5 hours, 55 minutes.
coding + fixing = 8 hours, 35 minutes.
more fixing time = 3 hours, 28 minutes.
morefixing.operator+(total) = 12 hours, 3 minutes.
--------------------------------
Process exited after 0.5087 seconds with return value 0
请按任意键继续. . .
实例11.3
mytime2.h
cpp
#ifndef MYTIME2_H_
#define MYTIME2_H_
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void AddMin(int m);
void AddHr(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;
void Show() const;
};
#endif
mytime2.cpp
cpp
#include <iostream>
#include "mytime2.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::AddHr(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.minutes/60;
sum.minutes %=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;
}
void Time::Show() const
{
std::cout<<hours<<" hours, "<<minutes<<" minutes.\n";
}
usemytime2.cpp
cpp
#include <iostream>
#include "mytime2.h"
int main()
{
using std::cout;
using std::endl;
Time weeding(4,35);
Time waxing(2,47);
Time total;
Time diff;
Time adjusted;
cout<<"weeding time = ";
weeding.Show();
cout<<endl;
cout<<"waxing time = ";
waxing.Show();
cout<<endl;
cout<<"total work time = ";
total = weeding+waxing;
total.Show();
cout<<endl;
diff = weeding - waxing;
cout<<"weeding time - waxing time = ";
diff.Show();
cout<<endl;
adjusted=total*1.5;
cout<<" adjusted work time = ";
adjusted.Show();
cout<<endl;
return 0;
}
编译运行结果:
cpp
weeding time = 4 hours, 35 minutes.
waxing time = 2 hours, 47 minutes.
total work time = 7 hours, 22 minutes.
weeding time - waxing time = 1 hours, 1 minutes.
adjusted work time = 11 hours, 3 minutes.
--------------------------------
Process exited after 0.5112 seconds with return value 0
请按任意键继续. . .
11.3 友元
实例11.4
mytime3.h
cpp
#ifndef MYTIME3_H_
#define MYTIME3_H_
#include <iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h,int m=0);
void AddMin(int m);
void AddHr(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;}
friend std::ostream & operator<<(std::ostream & os,const Time & t);
};
#endif
mytime3.cpp
cpp
#include <iostream>
#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::AddHr(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.minutes/60;
sum.minutes %=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";
return os;
}
usemytime3.cpp
cpp
#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<<endl;
temp=aida+tosca;
cout<<"Aida + Tosca: "<<temp<<endl;
temp=aida*1.17;
cout<<"Aida * 1.17: "<<temp<<endl;
cout<<"10.0 * Tosca: "<<10.0*tosca<<endl;
return 0;
}
编译运行结果:
cpp
Aida and Tosca:
3 hours, 35 minutes; 2 hours, 48 minutes
Aida + Tosca: 6 hours, 23 minutes
Aida * 1.17: 4 hours, 11 minutes
10.0 * Tosca: 28 hours, 0 minutes
--------------------------------
Process exited after 0.521 seconds with return value 0
请按任意键继续. . .