#ifndef SALESITEM_H
#define SALESITEM_H // Definition of Sales_item class and related functions goes here #include <iostream>
#include <string> class Sales_item {
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(), 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(), 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 ;
} #endif
#include <iostream>
#include "Sales_item.h"
int main()
{
// declare variables to hold running sum and data for the next record
Sales_item total, trans;
// is there data to process?
if (std::cin >> total) {
// if so, read the transaction records
  while (std::cin >> trans)
    if (total.same_isbn(trans))
      // match: update the running total
      total = total + trans;
    else {
      // no match: print & assign to total
      std::cout << total << std::endl;
      total = trans;
    }
    // remember to print last record
    std::cout << total << std::endl;
} else {
  // no input!, warn the user
  std::cout << "No data?!" << std::endl;
  return -; // indicate failure
}
return ;
}

Sales_item的更多相关文章

  1. Sales_item例子

    Sales_item.h #ifndef SALES_ITEM_H #define SALES_ITEM_H #include<iostream> #include<string&g ...

  2. Sales_item.h

    下列是<C++primer>书中介绍和使用的Sales_item.h类 经测试可以使用,现在贴在这里,分享给大家! 版本一: #ifndef SALESITEM_H#define SALE ...

  3. C++primer(第五版)Sales_item.h头文件

    C++primer(第五版)1.51练习章节需要有一个Sales_item类,但是给的网站找不到,直接复制下面就好咯: #ifndef SALESITEM_H #define SALESITEM_H ...

  4. 再读《C++ Primer》——变量和基本类型

    刚上大学那时,几个室友一块买了本<C++ Primer>第4版,看了一遍后就没怎么碰了,偶尔拿出来翻翻,当作工具书使用.后来知道有第5版了,一直觉得内容差不多吧.直到最近,再读其中的一些内 ...

  5. C++学习笔记 知识集锦(二)

    1. 命名规范 2. 代码格式 3. QString的判断 4. 对象的判空 5. 隐式接口&显式接口 6. vector&string 7. static 8. const 9. v ...

  6. C++11语法糖

    1.constexpr变量:声明为constexpr的变量一定是一个常量,新标准允许定义一种特殊的constexpr函数使得编译时就可计算结果,这样就能用constexpr函数去初始化constexp ...

  7. c++ 成员函数

    #include <iostream> #include "Sales_item.h" int main() { Sales_item item1, item2; st ...

  8. 初识C++的类

    //Sales_item.h#ifndef SALESITEM_H #define SALESITEM_H #include <iostream> #include<string&g ...

  9. C++学习基础八——重载输入和输出操作符

    一.重载输入操作符的要点: 1.返回值为istream &. 2.第一个参数为istream &in. 3.第二个参数为自定义类型的引用对象(例如Sales_Item &ite ...

随机推荐

  1. jquery plugins —— datatables ajax post更新数据

    通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...

  2. 百篇大计敬本年之系统篇《六》—— Ubuntu 16.04开启 root 超级用户

    Ubuntu 16.04系统在一开始安装完成时是无法切换到 root 用户的,普通用户需要使用 root 权限的时候通常需要在执行命令前加 "sudo",需要经常使用root权限的 ...

  3. jQuery:节点(插入,复制,替换,删除)操作

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  4. Kinect For Windows V2开发日志八:侦测、追踪人体骨架

    简介 Kinect一个很强大的功能就是它可以侦测到人体的骨骼信息并追踪,在Kinect V2的SDK 2.0中,它最多可以同时获取到6个人.每个人25个关节点的信息,并且通过深度摄像头,可以同时获取到 ...

  5. JavaScript--DOM事件(笔记)

    第1章 事件流1-1.事件冒泡:事件最开始由最具体的元素(文档中嵌套层次最深的那个节点)接收; 然后逐级向上传播至最不具体的那个节点(文档);1-2.事件捕获:不太具体的节点应该更早接收到事件,而最具 ...

  6. CSS3--幽灵按钮特效(实例)

    先预览效果 其中用到了CSS3的Transition属性:设置动画时间.效果:Transform属性:设置元素旋转.位移:box-sizing属性:以特定的方式定义某个区域的特定元素: 制作小三角方法 ...

  7. C语言知识总结(5)

    预处理指令 C语言提供的预处理指令主要有:宏定义.文件包含.条件编译 宏定义 不带参数的宏定义 1>一般形式 #define 宏名 字符串 比如#define A 10 2>作用 它的作用 ...

  8. Get到一个新技能!!! 不要不要的

    刚看到的,交你如何做题!!! 简直6的不行, ----------------------------------------------------------------------------- ...

  9. Ajax_2

    <script> var xmlHttp;//创建一个对象 function queryDetail(id){ if(window.ActiveXObject){ xmlHttp = ne ...

  10. 《锋利的jQuery》心得笔记--Two Sections

    第三章 1.    DOM操作(节点) 1)         查找节点可以查找元素节点和属性节点 2)         创建节点: (1)       创建元素节点 var addLi = $(“&l ...