作者在 2008-08-12 14:08:34 发布以下内容
无聊,发点代码晾晾……
// a Screen class
#include<iostream>
#include<string>
using namespace std;
#include<string>
using namespace std;
class Screen
{
public:
typedef string ::size_type index;
//the constructor
Screen();
Screen(index hh,index ww): height(hh),width(ww),cursor(hh),contents(hh*ww,' '),access_ctr(0){ }
char get() const
{
return contents[cursor];
}
char get(index ht,index wd) const;
inline index get_cursor() const; //inline declared in the class declaration,but can be defined inline later
Screen& move(index r,index c);
Screen& set(char);
Screen& set(index,index,char);
//display overloaded on whether the object is const or not
Screen& display(ostream &os)
{
do_display(os);
return *this;
}
const Screen& display(ostream &os) const
{
do_display(os);
return *this;
}
private:
string contents;
index cursor;
index height,width;
mutable size_t access_ctr; //may change in a const members
void do_display(ostream &os) const
{
++access_ctr;
os<<contents;
}
};
{
public:
typedef string ::size_type index;
//the constructor
Screen();
Screen(index hh,index ww): height(hh),width(ww),cursor(hh),contents(hh*ww,' '),access_ctr(0){ }
char get() const
{
return contents[cursor];
}
char get(index ht,index wd) const;
inline index get_cursor() const; //inline declared in the class declaration,but can be defined inline later
Screen& move(index r,index c);
Screen& set(char);
Screen& set(index,index,char);
//display overloaded on whether the object is const or not
Screen& display(ostream &os)
{
do_display(os);
return *this;
}
const Screen& display(ostream &os) const
{
do_display(os);
return *this;
}
private:
string contents;
index cursor;
index height,width;
mutable size_t access_ctr; //may change in a const members
void do_display(ostream &os) const
{
++access_ctr;
os<<contents;
}
};
Screen::Screen(): height(0),width(0),cursor(0),contents(""),access_ctr(0) { }//constructor
inline char Screen::get(index r,index c) const //not declared inline in the class declaration,but ok to make inline in definition
{
index row=r*width;
return contents[row+c];
}
Screen& Screen::move(index r,index c)
{
index row=r*width;
cursor=row+c;
return *this;
}
Screen& Screen::set(char c)
{
contents[cursor]=c;
return *this;
}
Screen& Screen::set(index r,index c,char ch)
{
index row=r*width;
cursor=row+c;
contents[cursor]=ch;
return *this;
}
inline char Screen::get(index r,index c) const //not declared inline in the class declaration,but ok to make inline in definition
{
index row=r*width;
return contents[row+c];
}
Screen& Screen::move(index r,index c)
{
index row=r*width;
cursor=row+c;
return *this;
}
Screen& Screen::set(char c)
{
contents[cursor]=c;
return *this;
}
Screen& Screen::set(index r,index c,char ch)
{
index row=r*width;
cursor=row+c;
contents[cursor]=ch;
return *this;
}
int main()
{
Screen myscreen(5,3);
const Screen blank(5,3);
myscreen.move(4,0);
myscreen.set('#');
myscreen.move(4,0).set('&'); //move cursor to given position,and set that character
//calls nonconst version
myscreen.move(4,0).set('*').display(cout); //move cursor to given position,set that character and display the screen
//calls const version
blank.display(cout);
myscreen.get(5,3);
// cout<<myscreen.access_ctr<<endl;
return 0;
}
{
Screen myscreen(5,3);
const Screen blank(5,3);
myscreen.move(4,0);
myscreen.set('#');
myscreen.move(4,0).set('&'); //move cursor to given position,and set that character
//calls nonconst version
myscreen.move(4,0).set('*').display(cout); //move cursor to given position,set that character and display the screen
//calls const version
blank.display(cout);
myscreen.get(5,3);
// cout<<myscreen.access_ctr<<endl;
return 0;
}