fork(2) download
  1. #include <iostream>
  2.  
  3. enum ConvertType
  4. {
  5. Celsius = -1,
  6. Fahrenheit = -2
  7. };
  8.  
  9. class Converter
  10. {
  11. protected:
  12. ConvertType _type;
  13.  
  14. public:
  15. Converter( ConvertType t ) : _type( t ) {}
  16.  
  17. double Convert( double val )
  18. {
  19. if ( _type == Celsius ) // Convert to celsius
  20. return 1.8 * val + 32;;
  21. return ( val / 1.8 ) - 32;
  22. }
  23. };
  24.  
  25. static Converter CreateConverter(ConvertType type)
  26. {
  27. return Converter(type);
  28. }
  29.  
  30. static void DoConvertion(short choice)
  31. {
  32. using ct = ConvertType; // ct = ConvertType
  33. auto converter = CreateConverter( ( choice == 1 ? ct( -1 ) : ct( -2 ) ) ); // If choice == 1 convert to celcius, else convert to fahrenheit
  34. double temp = 0.0;
  35. std::cout << "Enter temperature: ";
  36. std::cin >> temp;
  37. std::cout << "Result: " << converter.Convert(temp) << std::endl;
  38. }
  39.  
  40. int main()
  41. {
  42. short choice = 1;
  43. std::cout << "Enter choice: ";
  44. std::cin >> choice;
  45. DoConvertion(choice);
  46. }
  47.  
Success #stdin #stdout 0s 3416KB
stdin
1
40
stdout
Enter choice: Enter temperature: Result: 104