⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inventory.h

📁 Think in C++ 第二版源码
💻 H
字号:
//: C21:Inventory.h

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

#ifndef INVENTORY_H

#define INVENTORY_H

#include <iostream>

#include <cstdlib>

#include <ctime>



class Inventory {

  char item;

  int quantity;

  int value;

public:

  Inventory(char it, int quant, int val) 

    : item(it), quantity(quant), value(val) {}

  // Synthesized operator= & copy-constructor OK

  char getItem() const { return item; }

  int getQuantity() const { return quantity; }

  void setQuantity(int q) { quantity = q; }

  int getValue() const { return value; }

  void setValue(int val) { value = val; }

  friend std::ostream& operator<<(

    std::ostream& os, const Inventory& inv) {

    return os << inv.item << ": " 

      << "quantity " << inv.quantity 

      << ", value " << inv.value;

  }

};



// A generator:

struct InvenGen {

  InvenGen() { std::srand(std::time(0)); }

  Inventory operator()() {

    static char c = 'a';

    int q = std::rand() % 100;

    int v = std::rand() % 500;

    return Inventory(c++, q, v);

  }

};

#endif // INVENTORY_H ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -