程序清单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的更多相关文章

  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. iOS代码组件化--利用cocoaPods创建私有库

    如果项目模块多,模块间逻辑复杂,我们发现多个人同时维护一个代码仓库需要十分小心,一不小心,造成冲突,解决起来很烦,相信很多人都遇到手工删除合并的冲突的文件的经历. 如果利用组件化思想,每个人维护自己的 ...

  3. Centos 安装Java jdk

    1/ yum search java|grep jdk 2/ yum install java-......... 3/ vi /etc/profile 在最后添上: 4/ source /etc/p ...

  4. 007-chrome插件系列

    1.Axure RP Extension for Chrome 2.Charset 3.CLEAN crxMouse Gestures 4.Google 翻译 5.JSONView 6.restlet

  5. 深浅copy

    浅拷贝 只copy了一层 可哈希的变量改变则二者不同   不可哈希变量改变二者都改变深拷贝  全部都copy了 不论什么值改变 原值都改变呢 变量赋值.浅拷贝.深拷贝 1,创建变量 1.1不可变对象 ...

  6. Python selenium中注入并执行Javascript语句

    众所周知,Python通常结合selenium模块来完成一些web的自动化测试以及RPA(Robotic Process Automation)工作.事实上,Selenium还可以支持插入js语句.执 ...

  7. 获取地址栏url

    Url=${window.location.protocol}//${window.location.host}${window.location.pathname}

  8. 黑苹果,Win7,Win10,Xp 各个系统镜像文件下载地址(备用)

    windows Mac Xp(系统镜像下载装系统专区)百度系统世家也可 http://www.xp933.com/download/ 黑苹果系统(各种驱动型号下载专区) http://www.it36 ...

  9. 关于Oracle的44951事件

    最近有同事遇到某客户数据库产生大量阻塞,等待事件为:enq HW - contention,最开始采用不断杀会话的方式,效果不好,问题一直高频反复.进一步确认SQL是大量的insert,且插入的表中含 ...

  10. Vue一、起步

    1.参考资料-官网 https://cn.vuejs.org/v2/guide/ 2.介绍 Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架 与其它大型框架不同,V ...