Guest User

Untitled

a guest
Sep 17th, 2020
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 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 4
  42.     Car** cars = new Car*[length];
  43.     for (int i=0; i<length; i++)
  44.     {
  45.         cars[i] = new Car();
  46.     }
  47.  
  48.     cars[0]->setCar(1234, 20.5);
  49.     cars[1]->setCar(2345, 30.5);
  50.     cars[2]->setCar(3456, 40.5);
  51.  
  52.     for(int i=0; i<length; i++)
  53.     {
  54.         cars[i]->show();
  55.     }
  56.  
  57.     return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment