Advertisement
TheWhiteFang

Tutorial 5 Section B

Nov 23rd, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. // http://pastebin.com/u/TheWhiteFang
  2. //tutorial 5 Section B
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. class Temperature
  8. {
  9. private:
  10. // Declare private data members here
  11.     double tempCel;
  12.     double tempFar;
  13.  
  14. public:
  15. Temperature(char name, double val){ // complete this constructor
  16.     tempCel = 0;
  17.     tempFar = 32;
  18.  
  19.     if (name == 'c'|| name == 'C'){
  20.  
  21.     tempCel = val;
  22.     tempFar = (9.0/5.0)*tempCel + 32;
  23.     }
  24.  
  25.     else if(name == 'f' || name == 'F'){
  26.     tempFar = val;
  27.     tempCel = (tempFar - 32)/(9.0/5.0); //need to add .0 since double
  28.     }
  29.  
  30.         else{
  31.         cout << "Enter a valid value: ";
  32.    
  33.         }
  34.  
  35. }
  36.  
  37.  
  38.  
  39. double getCels(){
  40.  
  41.  
  42.     return tempCel;
  43. }
  44.  
  45. double getFar(){
  46.  
  47.     return tempFar;  
  48.  
  49. }
  50.  
  51.  
  52. // Declare/define other public members/functions here,
  53. // as per the instructions given in the abovementioned
  54. // question.
  55. };
  56. // Driver program
  57. int main()
  58. {
  59.     Temperature t1('C',100),t2('F',100);
  60.     cout<<"t1 = "<< t1.getCels()<<" C\n";
  61.     cout<<"t1 = "<< t1.getFar()<<" F\n";
  62.     cout<<"t2 = "<<t2.getCels()<<" C\n";
  63.     cout<<"t2 = "<<t2.getFar()<<" F\n";
  64.     t1 = t2;
  65.     cout<<"After assigning t2 to t1"<<endl;
  66.     cout<<"t1 = "<<t1.getCels()<<" C\n";
  67.     cout<<"t1 = "<<t1.getFar()<<" F\n";
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement