From 672289a9cfd8366ae4ba17ec79575d8e762b75a1 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 25 Nov 2022 12:24:19 +0000 Subject: [PATCH] W7 Q1 --- main.cpp | 125 +++++++++++++++---------------------------------------- 1 file changed, 33 insertions(+), 92 deletions(-) diff --git a/main.cpp b/main.cpp index 0d73854..d55775a 100644 --- a/main.cpp +++ b/main.cpp @@ -2,100 +2,41 @@ #include #include -using string = std::string; -void ignoreTillLineEnd(); -string promptfs(string); -int promptfi(string); -string guessRank(int); +class A { +public: + A() { + std::cout << "Class A Created\n"; + } + ~A() { + std::cout << "Class A Destroyed\n"; + } +}; + +class B { +public: + B() { + std::cout << "Class B Created\n"; + } + ~B() { + std::cout << "Class B Destroyed\n"; + } +}; + +class C : public A { + B b{}; +public: + C() { + std::cout << "Class C Created\n"; + } + ~C() { + std::cout << "Class C Destroyed\n"; + } +}; int main() { - std::mt19937 rand {std::random_device{}()}; - std::uniform_int_distribution clamp {1, 100}; - string name {promptfs("Enter your name:")}; - std::cout << "Hello " << name << ", welcome to guess the number.\n"; - int guesses {}; - int num {clamp(rand)}, ent {}; - do { - if (guesses++ > 0) { - if (ent > num) - std::cout << "You Guessed Too High!\n"; - else - std::cout << "You Guessed Too Low!\n"; - } - ent = promptfi("Enter your guess:"); - } - while (num != ent); - std::cout << "Well done " << name << ", you guessed correctly!\n"; - std::cout << "You took " << guesses << " guesses, rank " << guessRank(guesses) << " .\n"; + A v{}; + B w{}; + C x{}; return 0; } - -void ignoreTillLineEnd() { - std::cin.ignore(std::numeric_limits::max(), '\n'); -} - -string promptfs(string msg) { - bool once {}; - string toret {}; - do { - if (once) { - std::cin.clear(); - ignoreTillLineEnd(); - std::cout << "Input Failure!\n"; - } - std::cout << msg << "\n"; - std::cin >> toret; - once = true; - } - while (std::cin.fail()); - ignoreTillLineEnd(); - return toret; -} - -int promptfi(string msg) { - bool once {}; - int toret {}; - do { - if (once) { - std::cin.clear(); - ignoreTillLineEnd(); - std::cout << "Input Failure!\n"; - } - std::cout << msg << "\n"; - std::cin >> toret; - once = true; - } - while (std::cin.fail()); - ignoreTillLineEnd(); - return toret; -} - -string guessRank(int g) { - switch (g) { - case 0: - return "U"; - case 1: - return "A^"; - case 2: - return "A*"; - case 3: - return "A"; - case 4: - case 5: - return "B"; - case 6: - case 7: - return "C"; - case 8: - case 9: - case 10: - return "D"; - case 11: - case 12: - case 13: - return "E"; - default: - return "F"; - } -}