thespeedracer38

Pure Virtual Function

Feb 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <typeinfo>
  4.  
  5. using namespace std;
  6.  
  7. class Unit{
  8.     protected:
  9.         double value;
  10.     public:
  11.         virtual double input() = 0;
  12.         virtual double convert() = 0;
  13.         virtual void display() = 0;
  14.         virtual double getValue() = 0;
  15. };
  16.  
  17. class Farenheit: public Unit{
  18.     public:
  19.         double input();
  20.         // Converts from Farenheit to Celsius
  21.         double convert();
  22.         void display();
  23.         double getValue();
  24. };
  25.  
  26. class Celsius: public Unit{
  27.     public:
  28.         double input();
  29.         // Converts from Celsius to Farenheit
  30.         double convert();
  31.         void display();
  32.         double getValue();
  33. };
  34.  
  35. class Litres: public Unit{
  36.     public:
  37.         double input();
  38.         // Converts from Litres to Galons
  39.         double convert();
  40.         void display();
  41.         double getValue();
  42. };
  43.  
  44. class Galons: public Unit{
  45.     public:
  46.         double input();
  47.         // Converts from Galons to Litres
  48.         double convert();
  49.         void display();
  50.         double getValue();
  51. };
  52.  
  53. double Farenheit::convert(){
  54.     return (5.0 / 9.0) * (value - 32);
  55. }
  56.  
  57. double Celsius::convert(){
  58.     return ((9.0 / 5.0) * value) + 32;
  59. }
  60.  
  61. double Litres::convert(){
  62.     return value / 3.875;
  63. }
  64.  
  65. double Galons::convert(){
  66.     return value * 3.875;
  67. }
  68.  
  69. double Farenheit::input(){
  70.     cout << "Enter value in F = ";
  71.     cin >> value;
  72. }
  73.  
  74. double Celsius::input(){
  75.     cout << "Enter value in C = ";
  76.     cin >> value;
  77. }
  78.  
  79. double Litres::input(){
  80.     cout << "Enter value in L = ";
  81.     cin >> value;
  82. }
  83.  
  84. double Galons::input(){
  85.     cout << "Enter value in G = ";
  86.     cin >> value;
  87. }
  88.  
  89. void Farenheit::display(){
  90.     cout << value << "F" << endl;
  91. }
  92.  
  93. void Celsius::display(){
  94.     cout << value << "C" << endl;
  95. }
  96.  
  97. void Litres::display(){
  98.     cout << value << "L" << endl;
  99. }
  100.  
  101. void Galons::display(){
  102.     cout << value << "G" << endl;
  103. }
  104.  
  105. double Farenheit::getValue(){
  106.     return value;
  107. }
  108.  
  109. double Celsius::getValue(){
  110.     return value;
  111. }
  112.  
  113. double Litres::getValue(){
  114.     return value;
  115. }
  116.  
  117. double Galons::getValue(){
  118.     return value;
  119. }
  120.  
  121. void add(Unit *val1, Unit *val2){
  122.     if(typeid(*val1) == typeid(*val2)){
  123.         cout << " ";
  124.         val1->display();
  125.         cout << "+";
  126.         val2->display();
  127.         cout << "=";
  128.         cout << val1->getValue() + val2->getValue() << endl;
  129.     }
  130.     else{
  131.         cout << " ";
  132.         val1->display();
  133.         cout << "+";
  134.         val2->display();
  135.         cout << "=";
  136.         cout << val1->convert() + val2->getValue() << endl;
  137.     }
  138. }
  139.  
  140. int main() {
  141.     system("cls");
  142.     Celsius cel;
  143.     Farenheit far;
  144.    
  145.     cel.input();
  146.     far.input();
  147.    
  148.     add(&cel, &far);
  149.     cout << endl;
  150.     add(&far, &cel);
  151.     cout << endl;
  152.     add(&far, &far);
  153.     cout << endl;
  154.     add(&cel, &cel);
  155.    
  156.     cout << endl;
  157.     system("pause");
  158.     return 0;
  159. }
Add Comment
Please, Sign In to add comment