============================MODULE15======================================= | | | The program examples' source codes have been arranged in the same | | order that appeared in the Tutorial. This is unedited and unverified | | compilation. Published as is basis for educational, reacretional and | | brain teaser purposes. All trademarks, copyrights and IPs, wherever | | exist, are the sole property of their respective owner and/or | | holder. Any damage or loss by using the materials presented in this | | tutorial is USER responsibility. Part or full distribution, | | reproduction and modification is granted to any body. | | Copyright 2003-2005 © Tenouk, Inc. All rights reserved. | | Distributed through http://www.tenouk.com | | | | | =========================================================================== Originally programs compiled using Borland C++. Examples compiled using VC++/VC++ .Net and gcc or g++ are given at the end of every Module. For example if you want to compile C++ codes using VC++/VC++ .Net, change the header file accordingly. Just need some modification for the header files...: ------------------------------------------------- #include //for system() #include ... { C++ codes... } ------------------------------------------------- should be changed to: ------------------------------------------------- #include //use C++ wrapper to call C functions from C++ programs... #include using namespace std; ... { C++ codes... } ------------------------------------------------- In VC++/VC++ .Net the iostream.h (header with .h) is not valid anymore. It should be C++ header, so that it comply to the standard. In older Borland C++ compiler this still works, but not proper any more... and for standard C/C++ the portability should be no problem or better you read Module23 at http://www.tenouk.com/Module23.html to get the big picture...For C codes, they still C codes :o) ========================================================================= ========================================================================= //Program inherit1.cpp. For VC++/VC++ .Net change the //change the header files accordingly... //#include //#include //using namespace std; #include #include //---class declaration part--- class vehicle { //This variable will be automatically //inherited by all the derived class but not outside the //base and derived class of vehicle protected: int wheels; double weight; public: void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; //---derived class declaration part--- class car : public vehicle { int passenger_load; public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) { return passenger_load; } }; class truck : public vehicle { int passenger_load; double payload; public: void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //----The main program------ int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"Using base class, vehicle\n"; cout<<"-------------------------\n"; cout<<"The unicycle has " < #include //-----base and derived class declaration part---- class vehicle { //protected: //Note this is removed, so it is private now int wheels; double weight; public: //public specifier void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; class car : public vehicle { int passenger_load; public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle { int passenger_load; double payload; public: void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //------main program------- int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"Using base class, vehicle\n"; cout<<"-------------------------\n"; cout<<"The unicycle has "< #include //------base and derived class declaration part----- class vehicle { protected: int wheels; double weight; public: void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; //public keyword changed to private - private inheritance class car : private vehicle { int passenger_load; public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; //public keyword change to private - private inheritance class truck : private vehicle { int passenger_load; double payload; public: void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //------main program------ int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"Using base class, vehicle with public methods\n"; cout<<"---------------------------------------------\n"; cout<<"The unicycle has "<sedan_car.initialize(4,3500.0,5)\n"; cout<<"and sedan_car.passengers()\n"; cout<<"-------------------------------------------------------\n"; cout<<"The sedan car carries "<trailer.init_truck(1, 33675.0),\n"; cout<<"trailer.efficiency() and trailer.passengers()\n"; cout<<"--------------------------------------------------\n"; cout<<"\nOthers are private...\n"; //methods get_weight() and efficiency() not available //because we use private inheritance //cout<<"The trailer weighs "< #include //----base and derived class declaration part----- class vehicle { protected: int wheels; double weight; public: void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; //public keyword changed to private - private inheritance class car : private vehicle { int passenger_load; public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; //public keyword change to private - private inheritance class truck : private vehicle { int passenger_load; double payload; public: void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //-----main program------ int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"Using base class, vehicle with public methods\n"; cout<<"---------------------------------------------\n"; cout<<"The unicycle has "<sedan_car.initialize(4,3500.0,5)\n"; cout<<"and sedan_car.passengers()\n"; cout<<"-------------------------------------------------------\n"; cout<<"The sedan car carries "<trailer.init_truck(1, 33675.0),\n"; cout<<"trailer.efficiency() and trailer.passengers()\n"; cout<<"--------------------------------------------------\n"; cout<<"\nOthers are private...\n"; //methods get_weight() and efficiency() not available //because we use private inheritance //cout<<"The trailer weighs "< #include //-----base and derived class declaration part----- class vehicle { protected: int wheels; double weight; public: vehicle(void) { wheels = 7; weight = 11111.0; cout<<"Constructor's value of the base class, vehicle"<<'\n'; cout<<"----------------------------------------------\n"; } void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; //public inheritance class car : public vehicle //public inheritance { int passenger_load; public: car(void) { passenger_load = 4; cout<<"Constructor's value of the derived class, car"<<'\n'; cout<<"---------------------------------------------\n"; } void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle //public inheritance { int passenger_load; double payload; public: truck(void) { passenger_load = 3;payload = 22222.0; cout<<"Constructor's value of the derived class, truck"<<'\n'; cout<<"-----------------------------------------------\n"; } void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //-------main program------- int main() { vehicle unicycle; //unicycle.initialize(1, 12.5); //use default constructor value, so no need the //initialization code for object unicycle anymore. cout<<"The unicycle has "< #include //-----base and derived class declaration part---- class vehicle { protected: int wheels; double weight; public: vehicle(void) { wheels = 7; weight = 11111.0; cout<<"It is me!, Constructor #1, own by base class"<<'\n'; } vehicle(int input_wheels, double input_weight) { wheels = input_wheels; weight = input_weight; cout<<"It is me!, Constructor #2, own by base class"<<'\n'; } void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; class car : public vehicle { int passenger_load; public: car(void) { passenger_load = 4; cout<<"It is me!, Constructor #3, derived class, car"<<'\n'; } car(int people, int input_wheels, double input_weight):vehicle(input_wheels, input_weight), passenger_load(people) { cout<<"It is me!, Constructor #4, derived class, car"<<'\n'; } void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle { int passenger_load; double payload; public: truck(void) { passenger_load = 3; payload = 22222.0; } //the following code should be in one line.... truck(int people, double load, int input_wheels, double weight):vehicle(input_wheels,input_weight),passenger_load(people), payload(load) { cout<<"It is me!, Constructor #5, derived class, car"<<'\n'; } void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //-----main program------ int main() { vehicle unicycle(1, 12.5); // unicycle.initialize(1, 12.5); cout<<"The unicycle has "< #include //-----base and derived class declaration part---- class vehicle { protected: int wheels; double weight; public: vehicle(void) { wheels = 7; weight = 11111.0; cout<<"Constructor #1, own by base class"<<'\n';} vehicle(int input_wheels, double input_weight) { wheels = input_wheels; weight = input_weight; cout<<"Constructor #2, own by base class"<<'\n';} void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; class car : public vehicle { int passenger_load; public: car(void) {passenger_load = 4; cout<<"Constructor #3, derived class, car"<<"\n\n";} car(int people, int input_wheels, double input_weight):vehicle(input_wheels, input_weight),passenger_load(people) {cout<<"Constructor #4 derived class, car"<<'\n'; } void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; class truck : public vehicle { int passenger_load; double payload; public: truck(void) {passenger_load = 3; payload = 22222.0;} truck(int people, double load, int input_wheels, double input_weight):vehicle(input_wheels, input_weight),passenger_load(people), payload(load) { } void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //-----main program----- int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"The unicycle has " <initialize(18, 12500.0); trailer->init_truck(1, 33675.0); cout<<"The trailer weighs " << trailer->get_weight()<<" kg.\n"; cout<<"The trailer's efficiency is "<<100.0 * trailer->efficiency()<<" %.\n"; delete trailer; //deallocate the object system("pause"); return 0; } //-----base and derived class implementation part---- // initialize to any data desired void vehicle::initialize(int input_wheels, double input_weight) { wheels = input_wheels; weight = input_weight; } void car::initialize(int input_wheels, double input_weight, int people) { passenger_load = people; wheels = input_wheels; weight = input_weight; } void truck::init_truck(int how_many, double max_load) { passenger_load = how_many; payload = max_load; } double truck::efficiency(void) { return (payload / (payload + weight)); } ------------------------------------------------------------------------------------------------- //Using friend function... #include #include class SampleFriend { //private member variable int i; friend int friend_funct(SampleFriend *, int); //friend_funct is not private, //even though it's declared in the private section public: //constructor SampleFriend(void) { i = 0;}; int member_funct(int); }; //implementation part, both functions access private int i int friend_funct(SampleFriend *xptr, int a) { return xptr->i = a; } int SampleFriend::member_funct(int a) { return i = a; } main() { SampleFriend xobj; //note the difference in function calls cout<<"\nfriend_funct(&xobj, 10) is "< #include class Base { //available for this class member functions ONLY... private: int BaseVar; int NewX; int ExtraBaseVar; //available to this and derived classes... protected: int BaseVarOne; //available to the derived and outside classes... public: Base(); Base(int NewX); ~Base(); public: int SetBaseData(); int ShowBaseData(){return BaseVar;} int SimilarNameFunct(); }; class DerivedOne:public Base { //available to this class member functions ONLY... private: int DerivedOneVar; int ExtraDerivedVar; //available to the derived and outside classes... public: DerivedOne(); ~DerivedOne(); //available to the derived and outside classes... public: void SetDerivedOneData(); int ShowDerivedOneData() { //BaseVarOne is base class protected member //variable, available to this derived class... return (DerivedOneVar + BaseVarOne); } int SimilarNameFunct(); }; //base class constructor... Base::Base() { BaseVar = 100; //constructor counter... static int p; cout<<"Invoking base class constructor #"< using namespace std; //-----base and derived class declaration part----- class vehicle { protected: int wheels; double weight; public: void initialize(int input_wheels, double input_weight); int get_wheels(void) {return wheels;} double get_weight(void) {return weight;} double wheel_load(void) {return (weight/wheels);} }; //public keyword changed to private - private inheritance class car : private vehicle { int passenger_load; public: void initialize(int input_wheels, double input_weight, int people = 4); int passengers(void) {return passenger_load;} }; //public keyword change to private - private inheritance class truck : private vehicle { int passenger_load; double payload; public: void init_truck(int how_many = 4, double max_load = 24000.0); double efficiency(void); int passengers(void) {return passenger_load;} }; //-----main program---- int main() { vehicle unicycle; unicycle.initialize(1, 12.5); cout<<"Using base class, vehicle with public methods\n"; cout<<"---------------------------------------------\n"; cout<<"The unicycle has "<sedan_car.initialize(4,3500.0,5)\n"; cout<<" and sedan_car.passengers()\n"; cout<<"-------------------------------------------------------\n"; cout<<"The sedan car carries "<trailer.init_truck(1, 33675.0),\n"; cout<<" trailer.efficiency() and trailer.passengers()\n"; cout<<"--------------------------------------------------\n"; cout<<"\nOthers are private...\n"; //methods get_weight() and efficiency() not available //because we use private inheritance //cout<<"The trailer weighs "< using namespace std; class Base { //available for this class member functions ONLY... private: int BaseVar; int NewX; int ExtraBaseVar; //available to this and derived classes... protected: int BaseVarOne; //available to the derived and outside classes... public: Base(); Base(int NewX); ~Base(); public: int SetBaseData(); int ShowBaseData(){return BaseVar;} int SimilarNameFunct(); }; class DerivedOne:public Base { //available to this class member functions ONLY... private: int DerivedOneVar; int ExtraDerivedVar; //available to the derived and outside classes... public: DerivedOne(); ~DerivedOne(); //available to the derived and outside classes... public: void SetDerivedOneData(); int ShowDerivedOneData() { //BaseVarOne is base class protected member //variable, available to this derived class... return (DerivedOneVar + BaseVarOne); } int SimilarNameFunct(); }; //base class constructor... Base::Base() { BaseVar = 100; //constructor counter... static int p; cout<<"Invoking base class constructor #"<