Advertisement
gha890826

C++ Class Practice

Apr 8th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. // ConsoleApplication1.cpp : 定義主控台應用程式的進入點。
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<iostream>
  6. using namespace std;
  7.  
  8. class Car
  9. {
  10. public:
  11.     void init(double, double);
  12.     double re_gas(){ return gas; };
  13.     double re_eff(){ return eff; };
  14.     void go(double);
  15. private:
  16.     double gas, eff;
  17. };
  18.  
  19. void Car::init(double E, double G)
  20. {
  21.     gas = G;
  22.     eff = E;
  23. }
  24.  
  25. void Car::go(double kilo)
  26. {
  27.     if (gas >= kilo/eff)
  28.     {
  29.         gas -= kilo / eff;
  30.         if (gas == 0)
  31.         {
  32.             cout << "油剛好用完了!請加油!\n";
  33.         }
  34.         else
  35.         {
  36.             cout << "跑完了 " << kilo << " 公里,油還剩下 " << gas << " 公升\n";
  37.         }
  38.     }
  39.     else
  40.     {
  41.         cout << "油量不夠!現在的油只夠跑 " << gas*eff << " 公里\n";
  42.     }
  43. }
  44.  
  45. void main()
  46. {
  47.     Car superone;
  48.     superone.init(20, 30);
  49.     cout << "一台超級車一公升可以跑" << superone.re_eff() << "公里\n"<<"現在有"<<superone.re_gas()<<"公升汽油";
  50.  
  51.     float length;
  52.     while (superone.re_gas() > 0)
  53.     {
  54.         cout << "請輸入要跑多遠:";
  55.         cin >> length;
  56.         superone.go(length);
  57.     }
  58.    
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement