zoukankan      html  css  js  c++  java
  • Sales_item.h

      1 <pre name="code" class="plain"><strong><span style="font-size:14px;"><strong>#ifndef SALESITEM_H              
      2 // 若SALESITEM_H未被定义,则对其进行定义;
      3 #define SALESITEM_H
      4 // 自定义头文件Version_test;
      5 #include "Version_test.h" 
      6  
      7 //Sales_item类定义的相关函数
      8 #include <iostream>
      9 #include <string>
     10  
     11 class Sales_item {
     12 /*定义为友元函数,重载输入运算符>>;
     13 **第1个形参:运算符将要读取的流的引用;第2个形参:将要读入到的对象的引用;
     14 **第2个形参必须是非常量,因为本操作就是向此对象写入数据,此对象值会改变;
     15 */
     16 friend std::istream& operator>>(std::istream&, Sales_item&);
     17  
     18 /*定义为友元函数,重载输出运算符<<;
     19 **第1个形参:非常量ostream对象的引用;第2个形参:一个常量的引用;
     20 **第1个形参必须是非常量,因为本操作就是向流写入数据,其状态会改变;
     21 **第2个形参:引用是因为避免赋值实参;常量是因为通常打印对象不会改变对象本身的值;
     22 */
     23 friend std::ostream& operator<<(std::ostream&, const Sales_item&);
     24  
     25 /*定义为友元函数,重载关系运算符<;
     26 **第1个/第2个形参:一个常量的引用;
     27 **引用是因为避免改变实参;常量是因为比较对象并不需要改变对象本身的值;
     28 */
     29 friend bool operator<(const Sales_item&, const Sales_item&);
     30  
     31 /*定义为友元函数,重载相等运算符==;
     32 **定义两个对象是否相等,会比较对象的每一个数据成员,只有对应成员均相等时才相等;
     33 **第1个/第2个形参:一个常量的引用;
     34 **引用是因为避免改变实参;常量是因为比较对象并不需要改变对象本身的值;
     35 */
     36 friend bool operator==(const Sales_item&, const Sales_item&);
     37  
     38 //定义公共成员
     39 public:
     40 /*这种传递中的代码使用以下变量来控制编译;
     41 **变量:IN_CLASS_INITS/DEFAULT_FCNS;对应C++作用:类初始状态设置/default(默认);
     42 */
     43 #if defined(IN_CLASS_INITS) && defined(DEFAULT_FCNS)
     44 //若定义类初始值设置及默认值,则定义默认构造函数,不接受任何实参;
     45     Sales_item() = default;
     46 #else
     47 /*若未定义类初始值及默认值,则定义构造函数,函数体空;
     48 **构造函数初始列表为新创建的数据成员units_sold/revenue初始化,对应初始化值为0/0.0;
     49 */
     50     Sales_item(): units_sold(0), revenue(0.0) { }
     51 #endif
     52 /*只有一个string类型参数的构造函数,函数体空;
     53 **构造函数使用这个string对象初始化bookNo,units_sold/revenue使用类内初始值初始化;
     54 */
     55     Sales_item(const std::string &book):
     56               bookNo(book), units_sold(0), revenue(0.0) { }
     57 /*一个istream为参数的构造函数,函数体空;
     58 **函数体:从is中读取一条交易信息后存入this对象中;
     59 */
     60     Sales_item(std::istream &is) { is >> *this; }
     61 public:
     62 /* 重载运算符+=,用于对象之间的累加运算;
     63 ** 成员二元操作:左侧操作必然隐含的this指针
     64 */
     65     Sales_item& operator+=(const Sales_item&);
     66     
     67 /*常成员函数,表示只能由常成员对象访问;
     68 **对Sales_item对象进行操作,返回bookNo;
     69 */
     70     std::string isbn() const { return bookNo; }
     71     double avg_price() const;
     72 // 私有成员
     73 private:
     74 //隐式初始化为空字符串
     75     std::string bookNo;  
     76 //IN_CLASS_INITS:类初始状态设置;
     77 #ifdef IN_CLASS_INITS
     78 //显式初始化;
     79     unsigned units_sold = 0; 
     80     double revenue = 0.0;
     81 #else
     82 //定义变量;
     83     unsigned units_sold;  
     84     double revenue;       
     85 #endif
     86 };
     87  
     88 /* 二元谓词(有两个参数);
     89 **一个可调用的表达式,返回结果是一个能用作条件的值。
     90 **用于比较对象.isbn是否相等,相等返回布尔值true,否则false;
     91 */
     92 inline bool compareIsbn(const Sales_item &lhs, const Sales_item &rhs) 
     93 { return lhs.isbn() == rhs.isbn(); }
     94  
     95 // 非成员二元操作:必须声明每个操作数的参数;
     96 Sales_item operator+(const Sales_item&, const Sales_item&);
     97  
     98 /* 二元谓词(有两个参数);内联;
     99 **一个可调用的表达式,返回结果是一个能用作条件的值。
    100 ****定义==,用于对象相等;
    101 */
    102 inline bool operator==(const Sales_item &lhs, const Sales_item &rhs)
    103 {
    104     // must be made a friend of Sales_item
    105     return lhs.units_sold == rhs.units_sold &&
    106            lhs.revenue == rhs.revenue &&
    107            lhs.isbn() == rhs.isbn();
    108 }
    109  
    110 /* 二元谓词(有两个参数);内联;
    111 **一个可调用的表达式,返回结果是一个能用作条件的值。
    112 **定义!=,用于对象不相等;与==相反;
    113 */
    114 inline bool operator!=(const Sales_item &lhs, const Sales_item &rhs)
    115 {
    116     return !(lhs == rhs); 
    117 }
    118  
    119 // 假定两个对象指的是具有相同的ISBN,重载函数定义+=,用于对象+=;
    120 Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
    121 {
    122     units_sold += rhs.units_sold; 
    123     revenue += rhs.revenue; 
    124     return *this;
    125 }
    126  
    127 //假定两个对象指的是具有相同的ISBN,重载函数定义定义+,用于对象+;
    128 Sales_item operator+(const Sales_item& lhs, const Sales_item& rhs) 
    129 {
    130     Sales_item ret(lhs);  
    131     ret += rhs;         
    132     return ret;           
    133 }
    134 //定义输入流>>,用于对象输入;
    135 std::istream& operator>>(std::istream& in, Sales_item& s)
    136 {
    137     double price;
    138     in >> s.bookNo >> s.units_sold >> price;
    139     // check that the inputs succeeded
    140     if (in)
    141         s.revenue = s.units_sold * price;
    142     else 
    143         s = Sales_item();  // input failed: reset object to default state
    144     return in;
    145 }
    146 //定义输出流<<,用于对象输出;
    147 std::ostream& operator<<(std::ostream& out, const Sales_item& s)
    148 {
    149     out << s.isbn() << " " << s.units_sold << " "
    150         << s.revenue << " " << s.avg_price();
    151     return out;
    152 }
    153 //定义函数avg_price;
    154 double Sales_item::avg_price() const
    155 {
    156     if (units_sold) 
    157         return revenue/units_sold; 
    158     else 
    159         return 0;
    160 }
    161 #endif
    162 </strong>
    163 </span></strong>
  • 相关阅读:
    MySQL索引背后的数据结构及算法原理 [转]
    5.5下对DDL操作提速的测试
    由浅入深理解索引的实现(2) [转]
    由浅入深理解索引的实现(1) [转]
    两个比较有用的字符串函数
    在慢查询里保留注释部分
    想在Innodb表上做OPTIMIZE操作?先等等看再说!
    Win CE和smartphone和pocket pc和windows mobile比较(zt)
    学习笔记(配置SQL Server 2005允许远程连接)
    配置程序集的版本策略(zt)
  • 原文地址:https://www.cnblogs.com/wsl96/p/13374174.html
Copyright © 2011-2022 走看看