[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 = );
- 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");
- }
未完……
[C++ Primer Plus] 第11章、使用类(一)程序清单——重载 P408的更多相关文章
- C++ primer plus读书笔记——第11章 使用类
第11章 使用类 1. 运算符重载是一种形式的C++多态. 2. 不要返回指向局部变量或临时对象的引用.函数执行完毕后,局部变量和临时对象将消失,引用将指向不存在的数据. 3. 运算符重载的格式如下: ...
- C++ Primer:第七章:类
定义一个类: class Myclass{ int data_i; string data_str; public: int getdata_i() const { return data_i; } ...
- C++ Primer 5th 第11章 关联容器
练习11.1:描述map 和 vector 的不同. map是关联容器,vector是顺序容器,关联容器与值无关,vector则与值密切相关 练习11.2:分别给出最适合使用 list.vector. ...
- Prism 4 文档 ---第11章 部署Prism应用程序
要成功移动Prism应用到生产中,需要对部署计划为应用程序的设计过程的一部分.本章介绍了注意事项和你需要采取的准备以部署应用程序,以及你要在用户手中获得部署程序所需要采取的行动. Si ...
- 《C++ Primer Plus》第11章 使用类 学习笔记
本章介绍了定义和使用类的许多重要方面.一般来说,访问私有类成员的唯一方法是使用类方法.C++使用友元函数来避开这种限制.要让函数称为友元,需要在类声明中声明该函数,并在声明前加上关键字friend.C ...
- 《C++ primer》--第11章
习题11.1 algorithm头文件定义了一个count的函数,其功能类似于find.这个函数使用一对迭代器和一个值做参数,返回这个值出现次数的统计结果.编写程序读取一系列int型数据,并将它们存储 ...
- 【WPF学习】第二十六章 Application类——应用程序的生命周期
在WPF中,应用程序会经历简单的生命周期.在应用程序启动后,将立即创建应用程序对象,在应用程序运行时触发各种应用程序事件,你可以选择监视其中的某些事件.最后,当释放应用程序对象时,应用程序将结束. 一 ...
- C++ primer plus读书笔记——第16章 string类和标准模板库
第16章 string类和标准模板库 1. string容易被忽略的构造函数: string(size_type n, char c)长度为n,每个字母都为c string(const string ...
- C++ Primer 读书笔记:第11章 泛型算法
第11章 泛型算法 1.概述 泛型算法依赖于迭代器,而不是依赖容器,需要指定作用的区间,即[开始,结束),表示的区间,如上所示 此外还需要元素是可比的,如果元素本身是不可比的,那么可以自己定义比较函数 ...
随机推荐
- 好大一个坑: EF Core 异步读取大字符串字段比同步慢100多倍
这两天遇到一个奇怪的问题,通过 EF/EF Core 查询数据库速度奇慢,先是在传统的 ASP.NET 项目中遇到(用的是EF6.0),后来将该项目迁移至 ASP.NET Core 也是同样的问题(用 ...
- 保护模式.vbs
Sub Main Dim cnt Dim delay delay = 10000 For cnt = 0 To 80 crt.screen.Send "interface optical-r ...
- 14 - How to check replication status
The people using PostgreSQL and the Streaming Replication feature seem to ask many of the same quest ...
- memcached加固
Memcached服务安全加固 更新时间:2017-06-30 10:07:49 漏洞描述 Memcached是一套常用的key-value缓存系统,由于它本身没有权限控制模块,所以对公网开放的 ...
- ASCII Unicode UTF-8 之间的关系
转载请标明:https://i.cnblogs.com/EditPosts.aspx?opt=1 1. ASCII ASCII 只有127个字符,表示英文字母的大小写.数字和一些符号,但由于其他语言用 ...
- python摸爬滚打之day29----socketserver实现服务端和多个客户端通信
什么是socketserver? TCP协议下的socket实现了服务端一次只能和一个客户端进行通信, 而socketserver实现了服务端一次能和多个客户端进行通信, 底层调用的还是socket. ...
- pymysql连接数据库报错:'NoneType' object has no attribute 'encoding'
直接写 utf8 即可.
- LAYUI select 下拉框得高度
页面下得select 框 在css页面加样式 .layui-form-select dl { max-height: 152px;}
- Timestamp “时间戳” - 术语
A timestamp is a sequence of characters or encoded information identifying when a certain event occu ...
- LINQ交集/并集/差集/去重
using System.Linq; List<string> ListA = new List<string>(); List<string> ListB = n ...