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

  1. //1.h
  2. class Time {
  3. private:
  4. int hours;
  5. int minutes;
  6. public:
  7. Time();
  8. Time(int h, int m = );
  9. void AddMin(int m);
  10. void AddHr(int h);
  11. void Reset(int h=,int m=);
  12. Time operator+(const Time & t) const;//重载之前为:Time Sum(const Time & t) const;
  13. //只要把运算符(这里为“+”)放到operator后面,并将结果用做方法名即可
  14. void show() const;
  15. };
  16.  
  17. //1.cpp
  18. #include <iostream>
  19. #include "1.h"
  20. using namespace std;
  21.  
  22. Time::Time() {
  23. hours = minutes = ;
  24. }
  25.  
  26. Time::Time(int h, int m) {
  27. hours = h;
  28. minutes = m;
  29. }
  30. void Time::AddMin(int m) {
  31. minutes += m;
  32. hours += minutes / ;
  33. minutes %= ;
  34.  
  35. }
  36. void Time::AddHr(int h) {
  37. hours += h;
  38. }
  39. void Time::Reset(int h , int m ) {
  40. hours = h;
  41. minutes = m;
  42. }
  43. Time Time::operator+(const Time & t) const {
  44. Time sum;
  45. sum.minutes = minutes + t.minutes;
  46. sum.hours = hours + t.hours + sum.minutes / ;
  47. sum.minutes %= ;
  48. return sum;
  49. }
  50. void Time::show() const {
  51. cout <<" "<< hours << " hours, " << minutes << " minutes"<<endl;
  52. }
  53.  
  54. //main.cpp
  55. #include<iostream>
  56. #include "1.h"
  57. using namespace std;
  58.  
  59. void main() {
  60. Time plan,total;
  61. Time coding(, );
  62. Time fixing(, );
  63.  
  64. cout << "planning time =";
  65. plan.show();
  66. cout << "coding time =";
  67. coding.show();
  68. cout << "fixing time =";
  69. fixing.show();
  70.  
  71. int a = , b = ;
  72. cout << "a+b="<<a + b << endl;//int型加法
  73. total = coding + fixing;//Time型加法(即“+”号重载)
  74. cout << "coding + fixing =";
  75. total.show();
  76.  
  77. Time morefixing(, );
  78. cout << "more fixing time =";
  79. morefixing.show();
  80.  
  81. total = morefixing.operator+(total);
  82. cout << "morefixing.operator+(total) =";
  83. total.show();
  84.  
  85. system("pause");
  86. }

程序清单11.7~11.9

只贴出与上面代码不同的地方

  1. //1.h
  2. Time operator-(const Time & t) const;
  3. Time operator*(double n) const;
  4.  
  5. //1.cpp
  6. Time Time::operator-(const Time & t) const{
  7. Time diff;
  8. int tot1,tot2;
  9. tot1=t.minutes+*t.hours;
  10. tot2=minutes+*hours;
  11. diff.minutes=(tot2-tot1)%;
  12. diff.hours=(tot2-tot1)/;
  13. return diff;
  14. }
  15. Time Time::operator*(double n) const{
  16. Time result;
  17. long totalMinutes=hours*n*+minutes*n;
  18. result.hours=totalMinutes/;
  19. result.minutes=totalMinutes%;
  20. return result;
  21. }
  22.  
  23. //main.cpp
  24. #include<iostream>
  25. #include "1.h"
  26. using namespace std;
  27.  
  28. void main() {
  29. Time total,diff,adjust;
  30. Time weeding(, );
  31. Time waxing(, );
  32.  
  33. cout << "weeding time =";
  34. weeding.show();
  35. cout << "waxing time =";
  36. waxing.show();
  37.  
  38. total = weeding + waxing;
  39. cout << "weeding + waxing =";
  40. total.show();
  41.  
  42. diff = weeding - waxing;
  43. cout << "weeding - waxing =";
  44. diff.show();
  45.  
  46. adjust = total * 1.5;
  47. cout << "adjust work time =";
  48. adjust.show();
  49.  
  50. system("pause");
  51. }

程序清单11.10~11.12(友元)

只贴出与上面代码不同的地方

  1. //1.h
  2. friend Time operator*(double m,const Time &t) { return t*m; }//只有在类声明的原型中才能使用friend关键字
  3. friend std::ostream & operator<<(std::ostream &os,const Time &t);
  4.  
  5. //1.cpp
  6. std::ostream & operator<<(std::ostream &os,const Time &t){
  7. os << t.hours << " hours, " << t.minutes << " minutes";
  8. return os;
  9. }
  10.  
  11. //main.cpp
  12. #include<iostream>
  13. #include "1.h"
  14. using namespace std;
  15.  
  16. void main() {
  17. Time temp;
  18. Time aida(, );
  19. Time tosca(, );
  20.  
  21. cout << "Aida and Tosca:\n";
  22. cout <<aida<<"; "<<tosca<<endl;
  23. temp=aida+tosca;
  24. cout << "Aida and Tosca:"<<temp<<endl;
  25. temp=aida*1.17;
  26. cout << "Aida * 1.17:"<<temp<<endl;
  27. cout << "10.0 * Tosca:"<<10.0*tosca<<endl;
  28.  
  29. system("pause");
  30. }

