Advertisement
skb50bd

Distance(Class)(Data Convertion)

Jun 26th, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const float M2F = 3.28;
  6.  
  7. class Distance{
  8. private:
  9.     int feet;
  10.     float inches;
  11. public:
  12.     Distance(): feet(0), inches(0.0) {}
  13.     Distance(float meters){
  14.         float frac_feet = meters * M2F;
  15.         feet = frac_feet;
  16.         inches = (frac_feet - feet) * 12;
  17.     }
  18.  
  19.     ~Distance() {}
  20.  
  21.     operator float(){
  22.     return (feet + inches / 12) / M2F;
  23.     }
  24.  
  25.     void read() { cin >> feet >> inches;}
  26.     void show(){
  27.         cout.setf(ios::fixed);
  28.         cout << feet << " feet  " << setprecision(2) << inches << " inches" << endl;
  29.     }
  30. };
  31.  
  32.  
  33.  
  34. int main(){
  35.     cout.setf(ios::fixed);
  36.     float meters;
  37.     cout << "Enter METER value: ";
  38.     cin >> meters;
  39.     Distance D;
  40.     D = meters;
  41.     D.show();
  42.  
  43.     cout << "Enter FEET and INCHES: ";
  44.     D.read();
  45.     meters = D;
  46.     cout << meters << setprecision(2) << " meters" << endl;
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement