forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Exercise 14.13: Adding substraction and compound substraction
- Loading branch information
1 parent
ea1121a
commit c0e5d31
Showing
4 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// ex14_13.cpp | ||
// Exercise 14.13 | ||
// | ||
// Created by pezy on 3/9/15. | ||
// Substraction and compound-substraction operations added by hoeselm on 4/10/16. | ||
// | ||
// @Brief implementation of class Sales_data | ||
// @See ex14_13.h | ||
|
||
#include "ex14_13.h" | ||
|
||
Sales_data::Sales_data(std::istream &is) : Sales_data() | ||
{ | ||
is >> *this; | ||
} | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// ex14_13.h | ||
// Exercise 14.13 | ||
// | ||
// Created by pezy on 3/9/15. | ||
// Substraction and compound-substraction operations added by hoeselm on 4/10/16. | ||
// | ||
// @See ex7_41.h | ||
// @Add overloaded input, output, addition, and compound-assignment operators | ||
|
||
#ifndef CP5_CH14_EX14_13_H | ||
#define CP5_CH14_EX14_13_H | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class Sales_data { | ||
friend std::istream& operator>>(std::istream&, Sales_data&); // input | ||
friend std::ostream& operator<<(std::ostream&, const Sales_data&); // output | ||
friend Sales_data operator+(const Sales_data&, const Sales_data&); // addition | ||
friend Sales_data operator-(const Sales_data&, const Sales_data&); // substraction | ||
|
||
public: | ||
Sales_data(const std::string &s, unsigned n, double p):bookNo(s), units_sold(n), revenue(n*p){ } | ||
Sales_data() : Sales_data("", 0, 0.0f){ } | ||
Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f){ } | ||
Sales_data(std::istream &is); | ||
|
||
Sales_data& operator+=(const Sales_data&); // compound-assignment | ||
Sales_data& operator-=(const Sales_data&); // compound-substraction | ||
std::string isbn() const { return bookNo; } | ||
|
||
private: | ||
inline double avg_price() const; | ||
|
||
std::string bookNo; | ||
unsigned units_sold = 0; | ||
double revenue = 0.0; | ||
}; | ||
|
||
std::istream& operator>>(std::istream&, Sales_data&); | ||
std::ostream& operator<<(std::ostream&, const Sales_data&); | ||
Sales_data operator+(const Sales_data&, const Sales_data&); | ||
Sales_data operator-(const Sales_data&, const Sales_data&); | ||
|
||
inline double Sales_data::avg_price() const | ||
{ | ||
return units_sold ? revenue/units_sold : 0; | ||
} | ||
|
||
#endif // CP5_CH14_EX14_13_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#include "ex14_13.h" | ||
|
||
int main() | ||
{ | ||
Sales_data s1("book1", 150, 10); | ||
Sales_data s2("book1", 200, 20); | ||
|
||
std::cout << s1 << std::endl; | ||
|
||
// Assignment | ||
s1 = s1 + s2; | ||
std::cout << s1 << std::endl; | ||
|
||
// Compound assignment | ||
s1 += s2; | ||
std::cout << s1 << std::endl; | ||
|
||
// Compound substraction | ||
s1 -= s2; | ||
std::cout << s1 << std::endl; | ||
|
||
// Substraction | ||
s1 = s1 - s2; | ||
std::cout << s1 << std::endl; | ||
} |