C++ Primer 学习笔记——类(一)

* 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值