c++ primer第五版 练习7.23
练习 7.23:编写你自己的Screen类
//screen.h
#ifndef SCREEN_H #define SCREEN_H #include <string> #include <iostream> class Screen { public: typedef std::string::size_type pos; Screen()=default; Screen(pos ht,pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}//就是定义ht*wd范围内的整个C; char get() const { return contents[cursor]; } char get(pos row,pos col) const;//means row and col; Screen &move(pos row,pos col); Screen &set(char); Screen &set(pos r,pos c,char); std::ostream &do_display(std::ostream &os) const { os<<contents; return os; } private: pos cursor=0; pos height=0,width=0; std::string contents; }; #endif // SCREEN_H
//screen.cpp
#include "screen.h" Screen &Screen::move(pos r, pos c) { pos row=r*width; cursor=row+c; return *this; } char Screen::get(pos r, pos c) const { pos row=r*width; return contents[row+c]; } Screen &Screen::set(char c) { contents[cursor]=c; return *this; } Screen &Screen::set(pos r, pos c, char ch) { contents[r*width+c]=ch; return *this; }
本文出自 “奔跑的驴” 博客,请务必保留此出处http://amgodchan.blog.51cto.com/9521586/1574805
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。