类的基础学习笔记
#include "stdafx.h"
#include <string>
using namespace std;
class Screen
{
friend istream&
operator>>(istream&,Screen&);
friend ostream&
operator<<(ostream&,const Screen&);
private:
//static const int _height=24;//行数
//static const int _width=80; //列数
string _screen;
string::size_type _cursor;//当前屏幕screen的位置
short _height,_width;
public:
void home(){_cursor=0;};//inline函数
void move(int,int);
char get(){return _screen[_cursor];};
char get(int,int);
bool checkRange(int,int);
int Height(){return _height;};
int Width(){return _width;};
void copy(const Screen &sobj);
}ptrScreen,myScreen;
class StackScreen
{
int topStack;
Screen *stack;
void(*handler)();
};
class LinkScreen
{
Screen window;
LinkScreen *next;
LinkScreen *prev;
};
#include "stdafx.h"
#include "Screen.h"
bool Screen::checkRange(int row,int col)
{
if (row<1||row>_height||
col<1||col>_width)
{
cerr<<"Screen coordinates("
<<row<<","<<col
<<")out of bounds.\n";
return false;
}
return true;
}
inline void Screen::move(int r,int c)
{//将_cursor称到绝对位置
if(checkRange(r,c))//位置合法吗?
{
int row=(r-1)*_width;//行位置
_cursor=row+c-1;
}
}
char Screen::get(int r,int c)
{
move(r,c);//_cursor位置
return get();//另一个get()成员函数
}
void Screen::copy(const Screen &sobj)
{
//如果这个Screen对象与sobj是同一个对象,则无需拷贝
if (this!=&sobj)
{
_height=sobj._height;
_width=sobj._width;
_cursor=0;
//创建一个新字符串,他的内容与sobj.screen相同
_screen=sobj._screen;
}
}
}
ostream& operator<<(ostream& os,const Screen &s)
{
os<<"<"<<s._height
<<","<<s._width<<">";
os<<s._screen;
return os;
}
bool isEqual(Screen &s1,Screen *s2)
{
if(s1.Height()!=s2->Height()||
s1.Width()!=s2->Width())
return false;
for(int ix=0;ix<s1.Height();ix++)
for(int jy;jy<s2->Width();jy++)
if (s1.get(ix,jy)!=s2->get(ix,jy))
{
return false;
}
return true;
}