第11章 使用类

  • 11.1 mytime0.h
  • 11.2 mytime0.cpp
  • 11.3 usetime0.cpp
  • 11.4 mytime1.h
  • 11.5 mytime1.cpp
  • 11.6 usetime1.cpp
  • 11.7 mytime2.h
  • 11.8 mytime2.cpp
  • 11.9 usetime2.cpp
  • 11.10 mytime3.h
  • 11.11 mytime3.cpp
  • 11.12 usetime3.cpp
  • 11.13 vector.h
  • 11.14 vector.cpp
  • 11.15 randwalk.cpp
  • 11.16 stonewt.h
  • 11.17 stonewt.cpp
  • 11.18 stone.cpp
  • 11.19 stonewt1.h
  • 11.20 stonewt1.cpp
  • 11.21 stone1.cpp

11.1 mytime0.h

#ifndef MYTIME0_H_
#define MYTIME0_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 Sum(const Time& t)const;void Show() const;
};#endif

11.2 mytime0.cpp

#include<iostream>
#include"mytime0.h"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::Sum(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
{std::cout << hours << " hours, " << minutes << " minutes";
}

11.3 usetime0.cpp

#include<iostream>
#include"mytime0.h"int main()
{using std::cout;using std::endl;Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;cout << "planning time = ";planning.Show();cout << endl;cout << "coding time = ";coding.Show();cout << endl;cout << "fixing time = ";fixing.Show();cout << endl;total = coding.Sum(fixing);cout << "coding.Sum(fixing) = ";total.Show();cout << endl;return 0;
}

11.4 mytime1.h

#ifndef MYTIME1_H_
#define MYTIME1_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;void Show() const;
};#endif

11.5 mytime1.cpp

#include<iostream>
#include"mytime1.h"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
{std::cout << hours << " hours, " << minutes << " minutes";
}

11.6 usetime1.cpp

#include<iostream>
#include"mytime1.h"int main()
{using std::cout;using std::endl;Time planning;Time coding(2, 40);Time fixing(5, 55);Time total;cout << "planning time = ";planning.Show();cout << endl;cout << "coding time = ";coding.Show();cout << endl;cout << "fixing time = ";fixing.Show();cout << endl;total = coding + fixing;cout << "coding + fixing = ";total.Show();cout << endl;Time morefixing(3, 28);cout << "more fixing time = ";morefixing.Show();cout << endl;total = morefixing.operator+(total);cout << "morefixing.operator+(total) = ";total.Show();cout << endl;return 0;
}

11.7 mytime2.h

#ifndef MYTIME2_H_
#define MYTIME2_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 operator-(const Time& t)const;Time operator*(double n) const;void Show() const;
};#endif

11.8 mytime2.cpp

#include<iostream>
#include"mytime2.h"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;
}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 mult)const
{Time result;long totalminutes = hours * mult * 60 + minutes * mult;result.hours = totalminutes / 60;result.minutes = totalminutes % 60;return result;
}void Time::Show() const
{std::cout << hours << " hours, " << minutes << " minutes";
}

11.9 usetime2.cpp

#define _CRT_SECURE_NO_WARNINGS 1#include<iostream>
#include"mytime2.h"int main()
{using std::cout;using std::endl;Time weeding(4, 35);Time waxing(2, 47);Time total;Time diff;Time adjusted;cout << "weeding time = ";weeding.Show();cout << endl;cout << "waxing time = ";waxing.Show();cout << endl;cout << "total work time = ";total = weeding + waxing;total.Show();cout << endl;diff = weeding - waxing;cout << "weeding time - waxing time = ";diff.Show();cout << endl;adjusted = total * 1.5;cout << "adjusted work time = ";adjusted.Show();cout << endl;return 0;
}

11.10 mytime3.h

#ifndef MYTIME1_H_
#define MYTIME1_H_
#include <iostream>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 operator-(const Time& t) const;Time operator*(double n) const;friend Time operator*(double m, const Time& t) { return t * m; }friend std::ostream& operator<<(std::ostream& os, const Time& t);
};
#endif 

11.11 mytime3.cpp

#include "mytime3.h"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;
}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 mult) const
{Time result;long totalminutes = hours * mult * 60 + minutes * mult;result.hours = totalminutes / 60;result.minutes = totalminutes % 60;return result;
}std::ostream& operator<<(std::ostream& os, const Time& t)
{os << t.hours << " hours, " << t.minutes << " minutes";return os;
}

11.12 usetime3.cpp

#include <iostream>
#include "mytime3.h"int main()
{using std::cout;using std::endl;Time aida(3, 35);Time tosca(2, 48);Time temp;cout << "Aida and Tosca:\n";cout << aida << "; " << tosca << endl;temp = aida + tosca;cout << "Aida + Tosca: " << temp << endl;temp = aida * 1.17;cout << "Aida * 1.17: " << temp << endl;cout << "10.0 * Tosca: " << 10.0 * tosca << endl;return 0;
}

11.13 vector.h

