Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- const int length = 3;
- class Car
- {
- private:
- int num;
- double gas;
- public:
- Car();
- void setCar(int,double);
- void show();
- };
- Car::Car()
- {
- num = 0;
- gas = 0.0;
- cout<<"生產了汽車。\n";
- }
- void Car::setCar(int n, double g)
- {
- num = n;
- gas = g;
- cout<<"已使車號為"<<num<<",使汽油量為"<<gas<<"。\n";
- }
- void Car::show()
- {
- cout<<"車號是"<<num<<"。\n";
- cout<<"汽油量是"<<gas<<"。\n";
- }
- int main()
- {
- // method 4
- Car** cars = new Car*[length];
- for (int i=0; i<length; i++)
- {
- cars[i] = new Car();
- }
- cars[0]->setCar(1234, 20.5);
- cars[1]->setCar(2345, 30.5);
- cars[2]->setCar(3456, 40.5);
- for(int i=0; i<length; i++)
- {
- cars[i]->show();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment