summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar <[email protected]>2024-09-30 19:31:02 +0200
committerOskar <[email protected]>2024-09-30 19:31:02 +0200
commite0ffab6b379851aa573d7009fcf50e1be892f734 (patch)
tree7d725f15a645492f1b7dfc9c22c3f44cf83bb888
parentc2d59bb8a75c6228a91393b9cf3aedcb4d455218 (diff)
more
-rw-r--r--7p41.cpp74
-rw-r--r--7p42.cpp41
2 files changed, 115 insertions, 0 deletions
diff --git a/7p41.cpp b/7p41.cpp
new file mode 100644
index 0000000..12d0916
--- /dev/null
+++ b/7p41.cpp
@@ -0,0 +1,74 @@
+#include <iostream>
+
+/*
+ *
+ * 7.41
+ *
+ *
+ */
+
+struct SalesData;
+SalesData add(const SalesData&, const SalesData&);
+std::ostream &print(std::ostream&, const SalesData&);
+std::istream &read(std::istream&, SalesData&);
+struct SalesData {
+ SalesData(const std::string &s, unsigned n, double p): // constructor, both bookno, UnitsSold and Revenue
+ BookNo(s), UnitsSold(n), Revenue(p*n) { }
+ SalesData(): SalesData("", 0, 0) { std::cout << "delegated\n"; }
+ SalesData(const std::string &s): SalesData(s, 0, 0) { std::cout << "delegated\n"; }
+ SalesData(const std::string &s, unsigned ui): SalesData(s, ui, 0) { std::cout << "delegated\n"; }
+ SalesData(std::istream &sdcin): SalesData() { read(sdcin, *this); std::cout << "delegated\n"; }; // 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 () {
+
+ SalesData a;
+ SalesData b("WOW");
+ SalesData c("Woo", 23);
+ SalesData d("Woohoo", 33, 10);
+ SalesData e(std::cin);
+ return 0;
+}
diff --git a/7p42.cpp b/7p42.cpp
new file mode 100644
index 0000000..6f71c6a
--- /dev/null
+++ b/7p42.cpp
@@ -0,0 +1,41 @@
+#include <iostream>
+
+/*
+ *
+ * 7.40
+ *
+ *
+ */
+
+class Date {
+public:
+ Date(time_t et, time_t et_c): epochtime(et), epochcompare(et_c) {}
+ Date(): Date(0, 0) { std::cout << "delegated\n"; }
+ Date(time_t et): Date(et, 0) { std::cout << "delegated\n"; }
+ std::ostream& printepoch(std::ostream& pecout) {
+ pecout << epochtime;
+ return pecout;
+ }
+ time_t getepoch() { return epochtime; }
+ time_t getcompare() { return epochcompare; }
+ std::string& getformatted() {
+ formatted = "TEST"; // Just an example, we format the time here.
+ return formatted;
+ }
+
+private:
+ time_t epochtime;
+ time_t epochcompare;
+ std::string formatted;
+};
+
+int main () {
+
+ Date d1;
+ Date d2(342342423);
+ d1.printepoch(std::cout) << std::endl;
+ d2.printepoch(std::cout) << std::endl;
+ std::cout << d1.getepoch() << std::endl;
+ std::cout << d1.getformatted() << std::endl;
+ return 0;
+}