forked from jieniyimiao/CppPrimer
-
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.
- Loading branch information
Showing
4 changed files
with
179 additions
and
0 deletions.
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,48 @@ | ||
// | ||
// ex7_41.cpp | ||
// Exercise 7.41 | ||
// | ||
// Created by pezy on 11/20/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @Brief implementation of class Sales_data | ||
// @See ex7_41.h | ||
|
||
#include "ex7_41.h" | ||
|
||
// constructor | ||
Sales_data::Sales_data(std::istream &is) : Sales_data() | ||
{ | ||
std::cout << "Sales_data(istream &is)" << std::endl; | ||
read(is, *this); | ||
} | ||
|
||
// member functions. | ||
Sales_data& Sales_data::combine(const Sales_data& rhs) | ||
{ | ||
units_sold += rhs.units_sold; | ||
revenue += rhs.revenue; | ||
return *this; | ||
} | ||
|
||
// friend functions | ||
std::istream &read(std::istream &is, Sales_data &item) | ||
{ | ||
double price = 0; | ||
is >> item.bookNo >> item.units_sold >> price; | ||
item.revenue = price * item.units_sold; | ||
return is; | ||
} | ||
|
||
std::ostream &print(std::ostream &os, const Sales_data &item) | ||
{ | ||
os << item.isbn() << " " << item.units_sold << " " << item.revenue; | ||
return os; | ||
} | ||
|
||
Sales_data add(const Sales_data &lhs, const Sales_data &rhs) | ||
{ | ||
Sales_data sum = lhs; | ||
sum.combine(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,58 @@ | ||
// | ||
// ex7_41.h | ||
// Exercise 7.41 | ||
// | ||
// Created by pezy on 11/20/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
// @See ex7_26.h | ||
// @Add 1. use delegating constructors | ||
// 2. add a statement to the body of each of the constructors that prints a message whether it is executed. | ||
|
||
#ifndef CP5_ex7_41_h | ||
#define CP5_ex7_41_h | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class Sales_data { | ||
friend std::istream &read(std::istream &is, Sales_data &item); | ||
friend std::ostream &print(std::ostream &os, const Sales_data &item); | ||
friend Sales_data add(const Sales_data &lhs, const Sales_data &rhs); | ||
|
||
public: | ||
Sales_data(const std::string &s, unsigned n, double p):bookNo(s),units_sold(n),revenue(n*p) | ||
{ std::cout << "Sales_data(const std::string&, unsigned, double)" << std::endl; } | ||
|
||
Sales_data() : Sales_data("", 0, 0.0f) | ||
{ std::cout << "Sales_data()" << std::endl; } | ||
|
||
Sales_data(const std::string &s) : Sales_data(s, 0, 0.0f) | ||
{ std::cout << "Sales_data(const std::string&)" << std::endl; } | ||
|
||
Sales_data(std::istream &is); | ||
|
||
std::string isbn() const { return bookNo; }; | ||
Sales_data& combine(const Sales_data&); | ||
|
||
private: | ||
inline double avg_price() const; | ||
|
||
private: | ||
std::string bookNo; | ||
unsigned units_sold = 0; | ||
double revenue = 0.0; | ||
}; | ||
|
||
inline | ||
double Sales_data::avg_price() const | ||
{ | ||
return units_sold ? revenue/units_sold : 0; | ||
} | ||
|
||
// declarations for nonmember parts of the Sales_data interface. | ||
std::istream &read(std::istream &is, Sales_data &item); | ||
std::ostream &print(std::ostream &os, const Sales_data &item); | ||
Sales_data add(const Sales_data &lhs, const Sales_data &rhs); | ||
|
||
#endif |
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,55 @@ | ||
// | ||
// ex7_41_TEST.cpp | ||
// Test of Exercise 7.41 | ||
// | ||
// Created by pezy on 11/14/14. | ||
// Copyright (c) 2014 pezy. All rights reserved. | ||
// | ||
|
||
#include "ex7_41.h" | ||
using std::cout; using std::endl; | ||
|
||
int main() | ||
{ | ||
cout << "1. default way: " << endl; | ||
cout << "----------------" << endl; | ||
Sales_data s1; | ||
|
||
cout << "\n2. use std::string as parameter: " << endl; | ||
cout << "----------------" << endl; | ||
Sales_data s2("CPP-Primer-5th"); | ||
|
||
cout << "\n3. complete parameters: " << endl; | ||
cout << "----------------" << endl; | ||
Sales_data s3("CPP-Primer-5th", 3, 25.8); | ||
|
||
cout << "\n4. use istream as parameter: " << endl; | ||
cout << "----------------" << endl; | ||
Sales_data s4(std::cin); | ||
|
||
return 0; | ||
} | ||
|
||
/* | ||
* 1. default way: | ||
* ---------------- | ||
* Sales_data(const std::string&, unsigned, double) | ||
* Sales_data() | ||
* | ||
* 2. use std::string as parameter: | ||
* ---------------- | ||
* Sales_data(const std::string&, unsigned, double) | ||
* Sales_data(const std::string&) | ||
* | ||
* 3. complete parameters: | ||
* ---------------- | ||
* Sales_data(const std::string&, unsigned, double) | ||
* | ||
* 4. use istream as parameter: | ||
* ---------------- | ||
* Sales_data(const std::string&, unsigned, double) | ||
* Sales_data() | ||
* Sales_data(istream &is) | ||
* | ||
*/ |