#define _CRT_SECURE_NO_WARNINGS 1
#ifndef VECTOR_H_
#define VECTOR_H_#include <iostream>
using namespace std;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();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

11.14 vector.cpp

#include <cmath>
#include "vector.h"
using std::sqrt;
using std::sin;
using std::cos;
using std::atan;
using std::atan2;
using std::cout;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;elseang = atan2(y, x);}void Vector::set_x(){x = mag * cos(ang);}void Vector::set_y(){y = mag * sin(ang);}//public methodsVector::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() -- ";cout << "vector set to 0\n";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() --";cout << "vector set to 0\n";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;}std::ostream& operator<<(std::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 << ")";}elseos << "Vector object mode is invalid";return os;}
}

11.15 randwalk.cpp

#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector.h"int main()
{using namespace std;using VECTOR::Vector;srand(time(0));double direction;Vector step;Vector result(0.0, 0.0);unsigned long steps = 0;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() % 360;step.reset(dstep, direction, Vector::POL);result = result + step;steps++;}cout << "After " << steps << " steps, the subject "<< "has the following location : \n";cout << result << endl;result.polar_mode();cout << " or\n" << result << endl;cout << "Average outward distance per step = "<< result.magval() / steps << endl;steps = 0;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");return 0;
}

11.16 stonewt.h

#ifndef STONEWT_H_
#define STONEWT_H_class Stonewt
{private:enum{Lbs_per_stn  = 14};int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void show_lbs() const;void show_stn() const;
};#endif

11.17 stonewt.cpp

#include <iostream>
#include "stonewt.h"
using std::cout;Stonewt::Stonewt(double lbs)
{stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;
}Stonewt::Stonewt(int stn, double lbs)
{stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn;
}Stonewt::Stonewt()
{stone = pounds = pds_left = 0;
}Stonewt::~Stonewt()
{}void Stonewt::show_stn() const
{cout << stone << " stone, " << pds_left << " pounds\n";
}void Stonewt::show_lbs() const
{cout << pounds << " pounds\n";
}

11.18 stone.cpp

#include <iostream>
using std::cout;
#include "stonewt.h"void display(const Stonewt& st, int n);int main()
{Stonewt incognito = 275;Stonewt wolfe(285.7);Stonewt taft(21, 8);cout << "The calebrity weighed ";incognito.show_stn();cout << "The detective weighed ";wolfe.show_stn();cout << "The President weighed ";taft.show_lbs();incognito = 276.8;taft = 325;cout << "After dinner, the celebrity weighed ";incognito.show_stn();cout << "After dinner, the President weighed ";taft.show_lbs();display(taft, 2);cout << "The wrestler weighed even more.\n";display(422, 2);cout << "No stone left unearned\n";return 0;
}void display(const Stonewt& st, int n)
{for (int i = 0; i < n; i++){cout << "Wow! ";st.show_stn();}
}

11.19 stonewt1.h

#ifndef STONEWT_H_
#define STONEWT_H_class Stonewt
{private:enum { Lbs_per_stn = 14 };int stone;double pds_left;double pounds;public:Stonewt(double lbs);Stonewt(int stn, double lbs);Stonewt();~Stonewt();void show_lbs() const;void show_stn() const;operator int() const;operator double() const;
};#endif

11.20 stonewt1.cpp

#include <iostream>
using std::cout;
#include "stonewt1.h"Stonewt::Stonewt(double lbs)
{stone = int(lbs) / Lbs_per_stn;pds_left = int(lbs) % Lbs_per_stn + lbs - int(lbs);pounds = lbs;
}Stonewt::Stonewt(int stn, double lbs)
{stone = stn;pds_left = lbs;pounds = stn * Lbs_per_stn + lbs;
}Stonewt::Stonewt()
{stone = pounds = pds_left = 0;
}Stonewt::~Stonewt()
{}void Stonewt::show_stn() const
{cout << stone << " stone, " << pds_left << " pounds\n";
}void Stonewt::show_lbs() const
{cout << pounds << " pounds\n";
}Stonewt::operator int() const
{return int(pounds + 0.5);
}Stonewt::operator double() const
{return pounds;
}

11.21 stone1.cpp

#include <iostream>
#include "stonewt1.h"int main()
{using std::cout;Stonewt poppins(9, 2.8);double p_wt = poppins;cout << "Convert to double => ";cout << "Poppins: " << p_wt << " pounds.\n";cout << "Convert to int => ";cout << "Poppins: " << int(poppins) << " pounds.\n";return 0;
}

