home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / tutorial / cpptutor / source / inherit1.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-15  |  2.7 KB  |  126 lines

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