C++之类-2

#include<iostream>

using namespace std;

class Screen
{
public:
    //用typedef进行同义替换,且同义替换词在内部在外部都是一样的
    typedef std::string::size_type index;
    //写在类的内部的都是内联函数
    //定义构造函数
    Screen(index ht = 0, index wd = 0) :contents(ht*wd, 'A'), cursor(0), height(ht), width(wd)
    {

    }
    Screen(index ht, index wd, const std::string &conts);

    /*char get() const;
    {
        return contents[cursor];
    }*/
    /*//如果在类的内部只写类的声明而将函数的内容写在类的外部,那么这个函数就不再是内联函数了
    如果还是希望将写在外面的函数是内联的,那么就需要在函数前面添加关键字inline,外部函数或者内部声明前面都可以*/
    char get() const;
    //和普通的函数一样,类的成员函数也是可以重载的
    char get(index r, index c) const
    {
        index row = r*width;

        return contents[row + c];
    }
private:
    std::string contents;
    index cursor;
    index height, width;
};
inline char Screen::get() const
{
    return contents[cursor];
}

Screen::Screen(index ht, index wd, const std::string &conts) :contents(conts), cursor(0), height(ht), width(wd)
{

}
int main()
{
    Screen a(10,100);
    cout << a.get() << endl;
    cout << a.get() << endl;

    Screen b(6, 16, "hello screen class");
    cout << b.get() << endl;
    cout << b.get(0,4) << endl;

    cout << "测试一下" << endl;


    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容