#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. json数据处理技巧(字段带空格、增加字段)

    1.json数据的正常取值:json[i].fieldName 2.json数据的字段带空格:eval('json[' + i + ']["' + field + '"]') 3. ...

  2. Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    执行远程shell,启动远程机器的tomat时遇到次错误.后来发现原来是远程机器的.profile被人改掉了! 在.profile里加入 export JAVA_HOME=/home/evans/jd ...

  3. UIToolbar自定义背景及按钮设置

      //1.创建toolbar(MyToolbar继承UIToolbar) _myToolbar = [[MyToolbar alloc]initWithFrame:CGRectMake(kZero, ...

  4. 前台传到servlet的乱码问题要怎么处理

  5. c#中文件上传(1)

    * * ;//3M picPath = Server.MapPath("........."); HttpFileCollection postfile = Context.Req ...

  6. 10款基于jquery的web前端特效及源码下载

    1.jQuery时间轴插件:jQuery Timelinr 这是一款可用于展示历史和计划的时间轴插件,尤其比较适合一些网站展示发展历程.大事件等场景.该插件基于jQuery,可以滑动切换.水平和垂直滚 ...

  7. 【leetcode】367. Valid Perfect Square

    题目描述: Given a positive integer num, write a function which returns True if num is a perfect square e ...

  8. input中的id和name

    name在以下用途是不可替代的: 1. 表单(form)的控件名,提交的数据都用控件的name而不是id来控制.因为有许多name会同时对应多个控件,比如checkbox和radio,而id必须是全文 ...

  9. 应用型GIS 地理信息系统设计内容和方法

    挺好的一篇论 文 http://wenku.baidu.com/view/8e40a17c1711cc7931b7165e.html 文章就重点应用型地理信息系统的设计内容.设计过程.相关实现技术与方 ...

  10. codevs 3186 队列练习2

    3186 队列练习 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description (此题与队列练习1相比改了2处:1加 ...