C++Primer第五版 7.3.4节练习

练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。
答:见云盘程序 练习7.32.cpp
练习7.32

/*
*练习7.32 
*2015/7/13
*问题描述:练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。 
*说明:仅说明写的程序用 ,写得不好 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
* 
*/
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Window_mgr{
public:
    void clear();
}; 

void Window_mgr::clear(){
    cout << "clearing..." << endl; 

}
class Screen{
public:
    friend class Window_mgr;
    friend void Window_mgr::clear();

};

int main()
{
    Window_mgr a;
    a.clear();

    return 0;

}

练习7.32-1

/*
*练习7.32
*2015/7/14
*问题描述:练习7.32:定义你自己的Screen和Window_mgr,其中clear是window_mgr的成员,是Screen的友元。
*功能:做了点改进,便于理解。 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
* 
*/

#include <iostream> 
#include <string>
#include <vector> 
using namespace std;


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){}
    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, pos, char);
    Screen &display(std::ostream &os)
            {
                do_display(os);
                return *this;
            }
    const Screen &display(std::ostream &os) const
            {
                do_display(os);
                return *this;
            }
        //extern void Window_mgr::clear(ScreenIndex);   
    //friend void Window_mgr::clear(ScreenIndex);

    //friend void Window_mgr::clear(ScreenIndex);
    //extern class Window_mgr;
    friend class Window_mgr;
    //extern void Window_mgr::clear(ScreenIndex);
private:
    pos cursor = 0;
    pos height = 0, width = 0;
    std::string contents;
    void do_display(std::ostream &os) const { os << contents;}
};

inline 
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];
}

inline Screen &Screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}

inline Screen &Screen::set(pos r, pos col, char ch)
{
    contents[r * width + col] = ch;
    return *this;
}

    //typedef std::vector<Screen>::size_type ScreenIndex;
class Window_mgr {
public:
    typedef std::vector<Screen>::size_type ScreenIndex;
    void clear(ScreenIndex);
    void clear();
private:
    std::vector<Screen> screens{Screen(24, 80,' ')};

};

void Window_mgr::clear(ScreenIndex i)
{
    Screen &s = screens[i];
    s.contents = string(s.height * s.width, ' ');

}
void Window_mgr::clear()
{
    cout << "Nothing" << endl; 
}

int main()
{
    Screen test(5,5,'x');
    test.display(cout);
    Window_mgr a;


    a.clear(0);
    cout << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值