Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <typeinfo>
- using namespace std;
- class Unit{
- protected:
- double value;
- public:
- virtual double input() = 0;
- virtual double convert() = 0;
- virtual void display() = 0;
- virtual double getValue() = 0;
- };
- class Farenheit: public Unit{
- public:
- double input();
- // Converts from Farenheit to Celsius
- double convert();
- void display();
- double getValue();
- };
- class Celsius: public Unit{
- public:
- double input();
- // Converts from Celsius to Farenheit
- double convert();
- void display();
- double getValue();
- };
- class Litres: public Unit{
- public:
- double input();
- // Converts from Litres to Galons
- double convert();
- void display();
- double getValue();
- };
- class Galons: public Unit{
- public:
- double input();
- // Converts from Galons to Litres
- double convert();
- void display();
- double getValue();
- };
- double Farenheit::convert(){
- return (5.0 / 9.0) * (value - 32);
- }
- double Celsius::convert(){
- return ((9.0 / 5.0) * value) + 32;
- }
- double Litres::convert(){
- return value / 3.875;
- }
- double Galons::convert(){
- return value * 3.875;
- }
- double Farenheit::input(){
- cout << "Enter value in F = ";
- cin >> value;
- }
- double Celsius::input(){
- cout << "Enter value in C = ";
- cin >> value;
- }
- double Litres::input(){
- cout << "Enter value in L = ";
- cin >> value;
- }
- double Galons::input(){
- cout << "Enter value in G = ";
- cin >> value;
- }
- void Farenheit::display(){
- cout << value << "F" << endl;
- }
- void Celsius::display(){
- cout << value << "C" << endl;
- }
- void Litres::display(){
- cout << value << "L" << endl;
- }
- void Galons::display(){
- cout << value << "G" << endl;
- }
- double Farenheit::getValue(){
- return value;
- }
- double Celsius::getValue(){
- return value;
- }
- double Litres::getValue(){
- return value;
- }
- double Galons::getValue(){
- return value;
- }
- void add(Unit *val1, Unit *val2){
- if(typeid(*val1) == typeid(*val2)){
- cout << " ";
- val1->display();
- cout << "+";
- val2->display();
- cout << "=";
- cout << val1->getValue() + val2->getValue() << endl;
- }
- else{
- cout << " ";
- val1->display();
- cout << "+";
- val2->display();
- cout << "=";
- cout << val1->convert() + val2->getValue() << endl;
- }
- }
- int main() {
- system("cls");
- Celsius cel;
- Farenheit far;
- cel.input();
- far.input();
- add(&cel, &far);
- cout << endl;
- add(&far, &cel);
- cout << endl;
- add(&far, &far);
- cout << endl;
- add(&cel, &cel);
- cout << endl;
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment