[C++ Primer Plus] 第11章、使用类(一)程序清单——重载 P408

程序清单11.4~11.6(运算符重载——添加加法运算符)

//1.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 Sum(const Time & t) const;
    //只要把运算符(这里为“+”)放到operator后面,并将结果用做方法名即可
    void show() const;
};

//1.cpp
#include <iostream>  
#include "1.h"  
using namespace std;

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 {
    cout <<" "<< hours << " hours, " << minutes << " minutes"<<endl;
}

//main.cpp
#include<iostream>
#include "1.h"
using namespace std;

void main() {
    Time plan,total;
    Time coding(2, 40);
    Time fixing(5, 55);

    cout << "planning time =";
    plan.show();
    cout << "coding time =";
    coding.show();
    cout << "fixing time =";
    fixing.show();

    int a = 5, b = 6;
    cout << "a+b="<<a + b << endl;//int型加法
    total = coding + fixing;//Time型加法(即“+”号重载)
    cout << "coding + fixing =";
    total.show();

    Time morefixing(3, 28);
    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+60*t.hours;
    tot2=minutes+60*hours;
    diff.minutes=(tot2-tot1)%60;
    diff.hours=(tot2-tot1)/60;
    return diff;
}
Time Time::operator*(double n) const{
    Time result;
    long totalMinutes=hours*n*60+minutes*n;
    result.hours=totalMinutes/60;
    result.minutes=totalMinutes%60;
    return result;
}

//main.cpp
#include<iostream>
#include "1.h"
using namespace std;

