// composition.cc // we have covered inheritance, now we add composition // once students learn about inheritance, they tend to over use it. // it must be remembered that objects may be composed of other objects // composition has a different meaning and use than inheritance class door { // for a car door each usually a header file, e.g. door.h public: private: float width, height, mass; // etc. }; class engine { // for a car engine public: private: float horse_power, torque, mass; // etc. }; class hood { // for a car hood public: private: float width, height, mass; // etc. }; class wheel { // for a car wheel public: private: float diameter, width, mass; // etc. }; class transporter { // for a physical transporter public: float get_mass(transporter object); private: float mass; }; // should be abstract // #include "transporter" // all one file, so #include commented out class vehicle : public transporter { // for a physical vehicle // inherits "is a" transporter public: private: float new_variable; // and more } vehicle_1, vehicle_2; // can have objects //#include "vehicle.h" // all in this file //#include "door.h" //#include "engine.h" //#include "hood.h" //#include "wheel.h" class car : public vehicle { // for physical car // inherits "is a" vehicle public: private: door left_door; // composition, car "has a" door door right_door; engine v8; // conposition, car "has a" engine hood my_hood; // composition, car "has a" hood wheel lf,lr,rf,rr; // composition, car has wheels // multiple inheritance will not work to get four wheels! // vehicle a_vehicle; WRONG! by inheritance, car "is a" vehicle }; int main() { car c1, c2; // c1 and c2 are two objects, instances of class car }