C++primer Plus课本代码(第11章)相关推荐

  1. 【NoSQL数据库技术与应用】【课本代码】【课后题答案】【持续更新】

    文章目录 一.课本代码 第1章 初识NoSQL 第2章 文档存储数据库MongoDB 第3章 MongoDB数据库操作 3.8 使用Java操作MongoDB 1.搭建JAVA环境 (1)Java配置 ...

  2. c++primer plus 第11章 编程题第7题

    c++primer plus 第11章 编程题第7题 #pragma once #ifndef COMPLEX0_H_ #define COMPLEX0_H_ #include<iostream ...

  3. 第一行代码 第三版 第11章网络技术 11.6.1 Retrofit 应用 报错:android.system.ErrnoException: isConnected failed: ECONNRE

    在学习第11章 11.6.1Retrofit用法,这节的时候发生的报错:书上关于这个地方并没有说. 我搜索问题的关键语句是: java.net.ConnectException: Failed to ...

  4. 《代码大全2》第11章 变量名的力量

    目录 前言 11.1 选择好变量名的注意事项 11.1.1 最重要的命名注意事项 11.1.2 最适当的变量名字 11.1.3 变量名中的计算值限定词 11.1.4 变量名中常用的对仗词 11.2 为 ...

  5. C++ Primer 第五版 第6章——函数阅读笔记及习题答案(完整,附C++代码)

    C++Primer(第五版)第6章函数的阅读笔记及课后习题答案总结,课后习题答案是自己学习写出来的,如果有误,欢迎指正 还不完整,后续会更新添加 阅读笔记 C++ Primer 第五版 第6章 6.1 ...

  6. C++ Primer 学习笔记(第四章:表达式)

    2019独角兽企业重金招聘Python工程师标准>>> ##C++ Primer 学习笔记(第四章:表达式) [TOC] ###4.1 基础 左值和右值: 当一个对象被用作右值的时候 ...

  7. C++ Primer Plus 6th代码阅读笔记

    C++ Primer Plus 6th代码阅读笔记 第一章没什么代码 第二章代码 carrots.cpp : cout 可以拼接输出,cin.get()接受输入 convert.cpp 函数原型放在主 ...

  8. 《Learning Scrapy》(中文版)第11章 Scrapyd分布式抓取和实时分析

    序言 第1章 Scrapy介绍 第2章 理解HTML和XPath 第3章 爬虫基础 第4章 从Scrapy到移动应用 第5章 快速构建爬虫 第6章 Scrapinghub部署 第7章 配置和管理 第8 ...

  9. python编程从入门到实践课后题答案-《Python编程:从入门到实践》课后习题及答案—第11章...

    第11章 测试代码 11-1 城市和国家 :编写一个函数,它接受两个形参:一个城市名和一个国家名.这个函数返回一个格式为City, Country 的字符串,如Santiago, Chile .将这个 ...

  10. python封装类在当前文件中使用_name_下调用不了_学python中对于类怎么也不明白,对了是看到简明教程11章,类与变量的对象....想要通俗易懂的答案....

    代码(简明python教程第11章,感觉看懂了大半但是其中我没调用的为什么也有...谢谢!!!):#coding:utf-8#数据结构好抽象啊..#python的空格制表位,是灾难的开始啊#init制 ...

最新文章

  1. 【Android 逆向】Android 进程代码注入原理 ( 注入本质 | 静态注入和动态注入 | 静态注入两种方式 | 修改动态库重打包 | 修改 /data/app/xx/libs 动态库 )
  2. 中国大学生源质量排行榜150强
  3. linux下svn迁移
  4. PAT1045 快速排序 (25 分)【4/6通过】
  5. mysql存储过程while 遍历游标
  6. 用Java编写模仿的太阳系(九星行旋转)--原创
  7. 2019年Java编程开发值得学习的10大技术
  8. ad17 pcb扇孔_PCB设计中为什么需要先进行扇孔
  9. 怎么使用php连接mysql_如何使用PHP连接MySQL
  10. html中输出 u263c,二级C语言笔试必过399题
  11. 使用 JQueryElement ResponseProgress 显示页面执行进度
  12. SQLite学习手册(索引和数据分析/清理)-转
  13. idea 构建java 微服务_使用 IDEA 从 0 开始搭建 Spring Cloud 微服务
  14. Stack Overflow监控系统内部架构初探
  15. 工作流框架的设计要点
  16. 拼多多优惠券赔付规则 拼多多发货超时具体怎么赔 拼多多超时发货之后没有优惠券怎么办
  17. U盘蠕虫病毒解决办法
  18. Unity使用c#开发遇上的问题(六)(3dmax围绕指定中心旋转,unity中动态调用预制体并根据模型旋转指定角度)
  19. ELv2是一种什么样的存在?StarRocks为何惹众怒?
  20. Centos7安装trac手册

热门文章

  1. 文件包含漏洞防范措施
  2. 学java用不用学ps_【No935】零基础学习从入门到精通Ps课程
  3. 大数据处理需要用到的九种编程语言
  4. 商标注册成功后的中肯建议
  5. linux上传文件到百度云盘(使用shell脚本,不依赖python库)
  6. mysql8+maven+mybatis
  7. mysql relay log.info_技术分享 | slave_relay_log_info 表认知的一些展开
  8. Newton法(牛顿法 Newton Method)
  9. 教育问题案例研究(张奎明)
  10. 工作,究竟意味着什么