C++Primer 习题 第7章
生活随笔
收集整理的這篇文章主要介紹了
C++Primer 习题 第7章
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
C++Primer 習(xí)題 第7章
Person.h#ifndef PERSON_H #define PERSON_H #include<iostream> #include<string> using namespace std;class Person { public://7.15Person() :pname("無姓名"), paddress("無地址"){} Person(string &na, string &ad) :pname(na), paddress(ad){}Person(istream &is) { read(is, *this); }//7.5應(yīng)為const因為成員值未作改變string getname()const { return this->pname; }string getaddress()const { return this->paddress; }//簡單的訪問修改void changename(const string &na) { this->pname = na; }void changename(istream &is) { is >> this->pname; }void changeaddress(const string &ad) { this->paddress = ad; }void changeaddress(istream &is) { is >> this->paddress; }//7.9istream &read(istream &is, Person &per);ostream &print(ostream &os)const;private:string pname;string paddress; };#endifPerson.cpp#include"person.h" istream &Person::read(istream &is, Person &per) {is >> per.pname >> per.paddress;return is; }ostream &Person::print(ostream &os)const {os << "此人姓名:" << this->pname << endl << "此人地址:" << this->paddress << endl;return os; }main.cpp#include"person.h"ostream &print(ostream &os, Person &per) {os << "此人姓名:" << per.getname() << endl<< "此人地址:" << per.getaddress() << endl;return os; }int main() {Person p;p.print(cout);print(cout,p);Person p2(cin);p2.print(cout); } Screen.h#ifndef SCREEN_H #define SCREEN_H #include<iostream> #include<string> using namespace std; class Screen //7.23 { public:using pos = string::size_type;Screen() = default;//7.24Screen(pos h, pos w, char c) :height(h), width(w), contents(h*w, ' '){}Screen(pos h, pos w) :height(h), width(w){}//7.25/*可以依賴默認(rèn)拷貝賦值函數(shù),因為沒有申請堆對象*/ private:pos cursor = 0;pos height = 0;pos width = 0;string contents; };main.cpp#include"Screen.h" int main() {Screen scr; } #endif //7.32 #include<iostream> #include<string> using namespace std; class Window_mgr { public:void clear(); }; class Screen { public:using pos = string::size_type;friend void Window_mgr::clear();Screen() = default; //=delete可以不使用默認(rèn)構(gòu)造函數(shù)Screen(pos h,pos w,char c):height(h),width(w),contents(h*w,c){}Screen(pos h,pos w):Screen(h,w,'?'){}//7.5.2 C++11的****牛逼****之處,使用構(gòu)造初始化另一個構(gòu)造函數(shù)//Screen(pos h, pos w) :height(h), width(w), contents(h*w, 'A') {} //產(chǎn)生二義void changecontents(const string &str) { this->contents = str; }void changecontents(const pos size,const char c=' ') { this->contents = string(size,c); }//這里的const char被初始化了竟然不會出錯哦,可以推測先傳入的是形參,之后是默認(rèn)參數(shù)奧ostream &print(ostream &os = cout) { os << this->contents << endl; return os; }pos getscreensize()const { return this->height * this->width; } private:pos height = 0, width = 0;pos cursor = 0;string contents; }; void Window_mgr::clear() {/*這有啥用不如全局函數(shù)調(diào)用,除了能訪問私有變量之外,本例題感覺并不適合*/ } void clear(Screen &scr) {scr.changecontents(scr.getscreensize(),'#'); } int main() {Screen scr(3, 4);scr.print();clear(scr);scr.print(); }總結(jié)
以上是生活随笔為你收集整理的C++Primer 习题 第7章的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux-Socket实现模拟群聊(多
- 下一篇: c 语言函数公有私有区分,C++私有成员