Created
March 26, 2021 07:50
-
-
Save danemo01/d8d8cba3d2dcc84450aebaab60c1b985 to your computer and use it in GitHub Desktop.
Basic Singleton Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
double getFah(); | |
class Converter { | |
public: | |
static Converter* getInstance(); | |
float fromFtoC(float); | |
}; | |
int main() { | |
Converter c; | |
std::string name; | |
std::cout << "What is your name? "; | |
std::cin >> name; std::cin.ignore(80, '\n'); | |
std::cout << "Hello, " << name << "!\n"; | |
int temp; | |
std::cout << "Please enter a temperature. "; | |
double fah = getFah(); | |
std::cout << "Converted to Celsius, this temperature is " << c.fromFtoC(fah); | |
} | |
double getFah() { | |
double input; | |
while (!(std::cin >> input)) { | |
std::cout << "No letters please. try again: "; | |
std::cin.clear(); | |
std::cin.ignore(80, '\n'); | |
} | |
std::cin.ignore(80, '\n'); | |
return input; | |
} | |
float Converter::fromFtoC(float Fah) { | |
return (Fah-32) * (5.0/9.0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
/* | |
Danley Nemorin | |
CS410 - Software Engineering | |
A basic solution to the design of an singleton | |
*/ | |
class Converter { | |
public: | |
static Converter* getInstance(); | |
float fromFtoC(float); | |
~Converter(); | |
protected: | |
Converter() { }; | |
static Converter* instance; | |
}; | |
// Function prototype | |
double getFah(); | |
// Static gets assigned here; | |
Converter *Converter::instance = 0; | |
int main() { | |
std::string name; | |
std::cout << "What is your name? "; | |
std::cin >> name; std::cin.ignore(80, '\n'); | |
std::cout << "Hello, " << name << "!\n"; | |
int temp; | |
std::cout << "Please enter a temperature. "; | |
double fah = getFah(); | |
std::cout << "Converted to Celsius, this temperature is " << Converter::getInstance()->fromFtoC(fah); | |
} | |
// Function definition | |
double getFah() { | |
double input; | |
while (!(std::cin >> input)) { | |
std::cout << "No letters please. try again: "; | |
std::cin.clear(); | |
std::cin.ignore(80, '\n'); | |
} | |
std::cin.ignore(80, '\n'); | |
return input; | |
} | |
float Converter::fromFtoC(float Fah) { | |
return (Fah-32) * (5.0/9.0); | |
} | |
Converter* Converter::getInstance() { | |
if (!instance) // If instance is empty (0,NULL,nullptr) | |
instance = new Converter; // Allowed due to being within the Object Private Space | |
return instance; | |
} | |
Converter::~Converter() { | |
delete instance; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
double getFah(); | |
float fromFtoC(float); | |
int main() { | |
std::string name; | |
std::cout << "What is your name? "; | |
std::cin >> name; std::cin.ignore(80, '\n'); | |
std::cout << "Hello, " << name << "!\n"; | |
int temp; | |
std::cout << "Please enter a temperature. "; | |
double fah = getFah(); | |
std::cout << "Converted to Celsius, this temperature is " << fromFtoC(fah); | |
} | |
double getFah() { | |
double input; | |
while (!(std::cin >> input)) { | |
std::cout << "No letters please. try again: "; | |
std::cin.clear(); | |
std::cin.ignore(80, '\n'); | |
} | |
std::cin.ignore(80, '\n'); | |
return input; | |
} | |
float fromFtoC(float Fah) { | |
return (Fah-32) * (5.0/9.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment