#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. Linux与Windows文件传输实现

    Linux与Windows文件传输实现 一.概述 在学习Linux服务器的时候,我们有时需要与Windows下的文件进行交互传输,这个时候我们需要如何实现呢?今天是我第一次在博客园上写文章,此时正值学 ...

  2. oracle数据操纵语言(DML)data manipulation language

    数据库操纵语言(DML)用于查询和操纵模式对象中的数据,它不隐式地提交当前事务. SELECTINSERTUPDATEDELETECALLEXPLAIN PLANLOCK TABLEMERGE使用算术 ...

  3. 自定义TabBarController报错 - Unbalanced calls to begin/end appearance transitions for <>

    自定义了TabBarController 之后必须实现以下方法才能避免报错 -(void)viewWillAppear:(BOOL)animated { [self.selectedViewContr ...

  4. Matlab之画图

    1.设置对数坐标 set(gca,'XScale','log','YScale','log'); 2.加网格 grid on; 3.加log对齐网格 set(gca,'XScale','log','Y ...

  5. Struts2_使用 Filter 作为控制器的 MVC 应用

  6. JavaScript之动画3

    给一个div添加颜色,使其产生渐变效果,我们设置index为变量,使用setInterval函数方法改变rgb颜色值. window.onload = function(){ var boxDom = ...

  7. jQuery简单邮箱验证

    function chekmail() { var szReg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; ...

  8. js 调用php代码

    <?php $test = "var a = ".$_GET['test'].";"; ?> <mce:script type="t ...

  9. iOS - OC & Xcode

    一.入门 1.1 iOS模版介绍 1.2 简单工程项目 1.3 设置App启动的设备方向 1.4 Xcode界面介绍 1.5 快速查找文件 1.6 快速更改同名变量 1.7 将代码提取为方法 1.8 ...

  10. C# 关于委托和事件的妙文

    C# 关于委托和事件的妙文: 通过一个例子详细介绍委托和事件的作用:Observer模式简介 转自:http://blog.csdn.net/susan19890313/article/details ...