14.13
Sales_data& Sales_data::operator+=(const Sales_data &rhs)
{
units_sold += rhs.units_sold;
revenue += rhs.revenue;
return *this;
}
Sales_data& Sales_data::operator-=(const Sales_data &rhs)
{
units_sold -= rhs.units_sold;
revenue -= rhs.revenue;
return *this;
}
std::istream& operator>>(std::istream &is, Sales_data &item)
{
double price = 0.0;
is >> item.bookNo >> item.units_sold >> price;
if (is)
item.revenue = price * item.units_sold;
else
item = Sales_data();
return is;
}
std::ostream& operator<<(std::ostream &os, const Sales_data &item)
{
os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price();
return os;
}
Sales_data operator+(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum += rhs;
return sum;
}
Sales_data operator-(const Sales_data &lhs, const Sales_data &rhs)
{
Sales_data sum = lhs;
sum -= rhs;
return sum;
}
14.14
14.15
#include <iostream>
#include <string>
class Book {
friend std::istream& operator>>(std::istream&, Book&);
friend std::ostream& operator<<(std::ostream&, const Book&);
friend bool operator==(const Book&, const Book&);
friend bool operator!=(const Book&, const Book&);
friend bool operator<(const Book&, const Book&);
friend bool operator>(const Book&, const Book&);
friend Book operator+(const Book&, const Book&);
public:
Book() = default;
Book(unsigned no, std::string name, std::string author, std::string pubdate, unsigned number):no_(no), name_(name), author_(author), pubdate_(pubdate), number_(number) { }
Book(std::istream &in) { in >> *this; }
Book& operator+=(const Book&);
private:
unsigned no_;
std::string name_;
std::string author_;
std::string pubdate_;
unsigned number_;
};
std::istream& operator>>(std::istream&, Book&);
std::ostream& operator<<(std::ostream&, const Book&);
bool operator==(const Book&, const Book&);
std::istream& operator>>(std::istream &in, Book &book)
{
in >> book.no_ >> book.name_ >> book.author_ >> book.pubdate_ >> book.number_;
return in;
}
std::ostream& operator<<(std::ostream &out, const Book &book)
{
out << book.no_ << " " << book.name_ << " " << book.author_ << " " << book.pubdate_ << " " << book.number_ << std::endl;
return out;
}
bool operator==(const Book &lhs, const Book &rhs)
{
return lhs.no_ == rhs.no_;
}
bool operator!=(const Book &lhs, const Book &rhs)
{
return !(lhs == rhs);
}
bool operator<(const Book &lhs, const Book &rhs)
{
return lhs.no_ < rhs.no_;
}
bool operator>(const Book &lhs, const Book &rhs)
{
return rhs < lhs;
}
Book& Book::operator+=(const Book &rhs)
{
if (rhs == *this)
this->number_ += rhs.number_;
return *this;
}
Book operator+(const Book &lhs, const Book &rhs)
{
Book book = lhs;
book += rhs;
return book;
}