diff options
-rw-r--r-- | 7p52.cpp | 27 | ||||
-rw-r--r-- | 7p53.cpp | 39 | ||||
-rw-r--r-- | 7p54.cpp | 19 | ||||
-rw-r--r-- | 7p55.cpp | 13 | ||||
-rw-r--r-- | constructor-constexpr-test.cpp | 40 |
5 files changed, 138 insertions, 0 deletions
diff --git a/7p52.cpp b/7p52.cpp new file mode 100644 index 0000000..be826e7 --- /dev/null +++ b/7p52.cpp @@ -0,0 +1,27 @@ +#include <iostream> + +/* + * + * 7.52 + * + * + */ + +struct Sales_data { + std::string bookNo; + unsigned units_sold = 0; + double revenue = 0.0; +}; + +int main () { + + Sales_data item = {"978-0590353403", 25, 15.99}; + std::cout << item.bookNo << "\n" + << item.units_sold << "\n" + << item.revenue << std::endl; + + // There seems to be no issues + // Apparently it refuses to compile if we were to use C++11 + // I am currently on C++20 + return 0; +} diff --git a/7p53.cpp b/7p53.cpp new file mode 100644 index 0000000..2c6c194 --- /dev/null +++ b/7p53.cpp @@ -0,0 +1,39 @@ +#include <iostream> + +/* + * + * 7.53 + * + * + */ + +class Debug { +public: + constexpr Debug(): hardware(false), inputoutput(false), other(false) {} + constexpr Debug(bool a, bool b, bool c): hardware(a), inputoutput(b), other(c) {} + constexpr void SetHW(bool a) { hardware = a; } + constexpr void SetIO(bool a) { inputoutput = a; } + constexpr void SetO(bool a) { other = a; } + constexpr bool GetHW() const { return hardware; } + constexpr bool GetIO() const { return inputoutput; } + constexpr bool GetO() const { return other; } +private: + bool hardware; + bool inputoutput; + bool other; +}; + +int main () { + + Debug D1; + Debug Derrors(true, true, true); + std::cout << D1.GetHW() << " " << D1.GetIO() << " " << D1.GetO() << std::endl; + D1.SetHW(true); + std::cout << D1.GetHW() << " " << D1.GetIO() << " " << D1.GetO() << std::endl; + std::cout << Derrors.GetHW() << " " << Derrors.GetIO() << " " << Derrors.GetO() << std::endl; + D1 = Derrors; + std::cout << D1.GetHW() << " " << D1.GetIO() << " " << D1.GetO() << std::endl; + constexpr Debug D2(true, false, true); + std::cout << D2.GetHW() << " " << D2.GetIO() << " " << D2.GetO() << std::endl; + return 0; +} diff --git a/7p54.cpp b/7p54.cpp new file mode 100644 index 0000000..d8606c4 --- /dev/null +++ b/7p54.cpp @@ -0,0 +1,19 @@ + +/* + * + * 7.54 + * + * + */ + +int main () { + + /* + Answer: Yes and No. + + C++11 did not allow this + When compiling with C++20, this is allowed. + + */ + return 0; +} diff --git a/7p55.cpp b/7p55.cpp new file mode 100644 index 0000000..c8a7f96 --- /dev/null +++ b/7p55.cpp @@ -0,0 +1,13 @@ + +/* + * + * 7.55 + * + * + */ + +int main () { + + // No + return 0; +} diff --git a/constructor-constexpr-test.cpp b/constructor-constexpr-test.cpp new file mode 100644 index 0000000..ee2a06e --- /dev/null +++ b/constructor-constexpr-test.cpp @@ -0,0 +1,40 @@ +#include <iostream> + +/* + * + * + * + * + */ + +class Debug { +public: + constexpr Debug(bool b = true): hw(b), io(b), other(b) { } + constexpr Debug(bool h, bool i, bool o): + hw(h), io(i), other(o) { } + + constexpr bool any() { return hw || io || other; } + void set_io(bool b) { io = b; } + void set_hw(bool b) { hw = b; } + void set_other(bool b) { hw = b; } + void printDbg() { std::cout << hw << "\n" << io << "\n" << other << std::endl; } +private: + bool hw; + // hardware errors other than IO errors + bool io; + // IO errors + bool other; // other errors +}; + +int main () { + + /* + From page 299-300, i just needed to confirm what the code is doing. + */ + Debug d1; + Debug d2(true, true, false); + d1.printDbg(); + std::cout << "------" << std::endl; + d2.printDbg(); + return 0; +} |