c++学习笔记-类2

#include<iostream>
#include<string>

using namespace std;

class Screen
{
   public:
       typedef std::string::size_type index;//别名

       Screen(index ht = 0, index wd = 0) :contents(ht*wd,'A'), cursor(0), height(wd)
       {}

       char get() const
       {
           return contents[cursor];
       }
       char get(index r,index c) const//行列,get就是重载,类得成员函数也可以重载
       { 
        index row = r*width;
        return contents[row+c]; 
       }
       char get() const;


private:
     std::string contents;
     index cursor;
     index height, width;
};

inline char Screen::get() const
{
    return contents[cursor];
}
//有一种方法把函数做成内联函数,加incline,在类里声明
//函数在内部,函数声明在外部,也不是内联得,做成内敛得,必须写incline


int main()
{
    Screen a(10,100);

    cout << a.get() << endl;
    cout << a.get(2.8) << endl;
    system("pause");

    return 0;

}

猜你喜欢

转载自blog.csdn.net/weixin_42655231/article/details/82467580