Sales_item.h
#pragma once
#include <iostream>
#include <string>
class Sales_item {
friend std::ostream &operator<<(std::ostream &out, const Sales_item &s);
friend std::istream &operator>>(std::istream &in, Sales_item &s);
friend bool operator==(const Sales_item &lhs, const Sales_item &rhs);
friend bool operator!=(const Sales_item &lhs, const Sales_item &rhs);
friend bool operator<(const Sales_item &lhs, const Sales_item &rhs);
public:
Sales_item() :bookNo(), unit_sold(0), revenue(0) {};
Sales_item(const std::string &s) : bookNo(s), unit_sold(0), revenue(0) {};
Sales_item(const Sales_item &obj) : bookNo(obj.bookNo), unit_sold(obj.unit_sold), revenue(obj.revenue) {}
Sales_item(std::istream &in) {
in >> bookNo >> unit_sold >> revenue;
}
Sales_item operator+=(const Sales_item &);
Sales_item &operator=(const std::string &s);
Sales_item &operator=(const Sales_item &s);
std::string get_boolNo() const;
double avg_price() const;
private:
std::string bookNo;
unsigned unit_sold;
double revenue;
};
Sales_item operator+(const Sales_item &lhs, const Sales_item &rhs);
Sales_item.cpp
#include "Sales_item.h"
std::ostream &operator<<(std::ostream &out, const Sales_item &s) {
out << s.bookNo << " " << s.revenue << " " << s.unit_sold << s.avg_price();
return out;
}
std::istream &operator>>(std::istream &in, Sales_item &s) {
double price;
in >> s.bookNo >> s.unit_sold >> price;
if (in) {
s.revenue = s.unit_sold * price;
} else {
s = Sales_item();
}
return in;
}
bool operator==(const Sales_item &lhs, const Sales_item &rhs) {
return
lhs.bookNo == rhs.bookNo &&
lhs.unit_sold == rhs.unit_sold &&
lhs.revenue == rhs.revenue;
}
bool operator!=(const Sales_item &lhs, const Sales_item &rhs) {
return !(lhs == rhs);
}
bool operator<(const Sales_item &lhs, const Sales_item &rhs) {
return lhs.revenue < rhs.revenue;
}
Sales_item Sales_item::operator+=(const Sales_item &s) {
Sales_item ret;
ret.bookNo = bookNo;
ret.unit_sold = unit_sold + s.unit_sold;
ret.revenue = revenue + s.revenue;
return ret;
}
Sales_item &Sales_item::operator=(const std::string &s) {
Sales_item tmp(s);
*this = tmp;
return *this;
}
Sales_item &Sales_item::operator=(const Sales_item &s) {
bookNo = s.bookNo;
unit_sold = s.unit_sold;
revenue = s.revenue;
return *this;
}
std::string Sales_item::get_boolNo() const {
return bookNo;
}
double Sales_item::avg_price() const {
return revenue/unit_sold;
}
Sales_item operator+(const Sales_item &lhs, const Sales_item &rhs) {
Sales_item ret;
ret+=lhs;
ret+=rhs;
return ret;
}```