Skip to content

Instantly share code, notes, and snippets.

@danemo01
Created March 26, 2021 07:50
Show Gist options
  • Save danemo01/d8d8cba3d2dcc84450aebaab60c1b985 to your computer and use it in GitHub Desktop.
Save danemo01/d8d8cba3d2dcc84450aebaab60c1b985 to your computer and use it in GitHub Desktop.
Basic Singleton Solution
#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);
}
#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;
}
#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