Skip to content

Commit

Permalink
added 7.41~7.42
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Nov 20, 2014
1 parent 0970624 commit 713ae63
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
18 changes: 18 additions & 0 deletions ch07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,21 @@ private:
std::string pubdate_;
};
```
## Exercise 7.41 [Header](ex7_41.h)|[Cpp](ex7_41.cpp)|[Test](ex7_41_TEST.cpp)
## Exercise 7.42
```cpp
class Book {
public:
Book(unsigned no, std::string name, std::string author, std::string pubdate):no_(no), name_(name), author_(author), pubdate_(pubdate) { }
Book() : Book(0, "", "", "") { }
Book(std::istream &in) : Book() { in >> no_ >> name_ >> author_ >> pubdate_; }
private:
unsigned no_;
std::string name_;
std::string author_;
std::string pubdate_;
};
```
48 changes: 48 additions & 0 deletions ch07/ex7_41.cpp
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;
}
58 changes: 58 additions & 0 deletions ch07/ex7_41.h
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
55 changes: 55 additions & 0 deletions ch07/ex7_41_TEST.cpp
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;
}

// print
/*
* 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)
*
*/

0 comments on commit 713ae63

Please sign in to comment.