Guest User

Untitled

a guest
Sep 17th, 2020
889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int length = 3;
  6.  
  7. class Car
  8. {
  9.     private:
  10.         int num;
  11.         double gas;
  12.  
  13.     public:
  14.         Car();
  15.         void setCar(int,double);
  16.         void show();
  17. };
  18.  
  19. Car::Car()
  20. {
  21.     num = 0;
  22.     gas = 0.0;
  23.     cout<<"生產了汽車。\n";
  24. }
  25.  
  26. void Car::setCar(int n, double g)
  27. {
  28.     num = n;
  29.     gas = g;
  30.     cout<<"已使車號為"<<num<<",使汽油量為"<<gas<<"。\n";
  31. }
  32.  
  33. void Car::show()
  34. {
  35.     cout<<"車號是"<<num<<"。\n";
  36.     cout<<"汽油量是"<<gas<<"。\n";
  37. }
  38.  
  39. int main()
  40. {
  41.     // method 1
  42.     Car cars[length];
  43.  
  44.     cars[0].setCar(1234, 20.5);
  45.     cars[1].setCar(2345, 30.5);
  46.     cars[2].setCar(3456, 40.5);
  47.  
  48.     for(int i=0; i<length; i++)
  49.     {
  50.         cars[i].show();
  51.     }
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment