Skip to content

Commit

Permalink
Exercise 14.13: Adding substraction and compound substraction
Browse files Browse the repository at this point in the history
  • Loading branch information
gehtmaguad committed Apr 10, 2016
1 parent ea1121a commit c0e5d31
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ch14/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ see [Exercise 14.5](#exercise-145)
## Exercise 14.13:
>Which other arithmetic operators (Table 4.1 (p. 139)), if any, do you think Sales_data ought to support? Define any you think the class should include.
no others.
Substraction, in order to be able to revert a previously addition.
[hpp](ex14_13.h) | [cpp](ex14_13.cpp) | [Test](ex14_13_TEST.cpp)

## Exercise 14.14:
>Why do you think it is more efficient to define `operator+` to call `operator+=` rather than the other way around?
Expand Down
62 changes: 62 additions & 0 deletions ch14/ex14_13.cpp
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;
}
51 changes: 51 additions & 0 deletions ch14/ex14_13.h
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
25 changes: 25 additions & 0 deletions ch14/ex14_13_TEST.cpp
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;
}

0 comments on commit c0e5d31

Please sign in to comment.