#include<iostream>
#include<string>
using namespace std;
class Person{
public:
Person(const std::string &name, const std::string &address)
{
//前面的name表示私有成员的,而后面的name表示传入的参数
this->name = name;
this->address = address;
}
std::string getName() const
{
return this->name;
//this指向的是当前对象的name
}
std::string getAddress() const
{
return this->address;
}
private:
std::string name;
std::string address;
};
class Screen
{
public:
typedef std::string::size_type index;
Screen(index ht = 0, index wd = 0) :contents(ht*wd, 'a'), cursor(0), height(ht), width(wd)
{
}
char get() const
{
return contents[cursor];
}
char get(index r, index c) const
{
index row = r*width;
return contents[row + c];
}
//移动光标
Screen& move(index r, index c);
Screen& set(index, index, char);
Screen& set(char);
void display(std::ostream &os) const
{
do_display(os);
}
private:
std::string contents;
index cursor;
index height, width;
void do_display(std::ostream &os) const
{
os << contents;
}
};
Screen& Screen::set(index r, index c, char ch)
{
index row = r*width;
contents[row + c] = ch;
return *this;
}
Screen& Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
Screen& Screen::move(index r, index c)
{
index row = r*width;
cursor = row + c;
return *this;
}
int main()
{
Person p("zhangfei", "garden_5");
Person p2("liubei", "garden_6");
cout << p.getName() << endl;
cout << p2.getName() << endl;
Screen myScreen(5, 3);
cout << myScreen.get() << endl;
myScreen.set(3, 2, 'B');
cout << myScreen.get(3, 2) << endl;
//myScreen.move(2, 3);
//myScreen.set('C');
//使用下面的语句必须move函数要返回一个对象,则在上面的Screen中设置为类的引用并使用this指针return,且这里的this是必须写的
myScreen.move(2, 3).set('C');
myScreen.move(4, 0).set('X').move(4, 1).set('Y').move(4, 2).set('Z');
cout << myScreen.get() << endl;
myScreen.display(cout);
cout << endl;
return 0;
}
zhangfei
liubei
a
B
Z
aaaaaaaaaCaBXYZ