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;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值