标签:contents
练习 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
标签:contents
原文地址:http://amgodchan.blog.51cto.com/9521586/1574805