summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--7p43.cpp29
-rw-r--r--7p44.cpp30
-rw-r--r--7p45.cpp13
-rw-r--r--7p46.cpp15
-rw-r--r--7p47.cpp13
-rw-r--r--7p48.cpp75
-rw-r--r--7p49.cpp103
-rw-r--r--7p50.cpp11
-rw-r--r--7p51.cpp14
-rw-r--r--person.hpp3
10 files changed, 305 insertions, 1 deletions
diff --git a/7p43.cpp b/7p43.cpp
new file mode 100644
index 0000000..fb1b86b
--- /dev/null
+++ b/7p43.cpp
@@ -0,0 +1,29 @@
+#include <iostream>
+
+/*
+ *
+ * 7.43
+ *
+ *
+ */
+
+class NoDefault {
+public:
+ NoDefault(int j): i(j) {}
+ int geti() {return i;}
+private:
+ int i;
+};
+
+class C {
+public:
+ C(): nd(0) { }
+private:
+ NoDefault nd;
+};
+
+int main () {
+
+ C c;
+ return 0;
+}
diff --git a/7p44.cpp b/7p44.cpp
new file mode 100644
index 0000000..925f7d6
--- /dev/null
+++ b/7p44.cpp
@@ -0,0 +1,30 @@
+#include <iostream>
+#include <vector>
+/*
+ *
+ * 7.44
+ *
+ *
+ */
+
+class NoDefault {
+public:
+ NoDefault(int j): i(j) {}
+ int geti() {return i;}
+private:
+ int i;
+};
+
+class C {
+public:
+ C(): nd(0) { }
+private:
+ NoDefault nd;
+};
+
+int main () {
+
+ //std::vector<NoDefault> vnd(10); // NOT legal
+ std::vector<C> vc(10); // LEGAL
+ return 0;
+}
diff --git a/7p45.cpp b/7p45.cpp
new file mode 100644
index 0000000..f6569bb
--- /dev/null
+++ b/7p45.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 7.45
+ *
+ *
+ */
+
+int main () {
+
+ // Done in 7.44
+ return 0;
+}
diff --git a/7p46.cpp b/7p46.cpp
new file mode 100644
index 0000000..c835765
--- /dev/null
+++ b/7p46.cpp
@@ -0,0 +1,15 @@
+
+/*
+ *
+ * 7.46
+ *
+ *
+ */
+
+int main () {
+
+ /*
+
+ */
+ return 0;
+}
diff --git a/7p47.cpp b/7p47.cpp
new file mode 100644
index 0000000..6a9a687
--- /dev/null
+++ b/7p47.cpp
@@ -0,0 +1,13 @@
+
+/*
+ *
+ * 7.47
+ *
+ *
+ */
+
+int main () {
+
+ // I think it should be explicit so that we can make sure we only use the class type which will make sure the correct constructor is used.
+ return 0;
+}
diff --git a/7p48.cpp b/7p48.cpp
new file mode 100644
index 0000000..73ba879
--- /dev/null
+++ b/7p48.cpp
@@ -0,0 +1,75 @@
+#include <iostream>
+
+/*
+ *
+ * 7.48
+ *
+ *
+ */
+
+struct SalesData;
+SalesData add(const SalesData&, const SalesData&);
+std::ostream &print(std::ostream&, const SalesData&);
+std::istream &read(std::istream&, SalesData&);
+struct SalesData {
+ SalesData() = default;
+ explicit SalesData(const std::string &s): BookNo(s) { } //constructor, bookNo only
+ SalesData(const std::string &s, unsigned n, double p): // constructor, both bookno, UnitsSold and Revenue
+ BookNo(s), UnitsSold(n), Revenue(p*n) { }
+ explicit SalesData(std::istream &sdcin) { read(sdcin, *this); }; // Constructor, but read from istream
+ std::string isbn() const { return BookNo; } // member function
+ SalesData &combine(const SalesData&); // member function, defined elsewhere
+ inline double avg_price() const; // member function, defined elsewhere
+ std::string BookNo; // data member
+ unsigned int UnitsSold = 0; // data member
+ double Revenue = 0.0; // data member
+};
+
+inline double SalesData::avg_price() const {
+
+ if (UnitsSold)
+ return Revenue / UnitsSold;
+ else
+ return 0;
+}
+
+SalesData& SalesData::combine(const SalesData &rhs) {
+
+ UnitsSold += rhs.UnitsSold;
+ Revenue += rhs.Revenue;
+ return *this;
+}
+
+std::istream &read(std::istream &is, SalesData &item) { // Not member function
+
+ double price = 0;
+ is >> item.BookNo >> item.UnitsSold >> price;
+ item.Revenue = price * item.UnitsSold;
+ return is;
+}
+
+std::ostream &print(std::ostream &os, const SalesData &item) { // Not member function
+
+ os << item.isbn() << " " << item.UnitsSold << " "
+ << item.Revenue << " " << item.avg_price();
+ return os;
+}
+
+SalesData add(const SalesData sd1, const SalesData sd2) { // Not member function
+
+ SalesData sdsum = sd1;
+ sdsum.combine(sd2);
+ return sdsum;
+}
+
+int main () {
+
+ // Seems like the same thing happens for explicit and non-explicit
+
+ std::string null_isbn("9-999-99999-9");
+ SalesData i1(null_isbn);
+ SalesData i2("9-999-99999-9");
+ print(std::cout, i1) << "\n";
+ print(std::cout, i2) << std::endl;
+ return 0;
+}
diff --git a/7p49.cpp b/7p49.cpp
new file mode 100644
index 0000000..3cf1c09
--- /dev/null
+++ b/7p49.cpp
@@ -0,0 +1,103 @@
+#include <iostream>
+
+/*
+ *
+ * 7.49
+ *
+ *
+ */
+
+struct SalesData;
+SalesData add(const SalesData&, const SalesData&);
+std::ostream &print(std::ostream&, const SalesData&);
+std::istream &read(std::istream&, SalesData&);
+struct SalesData {
+ SalesData() = default;
+ SalesData(const std::string &s): BookNo(s) { } //constructor, bookNo only
+ SalesData(const std::string &s, unsigned n, double p): // constructor, both bookno, UnitsSold and Revenue
+ BookNo(s), UnitsSold(n), Revenue(p*n) { }
+ SalesData(std::istream &sdcin) { read(sdcin, *this); }; // Constructor, but read from istream
+ std::string isbn() const { return BookNo; } // member function
+
+ //-------
+ SalesData &combine(SalesData);
+ //SalesData &combine(SalesData&);
+ //SalesData &combine(const SalesData&) const; Does not compile
+ //-------
+
+ inline double avg_price() const; // member function, defined elsewhere
+ std::string BookNo; // data member
+ unsigned int UnitsSold = 0; // data member
+ double Revenue = 0.0; // data member
+};
+
+inline double SalesData::avg_price() const {
+
+ if (UnitsSold)
+ return Revenue / UnitsSold;
+ else
+ return 0;
+}
+
+SalesData& SalesData::combine(SalesData rhs) {
+
+ UnitsSold += rhs.UnitsSold;
+ Revenue += rhs.Revenue;
+ return *this;
+}
+
+/*
+SalesData& SalesData::combine(SalesData &rhs) {
+
+ UnitsSold += rhs.UnitsSold;
+ Revenue += rhs.Revenue;
+ return *this;
+}
+*/
+/*
+SalesData& SalesData::combine(const SalesData &rhs) const {
+
+ UnitsSold += rhs.UnitsSold;
+ Revenue += rhs.Revenue;
+ return *this;
+}
+*/
+
+std::istream &read(std::istream &is, SalesData &item) { // Not member function
+
+ double price = 0;
+ is >> item.BookNo >> item.UnitsSold >> price;
+ item.Revenue = price * item.UnitsSold;
+ return is;
+}
+
+std::ostream &print(std::ostream &os, const SalesData &item) { // Not member function
+
+ os << item.isbn() << " " << item.UnitsSold << " "
+ << item.Revenue << " " << item.avg_price();
+ return os;
+}
+
+SalesData add(const SalesData sd1, const SalesData sd2) { // Not member function
+
+ SalesData sdsum = sd1;
+ sdsum.combine(sd2);
+ return sdsum;
+}
+
+int main () {
+
+ /*
+ For each of the three following declarations of combine, explain what
+ happens if we call i.combine(s), where i is a Sales_data and s is a string:
+
+ (a) SalesData &combine(Sales_data); It does not combine as expected, i'm not sure.
+ (b) Sales_data &combine(Sales_data&); // does not compile
+ (c) Sales_data &combine(const Sales_data&) const; // does not compile
+ */
+ std::string s = "7.49";
+ SalesData i("",0,0);
+ i.combine(SalesData(s));
+ print(std::cout, i);
+ return 0;
+}
diff --git a/7p50.cpp b/7p50.cpp
new file mode 100644
index 0000000..f771102
--- /dev/null
+++ b/7p50.cpp
@@ -0,0 +1,11 @@
+
+/*
+ *
+ * 7.50
+ *
+ * Done in person.hpp
+ */
+
+int main () {
+ return 0;
+}
diff --git a/7p51.cpp b/7p51.cpp
new file mode 100644
index 0000000..e000343
--- /dev/null
+++ b/7p51.cpp
@@ -0,0 +1,14 @@
+
+/*
+ *
+ * 7.51
+ *
+ *
+ */
+
+int main () {
+
+ // Because we are specifying the size of the vector with that constructor
+ // String can be constructed with C strings
+ return 0;
+}
diff --git a/person.hpp b/person.hpp
index 29d0150..93223f3 100644
--- a/person.hpp
+++ b/person.hpp
@@ -2,10 +2,11 @@
#define PERSON_H
#include <string>
#include <vector>
+#include <iostream>
struct Person {
Person() = default;
- Person(const std::string &name): Name(name) {}
+ explicit Person(const std::string &name): Name(name) {}
Person(const std::string &name, const std::string &addr): Name(name), Address(addr) {}
std::string Name;
std::string Address;