Screen.cpp:
#include "stdafx.h"
#include "Screen.h"
#include <string>
using namespace std;
Screen::Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) //ht*wd个C填充contents
{
}
Screen::Screen(pos r, pos c)
{
contents=(r*c, ' ');
}
char Screen::get() const
{
access_ctr++;
return contents[cursor];
}
char Screen::get(pos r, pos c) const
{
access_ctr++;
pos row = r*width;
return contents[row+c];
}
Screen & Screen::set(char c)//如果该函数返回一个Screen而非Screen&,则需要一个Screen对象来接收他,返回的是对象的拷贝
{
contents[cursor] = c;
return *this;//返回调用该方法的对象的引用,即返回对象本身而非副本
//即:s.move(4,0).set('#')
//等价于:s.move(4,0)
// s.set('#')
}
Screen & Screen::set(pos r, pos col, char ch)
{
contents[r*width+col] = ch;//设置给定位置的新值
return *this;//返回调用该方法的对象的引用
}
Screen & Screen::display(std::ostream& os)
{
do_display(os);
return *this;
}
const Screen & Screen::display(std::ostream& os) const
{
os << "常量版本的display:" << std::endl;
do_display(os);
return *this;
}
inline Screen& Screen::move(pos r, pos c)//我们可以在类的内部把inline作为定义/声明的一部分显式的声明成员函数
{
pos row = r*width;//计算行的位置
cursor = row + c;//在行内将光标移到指定的列
return *this;//返回该类的实例
}
std::string::size_type Screen::get_access_ctr()
{
return access_ctr;
}
Screen::~Screen()
{
}
void Screen::do_display(std::ostream& os) const
{
os << contents;//显示相应的内容
}
#pragma once
#include <string>
class Screen
{
public:
typedef std::string::size_type pos;//表示位置,size_type类型
//-------------------constructor-------------------------
Screen()=default;//Screen有另一个构造函数,所以该函数是必须的
Screen(pos, pos, char);
Screen(pos, pos);
//-------------------get-------------------------
char get() const;//读取光标处的字符,隐式内联
char get(pos, pos)const;
//-------------------set-------------------------
Screen& set(char);//设置光标所在位置的字符
Screen& set(pos, pos, char);//设置给定位置的字符
//inline char get(pos, pos)const;//显式内联
//-------------------display-------------------------
Screen& display(std::ostream&);//根据对象是否是const重载display函数
const Screen& display(std::ostream&)const;
Screen& move(pos r, pos c);//之后被设为内联
std::string::size_type get_access_ctr();
~Screen();
private:
mutable size_t access_ctr=0;//声明了mutable的成员变量可以在常成员函数中被改变
pos cursor = 0;//光标
pos height = 0;//高
pos width = 0;//宽
std::string contents;//内容
//-------------------function-------------------------
void do_display(std::ostream&)const;
};
main:
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <vector>
#include "SalesItem.h"
#include "Chapter6.h"
#include "Person.h"
#include "Screen.h"
struct Sales_date
{
Sales_date()
{
}
string isbn() const
{
return bookNo;
}
string bookNo;
unsigned units_sold = 0;//成员的默认值
double revenue = 0.0;
};
//命名空间:
using namespace std;
using std::vector;
int main(int argc, char *argv[])
{
/*
Screen s(2,2,'*');
Screen s2 = s;
char c = s2.get(0,0);
cout << s2.get()<<endl;
cout << c << endl;
cout << "access_ctr:" << s2.get_access_ctr() << endl;
*/
Screen s(3, 3,'*');//非常量Screen对象
const Screen s2(3, 3,'o');//常量Screen对象
//在调用Screen对象的display方法时,会根据对象是否为const版本而调用相应的常量/非常量版本的display函数
s.set('#').display(cout);
cout << endl;
s2.display(cout);
cout << endl;
//Screen s3(3, 3, '@');
//s3.display(cout);
system("pause");
return 0;
}