Srceen function

#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)//一般情况字符串需要用const定义,且采用引用形式。
 :cotents(text),cursor(0),heigth(ht),width(wt){}
 Screen(index ht,index wt):contents(ht*wt,'A'),cursor(0),heigth(ht),width(wt){}//初始化的时候定义contents全部是A
 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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值