程序清单11.4~11.6(运算符重载——添加加法运算符)
//1.h
class Time {
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m = );
void AddMin(int m);
void AddHr(int h);
void Reset(int h=,int m=);
Time operator+(const Time & t) const;//重载之前为:Time Sum(const Time & t) const;
//只要把运算符(这里为“+”)放到operator后面,并将结果用做方法名即可
void show() const;
}; //1.cpp
#include <iostream>
#include "1.h"
using namespace std; Time::Time() {
hours = minutes = ;
} Time::Time(int h, int m) {
hours = h;
minutes = m;
}
void Time::AddMin(int m) {
minutes += m;
hours += minutes / ;
minutes %= ; }
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 / ;
sum.minutes %= ;
return sum;
}
void Time::show() const {
cout <<" "<< hours << " hours, " << minutes << " minutes"<<endl;
} //main.cpp
#include<iostream>
#include "1.h"
using namespace std; void main() {
Time plan,total;
Time coding(, );
Time fixing(, ); cout << "planning time =";
plan.show();
cout << "coding time =";
coding.show();
cout << "fixing time =";
fixing.show(); int a = , b = ;
cout << "a+b="<<a + b << endl;//int型加法
total = coding + fixing;//Time型加法(即“+”号重载)
cout << "coding + fixing =";
total.show(); Time morefixing(, );
cout << "more fixing time =";
morefixing.show(); total = morefixing.operator+(total);
cout << "morefixing.operator+(total) =";
total.show(); system("pause");
}
程序清单11.7~11.9
只贴出与上面代码不同的地方
//1.h
Time operator-(const Time & t) const;
Time operator*(double n) const; //1.cpp
Time Time::operator-(const Time & t) const{
Time diff;
int tot1,tot2;
tot1=t.minutes+*t.hours;
tot2=minutes+*hours;
diff.minutes=(tot2-tot1)%;
diff.hours=(tot2-tot1)/;
return diff;
}
Time Time::operator*(double n) const{
Time result;
long totalMinutes=hours*n*+minutes*n;
result.hours=totalMinutes/;
result.minutes=totalMinutes%;
return result;
} //main.cpp
#include<iostream>
#include "1.h"
using namespace std; void main() {
Time total,diff,adjust;
Time weeding(, );
Time waxing(, ); cout << "weeding time =";
weeding.show();
cout << "waxing time =";
waxing.show(); total = weeding + waxing;
cout << "weeding + waxing =";
total.show(); diff = weeding - waxing;
cout << "weeding - waxing =";
diff.show(); adjust = total * 1.5;
cout << "adjust work time =";
adjust.show(); system("pause");
}
程序清单11.10~11.12(友元)
只贴出与上面代码不同的地方
//1.h
friend Time operator*(double m,const Time &t) { return t*m; }//只有在类声明的原型中才能使用friend关键字
friend std::ostream & operator<<(std::ostream &os,const Time &t); //1.cpp
std::ostream & operator<<(std::ostream &os,const Time &t){
os << t.hours << " hours, " << t.minutes << " minutes";
return os;
} //main.cpp
#include<iostream>
#include "1.h"
using namespace std; void main() {
Time temp;
Time aida(, );
Time tosca(, ); cout << "Aida and Tosca:\n";
cout <<aida<<"; "<<tosca<<endl;
temp=aida+tosca;
cout << "Aida and Tosca:"<<temp<<endl;
temp=aida*1.17;
cout << "Aida * 1.17:"<<temp<<endl;
cout << "10.0 * Tosca:"<<10.0*tosca<<endl; system("pause");
}
程序清单11.13~11.15(Vector实现矢量操作:模拟随机漫步)
//1.h
#ifndef VECTOR_H_
#define VECTOR_H_
#include<iostream> namespace VECTOR{
class Vector //类声明
{
public:
enum Mode{RECT,POL};
private:
double x;
double y;
double mag;//长度
double ang;//角度
Mode mode;
void set_mag();
void set_ang();
void set_x();
void set_y();
public:
Vector();//默认构造函数
Vector(double n1,double n2,Mode form=RECT);
void reset(double n1,double n2,Mode form=RECT);
~Vector();//析构函数
//以下四个函数在类声明中定义,因此自动成为内联函数;不改变对象数据,所以声明时使用const
double xval() const {return x;}
double yval() const {return y;}
double magval() const {return mag;}
double angval() const {return ang;}
void polar_mode();
void rect_mode();
//重载
Vector operator+(const Vector &b) const;
Vector operator-(const Vector &b) const;
Vector operator-() const;
Vector operator*(double n) const;
//友元
friend Vector operator*(double n,const Vector &a);
friend std::ostream &operator<<(std::ostream &os,const Vector &V);
};
}
#endif //1.cpp
#include <cmath>
#include "1.h" //include <iostream>
using namespace std; namespace VECTOR{
const double Rad_to_deg=45.0/atan(1.0); void Vector::set_mag(){
mag=sqrt(x*x+y*y);
}
void Vector::set_ang(){
if(x==0.0&&y==0.0)
ang=0.0;
else
ang=atan2(y,x);
}
void Vector::set_x(){
x=mag*cos(ang);
}
void Vector::set_y(){
y=mag*sin(ang);
} Vector::Vector(){//默认构造函数
x=y=mag=ang=0.0;
mode=RECT;
}
Vector::Vector(double n1,double n2,Mode form){
mode=form;
if(form==RECT){
x=n1;
y=n2;
set_mag();
set_ang();
}else if(form==POL){
mag=n1;
ang=n2/Rad_to_deg;
set_x();
set_y();
}else{
cout<<"Incorrect 3rd argument to Vector() -- vector set to 0"<<endl;
x=y=mag=ang=0.0;
mode=RECT;
}
} void Vector::reset(double n1,double n2,Mode form){
mode=form;
if(form==RECT){
x=n1;
y=n2;
set_mag();
set_ang();
}else if(form==POL){
mag=n1;
ang=n2/Rad_to_deg;
set_x();
set_y();
}else{
cout<<"Incorrect 3rd argument to Vector() -- vector set to 0"<<endl;
x=y=mag=ang=0.0;
mode=RECT;
}
}
Vector::~Vector(){}//析构函数 void Vector::polar_mode(){
mode=POL;
}
void Vector::rect_mode(){
mode=RECT;
} Vector Vector::operator+(const Vector &b) const{
return Vector(x+b.x,y+b.y);
}
Vector Vector::operator-(const Vector &b) const{
return Vector(x-b.x,y-b.y);
}
Vector Vector::operator-() const{
return Vector(-x,-y);
}
Vector Vector::operator*(double n) const{
return Vector(n*x,n*y);
} Vector operator*(double n,const Vector &a){
return a*n;
}
ostream &operator<<(ostream &os,const Vector &v){
if(v.mode==Vector::RECT)
os<<"(x,y)=("<<v.x<<", "<<v.y<<")";
else if(v.mode==Vector::POL)
os<<"(m,a)=("<<v.mag<<", "<<v.ang*Rad_to_deg<<")";
else
os<<"Vector object mode is invalid";
return os;
}
} //main.cpp
#include<iostream>
#include<cstdlib> //rand(),srand()
#include<ctime> //time()
#include "1.h"
using namespace std;
using VECTOR::Vector; void main() {
srand(time()); //随机数种子生成器
double direction;
Vector step;
Vector result(0.0,0.0);
unsigned long steps=;
double target;
double dstep;
cout << "Enter target distance (q to quit):";
while (cin>>target)
{
cout<<"Enter step length:";
if(!(cin>>dstep))//输入错误就退出
break;
while (result.magval()<target)
{
direction=rand()%;
step.reset(dstep,direction,Vector::POL);
result=result+step;
steps++;
}
cout<<"After "<<steps<<" steps,the subject has the following lacation:"<<endl;
cout<<result<<endl;
result.polar_mode();
cout<<" or\n"<<result<<endl;
cout<<"Average outward distance per step= "<<result.magval()/steps<<endl;
steps=;
result.reset(0.0,0.0);
cout << "Enter target distance (q to quit):";
}
cout<<"Bye!\n";
cin.clear();
while (cin.get()!='\n')//直到换行才退出
continue; system("pause");
}
未完……