class练习2. 类之间的交互 / 条件型交互(两方关系的成立)

来源:互联网 发布:淘宝日落共青城第三部 编辑:程序博客网 时间:2024/05/21 10:34
#include <iostream>#include <string>using namespace std;class Item;class LibraryUser {private:  string name;  int items_borrowed_count;  Item *items_borrowed[3];  包含三项内容,书名,借阅数量,以及指向书本的指针  存作数组,每一个数组元素都是指向书本的指针----------public:  LibraryUser(string n) : name(n) {    items_borrowed_count = 0;    for (int c=0; c<3; c++)      items_borrowed[c] = NULL;  }  构造函数:同练习题1一样,首先利用初始化列表初始化其中的一项          或者可以用相同的办法一次初始好几个          LibraryUser(string n,int y) : name(n),items_borrowed_count(y)          而不是一次只能做一个  然后在函数体内清空指针组,表示目前这个人还没有借阅其他内容  目前老师两个例题都是通过这种方式初始化,推荐使用这种方式----------  bool borrow(Item &target, int days);};    这里使用了其他类的对象作为参数,但这个函数却不是友元函数    可能是因为只需要这个对象即可,而不需要考察对象内部的信息,所以不需要命令为友元函数    而这里使用了引用 ,引用了另一个item 如果不加引用号会报错    //为什么呢    利用函数完成类之间的交互----------class Item {protected:  int location;  string title;  LibraryUser *borrowed_by;  暂定的基类,包含有地方,书名,借阅者等基本信息public:  Item(string t, int l) : title(t), location(l) {    cout << "Item(" << this << "): constructing with '" << title << "' at location " << location << endl;    this一般是指向对象的指针,可以取出对象中的数据    直接输出this  默认是输出对象的名称      borrowed_by = NULL;  }  这里就一次初始化了两样物品,有的没的全都初始化了  LibraryUser *get_borrowed_by() const { return borrowed_by; }  void set_borrowed_by(LibraryUser *person) { borrowed_by = person; }  virtual int get_loan_period() const=0;  string get_title() const { return title; }};暂定一个基类,叫做“东西类”,我们今天所有的内容,都要以东西类作为基础----------class Book : public Item {     子类1:书本private:  int loan_period;  在父类的基础上,再加上借期的数据,现在一共四个信息public:  Book(string t, int l, int lp) : Item(t,l), loan_period(lp) {    cout << "Book(" << this << "): constructing with loan_period = " << loan_period << endl;  }   - 初始化子类之前一定要先初始化父类  这次四个信息全部初始化完全,借阅者的信息,   - 一定是全空的,而不是缺省,因为我们把它设定成全空   - 内联方式,函数代码就在类声明里写明  int get_loan_period() const {    return loan_period;  }};这个函数可以将我们初始化的时候输入进去的借期信息输出出来,拿来使用----------class Periodical : public Item {    子类2:期刊杂质private:  static int loan_period;   STATIC类信息,所有期刊都是一个借期,此外,STATIC数据不在类声明中初始化,都是在类外面初始化完成public:  Periodical(string t, int l) : Item(t,l) {  }  由于期刊对象除了静态变量以外没有自己特有的变量,因此也不许要特别的来初始化信息  int get_loan_period() const {    return loan_period;  }  static void set_loan_period(int lp) {    loan_period = lp;  }};    静态函数,静态函数来将静态变量初始化-----------交互交互交互交互交互-bool LibraryUser::borrow(Item &target, int days) {1.为什么是引用符号?     因为要切实改变对象的借阅状态,不要创造一个赋值对象2.为什么是lib_user?    因为是人员发出借阅行为,执行对象是ITEM,由人员与ITEM之间产生交互交互具体体现在什么方面,请往下看3.为什么是Item?        a.因为是Item的子类,所以书本 / 期刊有borrowed_by数据                      b.我们显示这里的操作对象是一个基类,但是实际上填写的是子类,做法没有错                      c.我们获得了这个item对象                      d.由这个item对象调用自己的函数改变自身的借阅状态                      e.一个类对象发出操作,改变另一个类的对象,改变对象的数据为调用者信息                      f.怎么完成调用者信息查询? -“THIS”  if (target.get_borrowed_by() != NULL)  {    cerr << "Sorry, that item is already on loan!" << endl;    return false;  }  错误情况1:检阅书本的借阅情况----------  if (items_borrowed_count >= 3) {    cerr << "Sorry, you have too many items already on loan" << endl;    return false;  }  错误情况2:检阅借阅者目前接了几本书了----------  if (days > target.get_loan_period()) {    cerr << "Sorry, you've asked for too long" << endl;    return false;  }  target.set_borrowed_by(this);错误情况3:检测借阅周期是否过于长----------  //  items_borrowed[items_borrowed_count++] = &target;  bool success = false;  for (int n=0; n<3; n++) {    if (items_borrowed[n] == NULL) {      items_borrowed[n] = &target;      success = true;      break;    }  }找到一个空位,将书名填充到借阅列表里看一下怎么将对象的地址赋给指针, &取地址----------  if (!success) {    cerr << "Internal error" << endl;    return false;  }  cout << name << " successfully borrows " << target.get_title() << endl;  items_borrowed_count++;  return true;}int Periodical::loan_period = 5;int main() {  Book cppwt("C++ Without Tears", 1, 10);  Book hhgitc("Hitchhikers Guide to IC", 2, 13);  Item *i = &cppwt;  cout << i->get_loan_period() << endl;  Periodical::set_loan_period(7);  Periodical atwww80("Around...", 3);  Periodical tmuqt("20m under the Queen's Tower", 4);  LibraryUser ali("ali");  LibraryUser georgia("georgia");  georgia.borrow(hhgitc, 7);  ali.borrow(hhgitc, 7);  return 0;}