c++_primer_exercise_1402

/*****************************************************
 * IDE:  VS2010
 * FILE: Sales_data.h
 *****************************************************/
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include 
  
  
   
   
#include 
   
   
    
    

class Sales_data
{
	friend std::ostream &operator<<(std::ostream &os, const Sales_data &item);
	friend std::istream &operator>>(std::istream &is, Sales_data &item);
	friend bool operator==(const Sales_data &lhs, const Sales_data &rhs);
	friend bool operator!=(const Sales_data &lhs, const Sales_data &rhs);
	friend Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs);

public:
	// constructors
	Sales_data() : units_sold(0), revenue(0.0) { }
	Sales_data(const std::string &s) :
		book_no(s), units_sold(0), revenue(0.0) { }
	Sales_data(const std::string &s, unsigned int n, double price) :
		book_no(s), units_sold(n), revenue(price * n) { }
	
	std::string isbn() const { return book_no; }
	Sales_data &operator+=(const Sales_data &rhs);

private:
	std::string book_no;
	unsigned int units_sold;
	double revenue;

	// utility function
	double avg_price() const;
};

#endif

   
   
  
  

/*****************************************************
 * IDE:  VS2010
 * FILE: Sales_data.cpp
 *****************************************************/
#include "Sales_data.h"

#include 
   
   
    
    
using std::string;
#include 
    
    
     
     
using std::istream; using std::ostream;

 utility function
double Sales_data::avg_price() const
{
	if (units_sold)
	{
		return revenue / units_sold;
	}
	else
	{
		return 0;
	}
}

 non-member Sales_data operations
inline ostream &operator<<(ostream &os, const Sales_data &item)
{
	os << item.isbn() << " " << item.units_sold << " "
	   << item.revenue << " " << item.avg_price();
	return os;
}

inline istream &operator>>(istream &is, Sales_data &item)
{
	double price;	// no need to initialize; we'll read into price before we use it
	is >> item.book_no >> item.units_sold >> price;
	if (is)			// check that the inputs succeeded
	{
		item.revenue = item.units_sold * price;
	}
	else			// input failed: give the object the default state
	{
		item = Sales_data();
	}
	return is;
}


inline bool operator==(const Sales_data &lhs, const Sales_data &rhs)
{
	return lhs.isbn() == rhs.isbn() &&
		lhs.units_sold == rhs.units_sold &&
		lhs.revenue == rhs.revenue;
}

inline bool operator!=(const Sales_data &lhs, const Sales_data &rhs)
{
	return !(lhs == rhs);
}

// member binary operator: left-hand operand is bound to the implicit this pointer
// assumes that both objects refer to the same book
Sales_data &Sales_data::operator+=(const Sales_data &rhs)
{
	units_sold += rhs.units_sold;
	revenue += rhs.revenue;
	return *this;
}

// assume that both objects refer to the same book
Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
{
	Sales_data sum = lhs;	// copy data 
	sum += rhs;
	return sum;
}

    
    
   
   

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值