home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CPTUTOR2.ZIP / ANSWERS.ARC / CH08_2.CPP < prev    next >
C/C++ Source or Header  |  1990-07-20  |  3KB  |  133 lines

  1.                              // Chapter 8 - Programming exercise 2
  2. #include "iostream.h"
  3.  
  4. class vehicle {
  5.    int wheels;
  6.    float weight;
  7. public:
  8.    vehicle(void) {wheels = 7; weight = 11111.0;
  9.                   cout << "Class vehicle\n";}
  10.    void initialize(int in_wheels, float in_weight);
  11.    int get_wheels(void) {return wheels;}
  12.    float get_weight(void) {return weight;}
  13.    float wheel_loading(void) {return weight/wheels;}
  14. };
  15.  
  16.  
  17.  
  18.  
  19. class car : public vehicle {
  20.    int passenger_load;
  21. public:
  22.    car(void) {passenger_load = 4;
  23.               cout << "Class car\n";}
  24.    void initialize(int in_wheels, float in_weight, int people = 4);
  25.    int passengers(void) {return passenger_load;}
  26. };
  27.  
  28.  
  29.  
  30.  
  31. class truck : public vehicle {
  32.    int passenger_load;
  33.    float payload;
  34. public:
  35.    truck(void) {passenger_load = 3; payload = 22222.0;
  36.                 cout << "Class truck\n";}
  37.    void init_truck(int how_many = 2, float max_load = 24000.0);
  38.    float efficiency(void);
  39.    int passengers(void) {return passenger_load;}
  40. };
  41.  
  42.  
  43.  
  44.  
  45. main()
  46. {
  47. vehicle unicycle;
  48.  
  49. // unicycle.initialize(1, 12.5);
  50.    cout << "The unicycle has " << 
  51.                                 unicycle.get_wheels() << " wheel.\n";
  52.    cout << "The unicycle's wheel loading is " << 
  53.          unicycle.wheel_loading() << " pounds on the single tire.\n";
  54.    cout << "The unicycle weighs " << 
  55.                              unicycle.get_weight() << " pounds.\n\n";
  56.  
  57. car sedan;
  58.  
  59. // sedan.initialize(4, 3500.0, 5);
  60.    cout << "The sedan carries " << sedan.passengers() << 
  61.                                                     " passengers.\n";
  62.    cout << "The sedan weighs " << sedan.get_weight() << " pounds.\n";
  63.    cout << "The sedan's wheel loading is " << 
  64.                     sedan.wheel_loading() << " pounds per tire.\n\n";
  65.  
  66. truck semi;
  67.  
  68. // semi.initialize(18, 12500.0);
  69. // semi.init_truck(1, 33675.0);
  70.    cout << "The semi weighs " << semi.get_weight() << " pounds.\n";
  71.    cout << "The semi's efficiency is " << 
  72.                           100.0 * semi.efficiency() << " percent.\n";
  73. }
  74.  
  75.  
  76.  
  77.  
  78.               // initialize to any data desired
  79. void
  80. vehicle::initialize(int in_wheels, float in_weight)
  81. {
  82.    wheels = in_wheels;
  83.    weight = in_weight;
  84. }
  85.  
  86.  
  87.  
  88. void
  89. car::initialize(int in_wheels, float in_weight, int people)
  90. {
  91.    passenger_load = people;
  92.    vehicle::initialize(in_wheels, in_weight);
  93. }
  94.  
  95.  
  96.  
  97.  
  98. void
  99. truck::init_truck(int how_many, float max_load)
  100. {
  101.    passenger_load = how_many;
  102.    payload = max_load;
  103. }
  104.  
  105.  
  106.  
  107. float
  108. truck::efficiency(void)
  109. {
  110.    return payload / (payload + get_weight());
  111. }
  112.  
  113.  
  114.  
  115.  
  116. // Result of execution
  117. //
  118. // Class vehicle
  119. // The unicycle has 7 wheel.
  120. // The unicycle's wheel loading is 1587.285767 pounds on the single tire.
  121. // The unicycle weighs 11111 pounds.
  122. //
  123. // Class vehicle
  124. // Class car
  125. // The sedan carries 4 passengers.
  126. // The sedan weighs 11111 pounds.
  127. // The sedan's wheel loading is 1587.285767 pounds per tire.
  128. //
  129. // Class vehicle
  130. // Class truck
  131. // The semi weighs 11111 pounds.
  132. // The semi's efficiency is 66.666669 percent.
  133.