program, the objects are instantiated from an?inherited class and the intent of this program?
is to illustrate that there is nothing magic?about a derived class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | /******************************************************* * MYCPLUS Sample Code - https://www.mycplus.com * * * * This code is made available as a service to our * * visitors and is provided strictly for the * * purpose of illustration. * * * * Please direct all inquiries to saqib at mycplus.com * *******************************************************/ #include <iostream.h> class vehicle { protected: int wheels; float weight; public: void initialize(int in_wheels, float in_weight); int get_wheels(void) {return wheels;} float get_weight(void) {return weight;} float wheel_loading(void) {return weight/wheels;} }; class car : public vehicle { int passenger_load; public: void initialize(int in_wheels, float in_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle { int passenger_load; float payload; public: void init_truck(int how_many = 2, float max_load = 24000.0); float efficiency(void); int passengers(void) {return passenger_load;} }; main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout << "The unicycle has " << unicycle.get_wheels() << " wheel.\n"; cout << "The unicycle's wheel loading is " << unicycle.wheel_loading() << " pounds on the single tire.\n"; cout << "The unicycle weighs " << unicycle.get_weight() << " pounds.\n\n"; car sedan[3]; int index; for (index = 0 ; index < 3 ; index++) { sedan[index].initialize(4, 3500.0, 5); cout << "The sedan carries " << sedan[index].passengers() << " passengers.\n"; cout << "The sedan weighs " << sedan[index].get_weight() << " pounds.\n"; cout << "The sedan's wheel loading is " << sedan[index].wheel_loading() << " pounds per tire.\n\n"; } truck *semi; semi = new truck; semi->initialize(18, 12500.0); semi->init_truck(1, 33675.0); cout << "The semi weighs " << semi->get_weight() << " pounds.\n"; cout << "The semi's efficiency is " << 100.0 * semi->efficiency() << " percent.\n"; delete semi; } // initialize to any data desired void vehicle::initialize(int in_wheels, float in_weight) { wheels = in_wheels; weight = in_weight; } void car::initialize(int in_wheels, float in_weight, int people) { passenger_load = people; wheels = in_wheels; weight = in_weight; } void truck::init_truck(int how_many, float max_load) { passenger_load = how_many; payload = max_load; } float truck::efficiency(void) { return payload / (payload + weight); } // Result of execution // // The unicycle has 1 wheel. // The unicycle's wheel loading is 12.5 pounds on the single tire. // The unicycle weighs 12.5 pounds. // // The sedan carries 5 passengers. // The sedan weighs 3500 pounds. // The sedan's wheel loading is 875 pounds per tire. // // The sedan carries 5 passengers. // The sedan weighs 3500 pounds. // The sedan's wheel loading is 875 pounds per tire. // // The sedan carries 5 passengers. // The sedan weighs 3500 pounds. // The sedan's wheel loading is 875 pounds per tire. // // The semi weighs 12500 pounds. // The semi's efficiency is 72.929072 percent. |