c++ prime plus 第十一章 第6题
来源:互联网 发布:网络爬虫 java 对比 编辑:程序博客网 时间:2024/05/19 18:10
前几天开始上课,偶尔翻了一下c++第十一章。主要讲了重载,友元,类型转换。。。
跳过了随机函数的那一小块。现在,基本上结合书可以写出这一章的内容,实现重载,友元,类型转换等。还不是很熟练,可以以后多练练书后面的习题。
//stonewt.h
#ifndef STONEWT_H_INCLUDED#define STONEWT_H_INCLUDED#include <iostream>//没有用状态成员mode//没有过多的使用转换函数using namespace std;class Stonewt{private: enum {Lbs_per_stn = 14}; int stone; double pds_left; //几英石几英磅 double pounds; //英磅public: Stonewt(double pou); Stonewt(int sto,double pds); Stonewt(); ~Stonewt();//operater overloading Stonewt operator+(const Stonewt & b) const; Stonewt operator-(const Stonewt & b) const; Stonewt operator*(double n) const; bool operator<(const Stonewt& b) const; bool operator<=(const Stonewt& b) const; bool operator==(const Stonewt& b) const;//返回类型//friends friend Stonewt operator*(double n,const Stonewt & b); friend ostream & operator<<(ostream & os, Stonewt & st); friend bool operator>(Stonewt& a, Stonewt& b); friend bool operator>=(Stonewt& a, Stonewt& b); friend bool operator!=(Stonewt& a, Stonewt& b);};#endif // STONEWT_H_INCLUDED
//stonewt
#include "stonewt.h"Stonewt::Stonewt(double pou){ stone = int(pou) / Lbs_per_stn; pds_left = int(pou) % Lbs_per_stn + pou - int(pou); pounds = pou;}Stonewt::Stonewt(int sto,double pds){ stone = sto; pds_left = pds; pounds = sto * Lbs_per_stn + pds;}Stonewt::Stonewt(){ stone = pounds = pds_left = 0;}Stonewt::~Stonewt(){}//operater overloadingStonewt Stonewt::operator+(const Stonewt & b) const{ return Stonewt(pounds+b.pounds);}Stonewt Stonewt::operator-(const Stonewt & b) const{ return Stonewt(pounds-b.pounds);}Stonewt Stonewt::operator*(double n) const{ return Stonewt(pounds*n);}//第一次尝试重载<,==等关系运算符bool Stonewt::operator<(const Stonewt& b) const{ if(pounds<b.pounds) return 1; else return 0;}bool Stonewt::operator<=(const Stonewt& b) const{ return pounds <= b.pounds;}bool Stonewt::operator==(const Stonewt& b) const{ return pounds == b.pounds;}//friendsbool operator>(Stonewt& a, Stonewt& b){ return a.pounds > b.pounds;}bool operator>=(Stonewt& a, Stonewt& b){ return a.pounds >= b.pounds;}bool operator!=(Stonewt& a, Stonewt& b){ return a.pounds != b.pounds;}Stonewt operator*(double n,const Stonewt & b){ return b*n;}ostream& operator<<(ostream& os, Stonewt & st){ os << st.pounds << " pounds,or " << st.stone << " stone and " << st.pds_left << " pounds." << std::endl; return os;}//c++pp11.9.6.cpp
#include <iostream>#include "stonewt.h"#include <cstdlib>using namespace std;int main(int argc, const char * argv[]) //?{ Stonewt a(12); a = 154;//转换函数 cout << a; int num=1; Stonewt b[7] = { 123, 23.32, 3.3 }; b[3]= b[1] + b[2]; b[4]= 10 * b[3]; for (int i = 5; i < 7; i++) { b[i] = rand();//随机数我还不是很明白 } cout << endl; cout << "7个Stonewt的输出:" << endl; for (int i = 0; i < 7; i++) { cout << b[i]; } cout << endl; Stonewt min = b[0]; Stonewt max = b[0]; for (int i = 0; i < 7; i ++) { if (min > b[i]) { min = b[i]; } if (max < b[i]) { max = b[i]; } if (b[i] >= a) { num++ ; } } cout << "Min: " << min ; cout << "Max: " << max ; cout << "bigger than 11: " << num << "个。" << endl; return 0;}
阅读全文
1 0
- c++ prime plus 第十一章 第6题
- C++prime plus第6版第三章要点
- c++Prime Plus 第六版第18章复习题第7题答案有误..
- c prime plus第八章
- c prime plus 第十二章
- C Prime Plus第十三章
- C Primer Plus 第十一章 编程练习 1-15题
- C++Primer Plus(第六版) 第十一章 第七题
- 记录——《C Primer Plus (第五版)》第十一章编程练习第5-12题
- c primer plus第十一章课后习题
- C primer plus 第十一章 练习1:
- C primer plus 第十一章 练习2:
- C primer plus 第十一章 练习3:
- c++primer plus第十一章答案
- c primer plus 第十一章学习小结
- c prime plus (第十七章 高级数据结构)
- C Prime plus 第六章习题
- c prime plus第十四章练习题
- Spring里PropertyPlaceholderConfigurer类的使用
- 六.SpringBoot集成实例系列-单数据源mongodb(权限控制)
- Redis安装和配置教程
- 前端入门练手项目
- Swift开发必备技巧:内存管理、weak和unowned
- c++ prime plus 第十一章 第6题
- 光场数据库mark
- Service的一点理解
- Java面试题全集(上)
- 每日一贴--2017/9/13
- JSTL
- 2017 ICPC 沈阳赛 E。Number
- svn搭建
- 解读tensorflow之rnn 的示例 ptb_word_lm.py