============================MODULE16======================================= | | | 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 mulinher1.cpp. For VC++/VC++ .Net or other C++ //standard compliance compiler, change the header files accordingly... //#include //#include //using namespace std; #include #include //-----class declaration and implementation part------- //-----base class number one-------- class moving_van { protected: float payload; float gross_weight; float mpg; //this variable only available for derived class //because of the protected keyword public: void initialize(float pl, float gw, float input_mpg) { payload = pl; gross_weight = gw; mpg = input_mpg; }; float efficiency(void) { return (payload / (payload + gross_weight)); }; float cost_per_ton(float fuel_cost) { return (fuel_cost / (payload / 2000.0)); }; }; //-----base class number two--- class driver { protected: float hourly_pay; public: void initialize(float pay) {hourly_pay = pay; }; float cost_per_mile(void) {return (hourly_pay / 55.0); } ; }; //-----derived class-------- //inherit from two different base classes class driven_truck : public moving_van, public driver { public: void initialize_all(float pl, float gw, float input_mpg, float pay) { payload = pl; gross_weight = gw; mpg = input_mpg; hourly_pay = pay; }; float cost_per_full_day(float cost_of_gas) { return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg); }; }; //-----main program---- int main() { //instantiate an object… driven_truck john_merc; john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50); cout<<"The efficiency of the Merc truck is "< #include //-----declaration and implementation class part------ //-----base class number one----- class moving_van { protected: float payload; float gross_weight; float mpg; public: void initialize(float pl, float gw, float input_mpg) { payload = pl; gross_weight = gw; mpg = input_mpg; }; float efficiency(void) { return (payload / (payload + gross_weight)); }; float cost_per_ton(float fuel_cost) { return (fuel_cost / (payload / 2000.0)); }; float cost_per_full_day(float cost_of_gas) //number one { return (8.0 * cost_of_gas * 55.0 / mpg); }; }; //-------base class number two------- class driver { protected: float hourly_pay; public: //same method name as in moving van class… void initialize(float pay) {hourly_pay = pay; }; float cost_per_mile(void) {return (hourly_pay / 55.0); } ; float cost_per_full_day(float overtime_premium) //number two {return (8.0 * hourly_pay); }; }; //------derived class------ //notice also the duplicated method names used class driven_truck : public moving_van, public driver { public: void initialize_all(float pl, float gw, float input_mpg, float pay) { payload = pl; gross_weight = gw; mpg = input_mpg; hourly_pay = pay; }; float cost_per_full_day(float cost_of_gas) //number three { return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg); }; }; //------main program------- int main() { driven_truck john_merc; john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50); cout<<"The efficiency of the Merc is "< #include //------class declaration and implementation part-------- //------class number one----------- class moving_van { protected: float payload; float weight; //note this variable float mpg; public: void initialize(float pl, float gw, float input_mpg) { payload = pl; weight = gw; mpg = input_mpg; }; float efficiency(void) { return(payload / (payload + weight)); }; float cost_per_ton(float fuel_cost) { return (fuel_cost / (payload / 2000.0)); }; }; //-----class number two----- class driver { protected: float hourly_pay; float weight; //another weight variable //variable with same name as in class number one public: void initialize(float pay, float input_weight) //same method name but different number of parameter { hourly_pay = pay; weight = input_weight; }; float cost_per_mile(void) {return (hourly_pay / 55.0); } ; float drivers_weight(void) {return (weight); }; }; //-----------derived class with multi inheritance--------- //---------declaration and implementation----------------- class driven_truck : public moving_van, public driver { public: void initialize_all(float pl, float gw, float input_mpg, float pay) //another same method name but different number of parameter { payload = pl; moving_van::weight = gw; mpg = input_mpg; hourly_pay = pay; }; float cost_per_full_day(float cost_of_gas) { return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg); }; float total_weight(void) //see, how to call different variables with same name { cout<<"\nCalling appropriate member variable\n"; cout<<"---->(moving_van::weight)+(driver::weight)\n"; cout<<"------------------------------------------\n"; return ((moving_van::weight) + (driver::weight)); }; }; //----the main program------- int main() { driven_truck john_merc; john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50); //accessing the derived class method john_merc.driver::initialize(15.50, 250.0); //accessing the base class number two cout<<"The efficiency of the Merc is "< #include //-----template declaration------- template ANY_TYPE maximum(ANY_TYPE a, ANY_TYPE b) { return (a > b) ? a : b; } //------main program------- int main(void) { int x = 10, y = -9; float real = 3.1415; char ch = 'C'; cout<<"maximum("< #include const int MAXSIZE = 128; //----class template------ template class stack { ANY_TYPE array[MAXSIZE]; int stack_pointer; public: stack(void) { stack_pointer = 0; }; void push(ANY_TYPE input_data){ array[stack_pointer++] = input_data; }; ANY_TYPE pop(void) { return array[--stack_pointer]; }; int empty(void) { return (stack_pointer == 0); }; }; char name[] = "Testing, this is an array, name[]"; //------main program------- int main(void) { int x = 30, y = -10; float real = 4.2425; stack int_stack; stack float_stack; stack string_stack; //storing data int_stack.push(x); int_stack.push(y); int_stack.push(67); float_stack.push(real); float_stack.push(-20.473); float_stack.push(107.03); string_stack.push("This is the first line of string"); string_stack.push("This is the second line of string"); string_stack.push("This is the third line of string"); string_stack.push(name); //displaying data cout<<"---------------Displaying data--------------------\n"; cout<<"\nInteger stack\n"; cout<<"-------------\n"; cout<<"Access using int_stack.pop(), first time : "< #include //---class declaration part--- //base class.... class MyFather { protected: char* EyeColor; char* HairType; double FamSaving; int FamCar; public: MyFather(){} ~MyFather(){} char* ShowEyeColor(); char* ShowHairType(); long double FamilySaving(); int FamilyCar(); }; //another base class... class MyMother { //notice the same member variables names //as in MyFather class... protected: char* EyeColor; char* HairType; int FamHouse; public: MyMother(){} ~MyMother(){} char* ShowMotherEye(); char* ShowMotherHair(); int FamilyHouse(); }; //single inheritance derived class... //aaahhh!!! my class :-) finally!!! class MySelf:public MyFather { //another member variables with same names... char* HairType; char* Education; public: MySelf(){} ~MySelf(){} char* ShowMyHair(); char* ShowMyEducation(); }; //multiple inheritance derived class... //notice the keyword public must follow every //parent class list as needed... class MySister:public MyFather,public MyMother { char* SisEye; float MonAllowance; public: MySister(){} ~MySister(){} char* ShowSisEye(); float ShowSisAllownace(); }; //-----class implementation part----- char* MyFather::ShowEyeColor() {return EyeColor = "Brown";} char* MyFather::ShowHairType() {return HairType = "Bald";} long double MyFather::FamilySaving() {return FamSaving = 100000L;} int MyFather::FamilyCar() {return FamCar = 4;} char* MyMother::ShowMotherEye() {return EyeColor = "Blue";} char* MyMother::ShowMotherHair() {return HairType = "Curly Blonde";} int MyMother::FamilyHouse() {return FamHouse = 3;} char* MySelf::ShowMyHair() {return HairType = "Straight Black";} char* MySelf::ShowMyEducation() {return Education = "Post Graduate";} char* MySister::ShowSisEye() {return SisEye = "Black";} float MySister::ShowSisAllownace() {return MonAllowance = 1000.00;} //------main program------ int main() { //instatiate the objects... MyFather ObjFat; MyMother ObjMot; MySelf ObjSelf; MySister ObjSis; cout<<"--My father's data--"< #include //-------class declaration and implementation------- //-------part------- //base class... class Base1 { protected: int SampleDataOne; public: Base1(){SampleDataOne = 100;} ~Base1(){} int SampleFunctOne() {return SampleDataOne;} }; //another base class... class Base2 { protected: int SampleDataTwo; public: Base2(){SampleDataTwo = 200;} ~Base2(){} int SampleFunctTwo() {return SampleDataTwo;} }; //derived class... class Derived1:public Base1,public Base2 { int MyData; public: Derived1(){MyData = 300;} ~Derived1(){} int MyFunct() { //the protected data of the base classes are available //for this derived class... return (MyData + SampleDataOne + SampleDataTwo); } }; //----main program------ int main() { //instantiate objects... Base1 SampleObjOne; Base2 SampleObjTwo; Derived1 SampleObjThree; cout<<"Normal access Base1 class data: "< using namespace std; //------class declaration and implementation part-------- //------class number one----------- class moving_van { protected: double payload; double weight; //note this variable double mpg; public: void initialize(double pl, double gw, double input_mpg) { payload = pl; weight = gw; mpg = input_mpg; }; double efficiency(void) { return(payload / (payload + weight)); }; double cost_per_ton(double fuel_cost) { return (fuel_cost / (payload / 2000.0)); }; }; //-----class number two------------ class driver { protected: double hourly_pay; double weight; //another weight variable //variable with same name as in class number one public: void initialize(double pay, double input_weight) //same method name but different number of parameter { hourly_pay = pay; weight = input_weight; }; double cost_per_mile(void) {return (hourly_pay / 55.0); }; double drivers_weight(void) {return (weight); }; }; //-----------derived class with multi inheritance--------- //---------declaration and implementation----------------- class driven_truck : public moving_van, public driver { public: void initialize_all(double pl, double gw, double input_mpg, double pay) //another same method name but different number of parameter { payload = pl; moving_van::weight = gw; mpg = input_mpg; hourly_pay = pay; }; double cost_per_full_day(double cost_of_gas) { return ((8.0 * hourly_pay) + (8.0 * cost_of_gas * 55.0) / mpg); }; double total_weight(void) //see, how to call different variables with same name { cout<<"\nCalling appropriate member variable\n"; cout<<"---->(moving_van::weight)+(driver::weight)\n"; cout<<"------------------------------------------\n"; return ((moving_van::weight) + (driver::weight)); }; }; //----the main program----- int main() { driven_truck john_merc; john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50); //accessing the derived class method john_merc.driver::initialize(15.50, 250.0); //accessing the base class number two cout<<"The efficiency of the Merc is "< using namespace std; //-----declaration and implementation class part------ //-----base class number one----- class moving_van { protected: float payload; float gross_weight; float mpg; public: void initialize(float pl, float gw, float input_mpg) { payload = pl; gross_weight = gw; mpg = input_mpg; }; float efficiency(void) {return (payload / (payload + gross_weight));}; float cost_per_ton(float fuel_cost) {return (fuel_cost / (payload / 2000.0));}; float cost_per_full_day(float cost_of_gas) //number one {return (5.5 * cost_of_gas * 55.0 / mpg);}; }; //-------base class number two------- class driver { protected: float hourly_pay; public: //same method name as in moving van class. void initialize(float pay) {hourly_pay = pay;}; float cost_per_mile(void) {return (hourly_pay / 55.0); } ; float cost_per_full_day(float overtime_premium) //number two {return (7.0 * hourly_pay); }; }; //-----derived class------ //notice also the duplicated method names used class driven_truck : public moving_van, public driver { public: void initialize_all(float pl, float gw, float input_mpg, float pay) { payload = pl; gross_weight = gw; mpg = input_mpg; hourly_pay = pay; }; float cost_per_full_day(float cost_of_gas) //number three { return ((7.0 * hourly_pay) + (5.5 * cost_of_gas * 55.0) / mpg); }; }; //-------main program----- int main() { driven_truck john_merc; john_merc.initialize_all(20000.0, 12000.0, 5.2, 12.50); cout<<"Merc's efficiency is "<