Sales_item.h

Sales_item.h(C++ primer 4)

/*
* This file contains code from "C++ Primer, Fourth Edition", by Stanley B.
* Lippman, Jose Lajoie, and Barbara E. Moo, and is covered under the
* copyright and warranty notices given in that book:

* "Copyright (c) 2005 by Objectwrite, Inc., Jose Lajoie, and Barbara E. Moo."


* "The authors and publisher have taken care in the preparation of this book,
* but make no expressed or implied warranty of any kind and assume no
* responsibility for errors or omissions. No liability is assumed for
* incidental or consequential damages in connection with or arising out of the
* use of the information or programs contained herein."

* Permission is granted for this code to be used for educational purposes in
* association with the book, given proper citation if and when posted or
* reproduced.Any commercial use of this code requires the explicit written
* permission of the publisher, Addison-Wesley Professional, a division of
* Pearson Education, Inc. Send your request for permission, stating clearly
* what code you would like to use, and in what specific way, to the following
* address: 

* Pearson Education, Inc.
* Rights and Contracts Department
* 75 Arlington Street, Suite 300
* Boston, MA 02216
* Fax: (617) 848-7047
*/ 
/*
C/C++ Keywords 
asm 插入一个汇编指令. 
auto 声明一个本地变量. 
bool 声明一个布尔型变量. 
break 结束一个循环. 
case 一个switch语句的一部分. 
catch 处理 thrown 产生的异常. 
char 声明一个字符型变量. 
class 声明一个类. 
const 声明一个常量. 
const_cast 从一个const变量中抛出. 
continue 在循环中间断循环. 
default 是一个case语句中的缺省项. 
delete 释放内存. 
do 构造循环体. 
double 声明一个双精度浮点变量. 
dynamic_cast 动态投射. 
else 是一个if语句中的预备条件. 
enum 创建列举类型. 
explicit 仅用在构造器的正确匹配. 
extern 告诉编译器在别的地方变量已经被定义过了. 
false 属于布尔值. 
float 声明一个浮点型变量. 
for 构造循环. 
friend 允许非函数成员使用私有数据. 
goto 跳转到程序的其它地方. 
if 从一次判断的结果处执行代码. 
inline 定义一个函数为内联. 
int 声明一个整型变量. 
long 声明一个长整型变量. 
mutable 忽略const变量. 
namespace 用一个定义的范围划分命名空间. 
new 允许动态存储一个新变量. 
operator 创建重载函数. 
private 在一个类中声明私有成员. 
protected 在一个类中声明被保护成员. 
public 在一个类中声明公共成员. 
register 定义一个寄存器变量. 
reinterpret_cast 改变一个变量的类型. 
return 从一个函数中返回. 
short 声明一个短整型变量. 
signed 修改变量类型声明. 
sizeof 返回一个变量或类型的长度. 
static 给一个变量创建永久的存储空间. 
static_cast 执行一个非多态性cast. 
struct 创建一个新结构体. 
switch 让一个变量在不同的判断下执行不同的代码. 
template 创建一个给特殊函数. 
this 指向当前对象. 
throw 抛出一个异常. 
true 布尔类型的一个值. 
try 执行一个被throw 抛出的异常. 
typedef 从现有的类型中创建一个新类型. 
typeid 描述一个对象. 
typename 声明一个类或未定义的类型. 
union 一个结构体在当前位置分配给多个变量相同的内存. 
unsigned 声明一个无符号整型变量. 
using 用来输入一个namespace. 
virtual 创建一个不被已构成类有限考虑的函数. 
void 声明函数或数据是无关联数据类型. 
volatile 警告编译器有关的变量可能被出乎意料的修改. 
wchar_t 声明一个带有宽度的字符型变量. 
while 用来构成循环
*/
#ifndef SALESITEM_H//如果未定义SALESITEM_H
#define SALESITEM_H//则定义SALESITEM_H

// Definition of Sales_item class and related functions goes here


#include <iostream>
#include <string>

class Sales_item {
//friend为友元声明,允许一个函数被其它类访问,即使是private类型的,也可以进行访问
//形象的说就是把家里的钥匙给了别人,让别人也可以进去
//bool 声明为布尔类型
//operator操作符重载
/*
operator+ 就是对加号进行重载,因为在C++中,
加法两边的对象一般是int型的或者是double,float型的,
但是要对复数进行加法,就不能直接用加号了。这时就必须对加号进行运算符重载,
即operator+这样重载之后对于两个复数之间的加法就可以进行了。
但是有五个运算符使不能重载的,“.”,“.*”,“::”,“sizeof”,以及“?:”。
明白了吗? 
*/
friend bool operator==(const Sales_item&, const Sales_item&);
// other members as before
public:
    // added constructors to initialize from a string or an istream
    Sales_item(const std::string &book):
              isbn(book), units_sold(0), revenue(0.0) { }
    Sales_item(std::istream &is) { is >> *this; }
    friend std::istream& operator>>(std::istream&, Sales_item&);
    friend std::ostream& operator<<(std::ostream&, const Sales_item&);
public:
    // operations on Sales_item objects
    // member binary operator: left-hand operand bound to implicit this pointer
    Sales_item& operator+=(const Sales_item&);
    // other members as before
    
public:
    // operations on Sales_item objects
    double avg_price() const;
    bool same_isbn(const Sales_item &rhs) const
        { return isbn == rhs.isbn; }
    // default constructor needed to initialize members of built-in type
    Sales_item(): units_sold(0), revenue(0.0) { }
// private members as before
private:
    std::string isbn;
    unsigned units_sold;
    double revenue;

};


// nonmember binary operator: must declare a parameter for each operand
Sales_item operator+(const Sales_item&, const Sales_item&);

inline bool 
operator==(const Sales_item &lhs, const Sales_item &rhs)
{
    // must be made a friend of Sales_item
    return lhs.units_sold == rhs.units_sold &&
           lhs.revenue == rhs.revenue &&
       lhs.same_isbn(rhs);
}

inline bool 
operator!=(const Sales_item &lhs, const Sales_item &rhs)
{
    return !(lhs == rhs); // != defined in terms of operator==
}

using std::istream; using std::ostream;

// assumes that both objects refer to the same isbn
inline
Sales_item& Sales_item::operator+=(const Sales_item& rhs) 
{
    units_sold += rhs.units_sold; 
    revenue += rhs.revenue; 
    return *this;
}

// assumes that both objects refer to the same isbn
inline
Sales_item 
operator+(const Sales_item& lhs, const Sales_item& rhs) 
{
    Sales_item ret(lhs); // copy lhs into a local object that we'll return
    ret += rhs;           // add in the contents of rhs 
    return ret;           // return ret by value
}

inline
istream& 
operator>>(istream& in, Sales_item& s)
{
    double price;
    in >> s.isbn >> s.units_sold >> price;
    // check that the inputs succeeded
    if (in)
        s.revenue = s.units_sold * price;
    else 
        s = Sales_item(); // input failed: reset object to default state
    return in;
}

inline
ostream& 
operator<<(ostream& out, const Sales_item& s)
{
    out << s.isbn << "\t" << s.units_sold << "\t" 
        << s.revenue << "\t" << s.avg_price();
    return out;
}

inline     //内联,相当于宏的展开,不需要进行参数压栈,代码生成的一系列操作
double Sales_item::avg_price() const
{
    if (units_sold) 
        return revenue/units_sold; 
    else 
        return 0;
}


#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值