#include<iostream>
#include<string>
using namespaces std;
class Screen
{
public:
typedef std::string::size_type index;
Screen(const std::string &text,index ht=0,index wt=0)
:cotents(text),cursor(0),heigth(ht),width(wt){}
Screen(index ht,index wt):contents(ht*wt,'A'),cursor(0),heigth(ht),width(wt){}
char get()
{
return contents[cursor];
}
char get(index r,index c)
{
index row=r*width;
return contents[row+c];
}
private:
std::string contents;
std::string::size_type cursor;
std::string::size_type height,width;
};
int main()
{
Screen x("hello Screem calss",3,6);
cout<<x.get()<<endl;
cout<<x.get(2.3)<<endl;
Screen y(10,10);
cout<<y.get()<<endl;
return 0;
}
注:
1. public中的成员函数在比较少的时候,将其写在public内部,当成员函数较大时需要将其写在类的外面,此时只需要在public里面声明;
2. 当成员函数写在类的外面时,此时成员函数将不是内联函数,需要通过inline显示的定义为内联函数;
```cpp
{
public:
...
char get();
...
char get(index r,index c);
...
}
inline Screen::char get()//定义成内联函数;
{
return contents[cursor];
}
Screen::char get(index r,index c)//未定义成内联函数;内联函数一般效率比较高;
{
index row=r*width;
return contents[row+c];
}