* Sale_data*
#include<iostream>
#include<string>
using namespace std;
class Sale_data
{
private:
string bookNo;//编号
unsigned units_sold;//出售数量
double revenue ;//总收益
public:
//构造
Sale_data() = default;//等同于默认的构造函数
Sale_data(const string &s) :bookNo(s){}
Sale_data(const string &s, unsigned n = 0, double p = 0) :bookNo(s), units_sold(n), revenue(p*n){}
Sale_data(istream &is);
string isbn() const { return bookNo; }
Sale_data& combine(const Sale_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
inline double avg_price() const;
Sale_data add(const Sale_data &lhs, const Sale_data &rhs)
{
Sale_data sum = lhs;
sum.combine(rhs);
return sum;//定义一个sum使原来的副本不改变
}
friend ostream &print(ostream&, const Sale_data&);
friend istream &read(istream&, Sale_data&);
};
Sale_data::Sale_data(istream &is){ read(is, *this); }
ostream &print(ostream &os ,const Sale_data &item)
{
os << item.bookNo << " " << item.units_sold << " " << item.revenue;
return os;
}
istream &read(istream &is, Sale_data &item)
{
double price = 0;
is >> item.bookNo >> item.units_sold >> item.revenue;
item.revenue = price*item.units_sold;
return is;
}
//int main()
//{
// Sale_data total("xxx" ,10,0);
// if (read(cin, total))
// {
// Sale_data trans;
// while (read(cin,trans))
// {
// if (trans.isbn() == total.isbn())
// {
// total.combine(trans);
// }
// else
// {
// print(cout, total) << endl;
// total = trans;
// }
//
// }
// print(cout, total) << endl;
// }
// else
// {
// cerr << " no data?";
// }
//}
Person
#include<iostream>
#include<string>
using namespace std;
class Person
{
string name;
string address;
public:
Person(string sn, string sa) :name(sn), address(sa){}
string Getname() const
{
return name;
}
string Getadd() const
{
return address;
}
friend ostream& print(ostream& os, const Person &p);
friend istream& read(istream& is, Person &p);//IO类属于不能被拷贝的类所以必须用引用
};
ostream& print(ostream& os,const Person &p)
{
os << p.name << " " << p.address;
return os;
}
istream& read(istream& is, Person &p)
{
is >> p.name >> p.address;
return is;
}
Screen
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Screen
{
public:
//typedef string::size_type pos;
using pos = string::size_type;
//构造
Screen() = default;
Screen(pos ht, pos wd,char c):height(ht),width(wd),contents(ht*wd,c){}
Screen(pos ht, pos wd) :height(ht), width(wd), contents(ht*wd, ' '){}
char get() const //得到光标所在位置的字符
{
return contents[cursor];
}
inline char get(pos ht, pos wd) const; //得到任意坐标所在位置的字符
Screen &move(pos ht, pos wd);//移动光标
Screen &set(char);//设置光标所在位置的字符
Screen &set(char, pos, pos);//设置任意位子的字符
Screen &display(ostream & os)
{
//os << contents;//产生相同代码时候,在定义一个函数方便以后测试
do_display(os);
return *this;
}
const Screen &display(ostream &os) const//使display为const对象,则返回值类型也是const Screen&.
//const 的重载
{
//os << contents;
do_display(os);
return *this;
}
//返回值为左值引用类型,意味着函数返回的是对象本身而不是对象的副本(引用就像是指针,返回的就是一个指定了的对象,如果用Screen,则返回的相当于给一个新的对象拷贝赋值,得到一个临时对象)
private:
pos cursor = 0;//cursor在类内部初始值为0
pos height = 0, width = 0;
string contents;
void do_display(ostream &os) const
{
os << contents;
}
};
inline char Screen::get(pos ht, pos wd) const
{
pos row = ht*width;
return contents[row + wd];
}
inline Screen &Screen::move(pos ht, pos wd)//在类体外部定义内敛函数
{
pos row = ht*width;
cursor = row + wd;
return *this;
}
inline Screen &Screen::set(char c)
{
contents[cursor] = c;
return *this;
}
inline Screen &Screen::set(char c, pos ht, pos wd)
{
contents[ht*width + wd] = c;
return *this;
}
//class Window_mgr
//{
//private:
// vector<Screen> screens{Screen(24, 90, ' ') };//初始化类的成员
//};
//报错了??是为什么???
int main()
{
Screen myScreen(5, 5, 's');
myScreen.move(0, 0).set('*').display(cout);
cout << endl;
myScreen.display(cout);
/* *ssss.....
*ssss....
当move的返回值类型为Screen 时候
*ssss.....
sssss.....
说明set没有改变mySreen,而改变了他的副本*/
return 0;
}