码迷,mamicode.com
首页 > 编程语言 > 详细

c++ primer第五版 练习7.23

时间:2014-11-10 01:15:43      阅读:268      评论:0      收藏:0      [点我收藏+]

标签: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

c++ primer第五版 练习7.23

标签:contents

原文地址:http://amgodchan.blog.51cto.com/9521586/1574805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!