C++ Primer(第五版)第七章 类 部分答案
生活随笔
收集整理的這篇文章主要介紹了
C++ Primer(第五版)第七章 类 部分答案
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
第七章 類
- 練習7.2
- 練習7.3
- 練習7.4
- 練習7.6
- 練習7.7
- 練習7.9
- 練習7.14、7.15、7.22
- 練習7.23、7.24、7.26
- 練習7.27
練習7.2
曾在 2.6.2 節的練習(第 76 頁)中編寫了一個 Sales_data類,請向這個類添加 combine 和 isbn 成員。
創建頭文件sales.h
#ifndef SALES_H #define SALES_H#include <string> #include <stdlib.h> using namespace std;struct Sales_data {string isbn() const //返回書的編號{return bookNo;}Sales_data& combine(const Sales_data& rhs);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.units_sold;return *this; } #endif;練習7.3
修改 7.1.1 節(第 229 頁)的交易處理程序,令其使用這些成員。
#include <iostream> using namespace std; #include "sales.h" #include <string>//讀取銷售記錄,生成每本書的銷售報告,顯示售出冊數、總銷售額和平均售價 int main() {Sales_data total; //保存當前求和結果的變量if (cin >> total.bookNo >> total.units_sold >> total.revenue){Sales_data trans;while (cin >> trans.bookNo >> trans.units_sold >> trans.revenue){if (total.isbn() == trans.isbn())total.combine(trans);else{cout << total.bookNo << " 的總銷售記錄是:共售出 " << total.units_sold << " 本,總收入是 " << total.revenue;total = trans;}}cout << total.bookNo << " 的總銷售記錄是:共售出 " << total.units_sold << " 本,總收入是 " << total.revenue << " 平均每本價格是 "<<total.revenue/total.units_sold;} }練習7.4
編寫一個名為 Person 的類,使其表示人員的姓名和住址。使用 string 對象存放這些元素,接下來的練習將不斷充實這個類的其他特征。
創建頭文件person.h
#pragma once #include<string> using namespace std; struct Person {string Name;string Address; };練習7.6
對于函數 add、read、和 print,定義你自己的版本。
創建頭文件sales.h
#ifndef SALES_H #define SALES_H#include <string> #include <stdlib.h> using namespace std;struct Sales_data {string isbn() const{return bookNo;}Sales_data& combine(const Sales_data& rhs);istream& read(istream& is, Sales_data& item);ostream& print(ostream& os, Sales_data& item);Sales_data add(const Sales_data& lhs, const Sales_data& rhs);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.units_sold;return *this; } istream& Sales_data::read(istream& is, Sales_data& item) //is用來代替cin,istream類和ostream類見后續的IO類型 {double price;is >> item.bookNo >> item.units_sold >> price;item.revenue = price * item.units_sold;return is; } ostream& Sales_data::print(ostream& os, Sales_data& item) {os << item.isbn() << " 的總銷售記錄是:共售出 " << item.units_sold << " 本,總收入是 "<< item.revenue << " " << " 平均每本價格是 "<<item.revenue/item.units_sold;return os; } Sales_data Sales_data::add(const Sales_data& lhs, const Sales_data& rhs) {Sales_data sum = lhs; //把lhs的數據成員拷貝給sumsum.combine(rhs); //把rhs的數據成員加到sum中return sum; }#endif;練習7.7
使用這些新函數重寫 7.1.2 節(第 233 頁)練習中的交易處理程序。
#include <iostream> using namespace std; #include "sales.h" #include <string>//讀取銷售記錄,生成每本書的銷售報告,顯示售出冊數、總銷售額和平均售價 int main() {Sales_data total; if (total.read(cin,total)){Sales_data trans;while (total.read(cin,trans)){if (total.isbn() == trans.isbn())total.combine(trans);else{total.print(cout,total);cout << endl;total = trans;}}total.print(cout, total);cout << endl;} }練習7.9
對于 7.1.2節(第 233 頁)練習中的代碼,添加讀取和打印 Person 對象的操作。
#pragma once #include <string> #include <iostream> using namespace std;struct Person {string Name;string Address;istream& read(istream& is, Person& per);ostream& print(ostream& os, const Person& per);}; istream& Person::read(istream& is, Person& per) {is >> per.Name >> per.Address;return is; }ostream& Person::print(ostream& os, const Person& per) {os << "名字為:" << per.Name << " 地址為:" << per.Address;return os; }練習7.14、7.15、7.22
7.14.編寫一個構造函數,令其用我們提供的類內初始值顯式地初始化成員。
7.15.為你的 Person 類添加正確的構造函數。
7.22.修改你的 Person 類使其隱藏實現的細節。
頭文件
#pragma once #include <string> #include <iostream> using namespace std; '''構造函數可以有多個,發生了重載,實例化的時候只能調用一個''' class Person { public://1、無參構造函數,可以直接Person p創建實例Person() = default; //2、無參顯示構造,可以直接Person p創建實例,和默認無參構造無法發生重載Person():Name("megumi"),Address("japan"){} //3、有參構造函數,需要Person p("sanae","erci")創建實例Person(string N, string A) :Name(N), Address(A) {} //4、類外構造函數的聲明Person(istream& is);istream& read(istream& is, Person& per); //讀取函數ostream& print(ostream& os, const Person& per); //輸出函數 private:string Name;string Address; };//類外構造函數的定義 Person::Person(istream &is) {read(is,*this); }istream& Person::read(istream& is, Person& per) {is >> per.Name >> per.Address;return is; }ostream& Person::print(ostream& os, const Person& per) {os << "名字為:" << per.Name << " 地址為:" << per.Address;return os; }主程序
#include <iostream> using namespace std; #include "person.h" int main() {//1和2采用這種實例化Person per;per.print(cout, per);cout<<endl;//3采用這種實例化Person per1("sanae","beijing");per1.print(cout,per1);cout<<endl;//4類外構造函數的使用Person per2(cin);per2.print(cout,per2);}練習7.23、7.24、7.26
頭文件screen.h
#pragma once #include <string> #include <iostream> using namespace std;class Screen { public:typedef string::size_type pos;Screen() = default;Screen(pos ht, pos wd) :height(ht), width(wd) { }Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c) { }char get() const //讀取光標處的字符{return contents[cursor]; //隱式內聯}inline char get(pos ht, pos wd) const; //顯示內聯Screen& move(pos r, pos c); //能在之后被設為內聯private:pos cursor = 0;pos height = 0, width = 0;string contents; }; 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; }練習7.27
頭文件screen.h
#pragma once #include <string> #include <iostream> using namespace std;class Screen { public:typedef string::size_type pos;Screen() = default;Screen(pos ht, pos wd) :height(ht), width(wd) { }Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c) { }char get() const //讀取光標處的字符{return contents[cursor]; //隱式內聯}inline char get(pos ht, pos wd) const; //顯示內聯Screen& move(pos r, pos c); //負責移動光標,能在之后被設為內聯Screen& set(char);Screen& set(pos r, pos col, char ch);Screen& display(ostream& os){do_display(os);return *this;}const Screen& display(ostream& os) const{do_display(os);return *this;}private:pos cursor = 0; //光標的位置pos height = 0, width = 0; //屏幕的寬高string contents; //存放數據//該函數負責顯示Screen的內容void do_display(ostream& os) const{os << contents;} }; 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 c) {contents[cursor] = c; //設置當前光標所在位置的新值return *this; //將this對象作為左值返回 } inline Screen& Screen::set(pos r, pos col, char ch) {contents[r * width + col] = ch; //設置給定位置的新值return *this; //將this對象作為左值返回 }主程序
#include "screen.h"int main() {Screen myScreen(5, 5, 'x'); //創建一個有5*5個x的對象myScreen.move(4, 0).set('#').display(cout);//移動光標4*5+0下,把那個位置的x替換成#,并且顯示出來cout << '\n';myScreen.display(cout);cout << '\n'; }結果為:
xxxxxxxxxxxxxxxxxxxx#xxxx xxxxxxxxxxxxxxxxxxxx#xxxx總結
以上是生活随笔為你收集整理的C++ Primer(第五版)第七章 类 部分答案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 路由器通过有线如何连接ap路由器有线ap
- 下一篇: ubuntu下vscode使用cmake