void main() {
    Time total,diff,adjust;
    Time weeding(4, 35);
    Time waxing(2, 47);

    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(3, 35);
    Time tosca(2, 48);

    cout << "Aida and Tosca:
";
    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 //1.h
  2 #ifndef VECTOR_H_
  3 #define VECTOR_H_
  4 #include<iostream>
  5 
  6 namespace VECTOR{
  7     class Vector    //类声明
  8     {
  9     public:
 10         enum Mode{RECT,POL};
 11     private:
 12         double x;
 13         double y;
 14         double mag;//长度
 15         double ang;//角度
 16         Mode mode;
 17         void set_mag();
 18         void set_ang();
 19         void set_x();
 20         void set_y();
 21     public:
 22         Vector();//默认构造函数
 23         Vector(double n1,double n2,Mode form=RECT);
 24         void reset(double n1,double n2,Mode form=RECT);
 25         ~Vector();//析构函数
 26         //以下四个函数在类声明中定义,因此自动成为内联函数;不改变对象数据,所以声明时使用const
 27         double xval() const {return x;}
 28         double yval() const {return y;}
 29         double magval() const {return mag;}
 30         double angval() const {return ang;}
 31         void polar_mode();
 32         void rect_mode();
 33         //重载
 34         Vector operator+(const Vector &b) const;
 35         Vector operator-(const Vector &b) const;
 36         Vector operator-() const;
 37         Vector operator*(double n) const;
 38         //友元
 39         friend Vector operator*(double n,const Vector &a);
 40         friend std::ostream &operator<<(std::ostream &os,const Vector &V);
 41     };
 42 }
 43 #endif
 44 
 45 //1.cpp
 46 #include <cmath>  
 47 #include "1.h" //include <iostream> 
 48 using namespace std;
 49 
 50 namespace VECTOR{
 51     const double Rad_to_deg=45.0/atan(1.0);
 52 
 53     void Vector::set_mag(){
 54         mag=sqrt(x*x+y*y);
 55     }
 56     void Vector::set_ang(){
 57         if(x==0.0&&y==0.0)
 58             ang=0.0;
 59         else
 60             ang=atan2(y,x);
 61     }
 62     void Vector::set_x(){
 63         x=mag*cos(ang);
 64     }
 65     void Vector::set_y(){
 66         y=mag*sin(ang);
 67     }
 68 
 69     Vector::Vector(){//默认构造函数
 70         x=y=mag=ang=0.0;
 71         mode=RECT;
 72     }
 73     Vector::Vector(double n1,double n2,Mode form){
 74         mode=form;
 75         if(form==RECT){
 76             x=n1;
 77             y=n2;
 78             set_mag();
 79             set_ang();
 80         }else if(form==POL){
 81             mag=n1;
 82             ang=n2/Rad_to_deg;
 83             set_x();
 84             set_y();
 85         }else{
 86             cout<<"Incorrect 3rd argument to Vector() -- vector set to 0"<<endl;
 87             x=y=mag=ang=0.0;
 88             mode=RECT;
 89         }
 90     }
 91 
 92     void Vector::reset(double n1,double n2,Mode form){
 93         mode=form;
 94         if(form==RECT){
 95             x=n1;
 96             y=n2;
 97             set_mag();
 98             set_ang();
 99         }else if(form==POL){
100             mag=n1;
101             ang=n2/Rad_to_deg;
102             set_x();
103             set_y();
104         }else{
105             cout<<"Incorrect 3rd argument to Vector() -- vector set to 0"<<endl;
106             x=y=mag=ang=0.0;
107             mode=RECT;
108         }
109     }
110     Vector::~Vector(){}//析构函数
111 
112     void Vector::polar_mode(){
113         mode=POL;
114     }
115     void Vector::rect_mode(){
116         mode=RECT;
117     }
118 
119     Vector Vector::operator+(const Vector &b) const{
120         return Vector(x+b.x,y+b.y);
121     }
122     Vector Vector::operator-(const Vector &b) const{
123         return Vector(x-b.x,y-b.y);
124     }
125     Vector Vector::operator-() const{
126         return Vector(-x,-y);
127     }
128     Vector Vector::operator*(double n) const{
129         return Vector(n*x,n*y);
130     }
131 
132     Vector operator*(double n,const Vector &a){
133         return a*n;
134     }
135     ostream &operator<<(ostream &os,const Vector &v){
136         if(v.mode==Vector::RECT)
137             os<<"(x,y)=("<<v.x<<", "<<v.y<<")";
138         else if(v.mode==Vector::POL)
139             os<<"(m,a)=("<<v.mag<<", "<<v.ang*Rad_to_deg<<")";
140         else
141             os<<"Vector object mode is invalid";
142         return os;
143     }
144 }
145 
146 //main.cpp
147 #include<iostream>
148 #include<cstdlib>    //rand(),srand()
149 #include<ctime>    //time()
150 #include "1.h"
151 using namespace std;
152 using VECTOR::Vector;
153 
154 void main() {
155     srand(time(0));    //随机数种子生成器
156     double direction;
157     Vector step;
158     Vector result(0.0,0.0);
159     unsigned long steps=0;
160     double target;
161     double dstep;
162     cout << "Enter target distance (q to quit):";
163     while (cin>>target)
164     {
165         cout<<"Enter step length:";
166         if(!(cin>>dstep))//输入错误就退出
167             break;
168         while (result.magval()<target)
169         {
170             direction=rand()%360;
171             step.reset(dstep,direction,Vector::POL);
172             result=result+step;
173             steps++;
174         }
175         cout<<"After "<<steps<<" steps,the subject has the following lacation:"<<endl;
176         cout<<result<<endl;
177         result.polar_mode();
178         cout<<" or
"<<result<<endl;
179         cout<<"Average outward distance per step= "<<result.magval()/steps<<endl;
180         steps=0;
181         result.reset(0.0,0.0);
182         cout << "Enter target distance (q to quit):";
183     }
184     cout<<"Bye!
";
185     cin.clear();
186     while (cin.get()!='
')//直到换行才退出
187         continue;
188     
189     system("pause");
190 }

 未完……

原文地址:https://www.cnblogs.com/little-monkey/p/7763906.html