程序清单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()); //随机数种子生成器
  156. double direction;
  157. Vector step;
  158. Vector result(0.0,0.0);
  159. unsigned long steps=;
  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()%;
  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\n"<<result<<endl;
  179. cout<<"Average outward distance per step= "<<result.magval()/steps<<endl;
  180. steps=;
  181. result.reset(0.0,0.0);
  182. cout << "Enter target distance (q to quit):";
  183. }
  184. cout<<"Bye!\n";
  185. cin.clear();
  186. while (cin.get()!='\n')//直到换行才退出
  187. continue;
  188.  
  189. system("pause");
  190. }

未完……

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

  1. C++ primer plus读书笔记——第11章 使用类

    第11章 使用类 1. 运算符重载是一种形式的C++多态. 2. 不要返回指向局部变量或临时对象的引用.函数执行完毕后,局部变量和临时对象将消失,引用将指向不存在的数据. 3. 运算符重载的格式如下: ...

  2. C++ Primer:第七章:类

    定义一个类: class Myclass{ int data_i; string data_str; public: int getdata_i() const { return data_i; } ...

  3. C++ Primer 5th 第11章 关联容器

    练习11.1:描述map 和 vector 的不同. map是关联容器,vector是顺序容器,关联容器与值无关,vector则与值密切相关 练习11.2:分别给出最适合使用 list.vector. ...

  4. Prism 4 文档 ---第11章 部署Prism应用程序

        要成功移动Prism应用到生产中,需要对部署计划为应用程序的设计过程的一部分.本章介绍了注意事项和你需要采取的准备以部署应用程序,以及你要在用户手中获得部署程序所需要采取的行动.     Si ...

  5. 《C++ Primer Plus》第11章 使用类 学习笔记

    本章介绍了定义和使用类的许多重要方面.一般来说,访问私有类成员的唯一方法是使用类方法.C++使用友元函数来避开这种限制.要让函数称为友元,需要在类声明中声明该函数,并在声明前加上关键字friend.C ...

  6. 《C++ primer》--第11章

    习题11.1 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果.编写程序读取一系列int型数据,并将它们存储 ...

  7. 【WPF学习】第二十六章 Application类——应用程序的生命周期

    在WPF中,应用程序会经历简单的生命周期.在应用程序启动后,将立即创建应用程序对象,在应用程序运行时触发各种应用程序事件,你可以选择监视其中的某些事件.最后,当释放应用程序对象时,应用程序将结束. 一 ...

  8. C++ primer plus读书笔记——第16章 string类和标准模板库

    第16章 string类和标准模板库 1. string容易被忽略的构造函数: string(size_type n, char c)长度为n,每个字母都为c string(const string ...

  9. C++ Primer 读书笔记:第11章 泛型算法

    第11章 泛型算法 1.概述 泛型算法依赖于迭代器,而不是依赖容器,需要指定作用的区间,即[开始,结束),表示的区间,如上所示 此外还需要元素是可比的,如果元素本身是不可比的,那么可以自己定义比较函数 ...

随机推荐

  1. 好大一个坑: EF Core 异步读取大字符串字段比同步慢100多倍

    这两天遇到一个奇怪的问题,通过 EF/EF Core 查询数据库速度奇慢,先是在传统的 ASP.NET 项目中遇到(用的是EF6.0),后来将该项目迁移至 ASP.NET Core 也是同样的问题(用 ...

  2. 保护模式.vbs

    Sub Main Dim cnt Dim delay delay = 10000 For cnt = 0 To 80 crt.screen.Send "interface optical-r ...

  3. 14 - How to check replication status

    The people using PostgreSQL and the Streaming Replication feature seem to ask many of the same quest ...

  4. memcached加固

    Memcached服务安全加固 更新时间:2017-06-30 10:07:49    漏洞描述 Memcached是一套常用的key-value缓存系统,由于它本身没有权限控制模块,所以对公网开放的 ...

  5. ASCII Unicode UTF-8 之间的关系

    转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...

  6. python摸爬滚打之day29----socketserver实现服务端和多个客户端通信

    什么是socketserver? TCP协议下的socket实现了服务端一次只能和一个客户端进行通信, 而socketserver实现了服务端一次能和多个客户端进行通信, 底层调用的还是socket. ...

  7. pymysql连接数据库报错:'NoneType' object has no attribute 'encoding'

    直接写 utf8 即可.

  8. LAYUI select 下拉框得高度

    页面下得select 框 在css页面加样式 .layui-form-select dl {    max-height: 152px;}

  9. Timestamp “时间戳” - 术语

    A timestamp is a sequence of characters or encoded information identifying when a certain event occu ...

  10. LINQ交集/并集/差集/去重

    using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...