C++primer 第七章
来源:互联网 发布:网络策划推广公司 编辑:程序博客网 时间:2024/05/21 07:50
本章主要一步步构造类。
7.2
#pragma once#include <iostream>#include <string>using namespace std;struct Sales_data {std::string isbn() const { return bookNo; }Sales_data& combine(const Sales_data&);std::string bookNo;unsigned units_sold = 0;double revenue = 0.0;};Sales_data& Sales_data::combine(const Sales_data &rhs){units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;}
7.4&&7.5
#include <iostream>#include <string>struct Person {std::string get_name() const { return name; }std::string get_adress()const { return adress; }std::string name;std::string adress;};
7.6
#pragma once#ifndef SALES_DATA_H#define SALES_DATA_H#include <iostream>#include <string>using namespace std;struct Sales_data {std::string isbn() const { return bookNo; }Sales_data& combine(const Sales_data&);double avg_price()const;std::string bookNo;unsigned units_sold = 0;double revenue = 0.0;};std::istream& read(istream&, Sales_data&);std::ostream& print(ostream&, const Sales_data&);Sales_data add(const Sales_data&, const Sales_data&);Sales_data& Sales_data::combine(const Sales_data &rhs){units_sold += rhs.units_sold;revenue += rhs.revenue;return *this;}double Sales_data::avg_price() const{if (units_sold) {return revenue / units_sold;}else {return 0;}}istream& read(istream& is, Sales_data& item){double price = 0;is >> item.bookNo >> item.units_sold >> price;item.revenue = item.units_sold*price;return is;}ostream& print(ostream& os, const Sales_data& item){os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();return os;}Sales_data add(const Sales_data& lhs, const Sales_data& rhs){Sales_data sum = lhs;sum.combine(rhs);return sum;}#endif
7.9
#pragma once#ifndef PERSON_H#define PERSON_H#include <iostream>#include <string>using namespace std;struct Person {std::string get_name() const { return name; }std::string get_adress()const { return adress; }std::string name;std::string adress;};std::istream& read(istream&, Person&);std::ostream& print(ostream&, const Person&);istream& read(istream& is, Person& people){is >> people.name >> people.adress;return is;}ostream& print(ostream& os, const Person& people){os << people.get_name() << " " << people.get_adress();return os;}#endif
7.11
#include <iostream>#include <string>#include "Sales_data.h"using namespace std;int main(){Sales_data item1;Sales_data item2("C++ Primer");Sales_data item3("C++ Primer", 10, 128);Sales_data item4(cin);print(cout, item1);cout << endl;print(cout, item2);cout << endl;print(cout, item3);cout << endl;print(cout, item4);cout << endl;}
7.12
Sales_data(std::istream& is){double price = 0;is >> bookNo >> units_sold >> price;revenue = price*units_sold;}
7.15
Person() = default;Person(const string& s) :name(s), adress("Can't get adress") {}Person(const string& s1, const string& s2) :name(s1), adress(s2) {}
7.32
#pragma once#ifndef SCREEN_H#define SCREEN_H#include <iostream>#include <string>#include <vector>using namespace std;class Screen {friend class Windows_mgr;public:typedef std::string::size_type pos;Screen() = default;Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}char get() const { return contents[cursor]; }char get(pos, pos) const;Screen& move(pos, pos);Screen& set(char);Screen& set(pos, pos, char);Screen& display(std::ostream& os) { do_display(os); return *this; }const Screen& display(std::ostream& os) const { do_display(os); return *this; }private:void do_display(std::ostream& os)const { os << contents; }pos cursor = 0;pos height = 0;pos width = 0;std::string contents;};inline char Screen::get(pos r, pos c) const{pos row = r*width;return contents[row + c];}inline Screen& Screen::move(pos r, pos c){pos row = r*width;cursor = row + c;return *this;}inline Screen& Screen::set(char ch){contents[cursor] = ch;return *this;}inline Screen& Screen::set(pos r, pos c, char ch){contents[r*width + c] = ch;return *this;}class Windows_mgr {public:using ScreenIndex = std::vector<Screen>::size_type;void clear(ScreenIndex);private:std::vector<Screen> screens{ Screen(24,80,' ') };};void Windows_mgr::clear(ScreenIndex i){Screen &s = screens[i];s.contents = string(s.height*s.width, ' ');}#endif
0 0
- C++primer 第七章
- c primer plus 第七章
- c primer plus 第七章
- c primer plus 第七章
- c primer plus 第七章
- 第七章 类 c++primer
- C++Primer习题第七章
- c primer plus 第七章 1
- c primer plus 第七章 2
- c primer plus 第七章 3
- C++Primer笔记 第七章 函数
- 《C Plus Primer》第七章编程题
- C++Primer读书笔记(第七章)
- [1]【C++Primer】第七章 函数
- c++primer第七章函数7.31
- C++Primer第七章:类(学习摘录)
- C primer plus第七章习题
- c++primer第五版第七章练习
- cocoapods的更新常见错误
- 解析Tensorflow官方PTB模型的demo
- HBase表预分区
- 带三角指示器的ProgressBar
- Android —— Android版本升级
- C++primer 第七章
- 使用Spring实现读写分离( MySQL实现主从复制)
- GPU处理图像 Shader的入门
- Android网络请求库android-async-http使用
- SpringMVC学习2
- 笔试题:字符串去除空格
- ftp远程下载文件一、远程文件下载
- 删除word2010中的一个横线
- 封装添加事件监听程序