diff options
-rw-r--r-- | 7p56.cpp | 15 | ||||
-rw-r--r-- | 7p57.cpp | 39 | ||||
-rw-r--r-- | 7p58.cpp | 38 | ||||
-rw-r--r-- | class-tests-main.cpp | 26 | ||||
-rw-r--r-- | class-tests.hpp | 78 |
5 files changed, 196 insertions, 0 deletions
diff --git a/7p56.cpp b/7p56.cpp new file mode 100644 index 0000000..bdddef9 --- /dev/null +++ b/7p56.cpp @@ -0,0 +1,15 @@ + +/* + * + * 7.56 + * + * + */ + +int main () { + + // A static member is shared across all objects of the same Class type. + // This means that if we change the static member, all classes will be + // see this change on the static member. + return 0; +} diff --git a/7p57.cpp b/7p57.cpp new file mode 100644 index 0000000..03be16f --- /dev/null +++ b/7p57.cpp @@ -0,0 +1,39 @@ +#include <iostream> + +/* + * + * 7.57 + * + * + */ + +class Account { +public: + Account(std::string o, double am): owner(o), amount(am) {} + Account(std::string o): Account(o, 0) {} + Account(): Account("John Doe", 0) {} + void calculate() { amount += amount * interestRate; } + static double rate() { return interestRate; } + static void rate(double); +private: + std::string owner; + double amount; + static double interestRate; + static double initRate(); +}; + +double Account::initRate() { + + return 1; +} + +double Account::interestRate = Account::initRate(); + +int main () { + + Account a1; + Account a2("Joe Johnsson"); + Account a3("Manny Manson", 1000); + std::cout << Account::rate() << std::endl; + return 0; +} diff --git a/7p58.cpp b/7p58.cpp new file mode 100644 index 0000000..ac94db8 --- /dev/null +++ b/7p58.cpp @@ -0,0 +1,38 @@ +#include <iostream> +#include <vector> + +/* + * + * 7.58 + * + * + */ + +class Example { +public: + static double rate; // Cannot be initialized here + static const int vecSize = 20; + static std::vector<double> vec; // Cannot be initialized here +}; + +// We initialize them here instead +double Example::rate = 20; +std::vector<double> Example::vec(vecSize); +int main () { + + /* + // example.h + class Example { + public: + static double rate = 6.5; + static const int vecSize = 20; + static vector<double> vec(vecSize); + }; + + // example.C + #include "example.h" + double Example::rate; + vector<double> Example::vec; + */ + return 0; +} diff --git a/class-tests-main.cpp b/class-tests-main.cpp new file mode 100644 index 0000000..86a289e --- /dev/null +++ b/class-tests-main.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include "class-tests.hpp" + +/* + * + * + * + * + */ + +int main () { + + CLASS_TESTS a; + a.Get_System_Info(); + a.print_CT(); + if(a.isFailed()) { + std::cout << "Fail." << std::endl; + return 1; + } + + if(a.isSuccess()) { + std::cout << "Success!" << std::endl; + } + + return 0; +} diff --git a/class-tests.hpp b/class-tests.hpp new file mode 100644 index 0000000..8ac10a2 --- /dev/null +++ b/class-tests.hpp @@ -0,0 +1,78 @@ +/* + Made this file mainly to just play around with classes and play around with the features. + No real use for this code. + */ +#ifndef CLASS_TESTS_HPP +#define CLASS_TESTS_HPP +#include <iostream> +#include <string> +#include <vector> +#include <sys/utsname.h> +#include <time.h> + +struct ERROR_RESULT { + friend class CLASS_TESTS; +private: + bool fail = false; + bool partial = false; + bool success = false; +}; + +class CLASS_TESTS { +public: + CLASS_TESTS(): Kernel(""), Distro_OS(""), EnvVarHOME(""), TotalMemoryBytes(0), CurTime(0) { } + CLASS_TESTS(std::string K, std::string D, std::string H, size_t B, time_t T): + Kernel(K), Distro_OS(D), EnvVarHOME(H), TotalMemoryBytes(B), CurTime(T){ } + void print_CT(); + ERROR_RESULT Get_System_Info(); + ERROR_RESULT Get_Errors() { return res; }; + bool isFailed() { return res.fail; } + bool isPartial() { return res.partial; } + bool isSuccess() { return res.success; } +private: + std::string Kernel; + std::string Distro_OS; + std::string EnvVarHOME; + size_t TotalMemoryBytes; + time_t CurTime; + ERROR_RESULT res; +}; + +void CLASS_TESTS::print_CT() { + std::cout << Kernel << "\n" + << Distro_OS << "\n" + << EnvVarHOME << "\n" + << TotalMemoryBytes << "\n" + << CurTime << std::endl; +} + +ERROR_RESULT CLASS_TESTS::Get_System_Info() { + + struct utsname gsi; + if(uname(&gsi) == -1) { + res.fail = true; + return res; + } + + Kernel = gsi.sysname; + Distro_OS = gsi.release; + char *r = std::getenv("HOME"); + if(r == NULL) { + res.fail = false; + res.partial = true; + return res; + } + + EnvVarHOME = r; + CurTime = time(NULL); + if(CurTime == -1) { + res.partial = true; + } + + TotalMemoryBytes = 4096; + res.partial = false; + res.success = true; + return res; +} + +#endif |