===========================MODULE14======================================== | | | 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) ========================================================================= ========================================================================= //class declaration part, vehicle.h, header file //save and include this file in your project //do not compile or run #ifndef VEHICLE_H //preprocessor directive #define VEHICLE_H //----class declaration part – the interface----- class Cvehicle { protected: //new keyword int wheels; int weight; public: void initialize(int input_wheels, float input_weight); int get_wheels(void); float get_weight(void); float wheel_load (void); }; #endif ------------------------------------------------------------------------------------------- //Program vehicle.cpp, implementation part, //compile without error, generating object file, do not run #include "vehicle.h" //-----class implementation part------ //initialize to data input by user void Cvehicle::initialize(int input_wheels, float input_weight) { wheels = input_wheels; weight = input_weight; } //get the number of wheels of this vehicle int Cvehicle::get_wheels() { return wheels; } //return the weight of this vehicle float Cvehicle::get_weight() { return weight; } //return the load on each wheel float Cvehicle::wheel_load() { return (weight/wheels); } ----------------------------------------------------------------------------------------------- //program transprt.cpp, the main program, //compile and run... //For VC++/VC++ .Net change the header files accordingly... //#include //#include //using namespace std; #include #include #include "vehicle.h" //user define header file and put it in the same folder as this program void main() { Cvehicle car, motorcycle, truck, sedan_car; //4 objects instantiated //data initialization car.initialize(4,3000.0); truck.initialize(20,30000.0); motorcycle.initialize(2,900.0); sedan_car.initialize(4,3000.0); //Display the data cout<<"The car has "< #include #include "vehicle.h" //the interface of the Cvehicle class #include "car.h" //the interface of the Ccar class #include "truck.h" //the interface of the Ctruck class void main() { Cvehicle unicycle; //base class cout<<"Unicycle using the Cvehicle base class\n"; cout<<"--------------------------------------\n"; unicycle.initialize(1,12.5); cout<<"Unicycle has "< #include //base class class Base { //member variables and member //functions... public: Base(){} ~Base(){} protected: private: }; //derived class... class Derived:public Base { //same as normal class actually... //member variables and member function... public: Derived(){} ~Derived(){} private: protected: }; void main() { cout<<"Testing the program skeleton..."< #include //---class declaration and implementation------ //base class... class MyFather { //member variables and member //functions... private: char* EyeColor; char* HairType; double FamSaving; protected: public: MyFather(){} ~MyFather(){} char* GetEye() { return EyeColor = "Brown";} char* GetHair() { return HairType = "Straight";} double GetSaving() {return FamSaving = 30000;} }; //derived class... class MySelf:public MyFather { //same as normal class actually... private: char* MyEye; char* MyHair; public: MySelf(){} ~MySelf(){} char* GetMyEye() { return MyEye = "Blue";} char* GetMyHair() {return MyHair = "Curly";} protected: }; //another derived class... class MySister:public MyFather { private: char* SisEye; char* SisHair; public: MySister(){} ~MySister(){} char* GetSisEye() {return SisEye = "Black";} char* GetSisHair() { return SisHair = "Blonde";} }; //-------main program-------- int main() { //base class object... MyFather Test1; cout<<"Testing the inheritance program...\n"< using namespace std; //---class declaration and implementation------ //base class... class MyFather { //member variables and member //functions... private: char* EyeColor; char* HairType; double FamSaving; protected: //protected members here… public: MyFather(){} ~MyFather(){} char* GetEye() { return EyeColor = "Brown";} char* GetHair() { return HairType = "Straight";} double GetSaving() {return FamSaving = 30000;} }; //derived class... class MySelf:public MyFather { //same as normal class actually... private: char* MyEye; char* MyHair; public: MySelf(){} ~MySelf(){} char* GetMyEye() { return MyEye = "Blue";} char* GetMyHair() {return MyHair = "Curly";} protected: }; //another derived class... class MySister:public MyFather { private: char* SisEye; char* SisHair; public: MySister(){} ~MySister(){} char* GetSisEye() {return SisEye = "Black";} char* GetSisHair() { return SisHair = "Blonde";} }; //------main program-------- int main() { //base class object... MyFather Test1; cout<<"Testing the inheritance program...\n"< using namespace std; //---class declaration and implementation------ //base class... class MyFather { //member variables and member //functions... private: char* EyeColor; char* HairType; double FamSaving; protected: public: MyFather(){} ~MyFather(){} char* GetEye() {return EyeColor = "Brown";} char* GetHair() {return HairType = "Straight";} double GetSaving() {return FamSaving = 30000;} }; //derived class... class MySelf:public MyFather { //same as normal class actually... private: char* MyEye; char* MyHair; public: MySelf(){} ~MySelf(){} char* GetMyEye() {return MyEye = "Blue";} char* GetMyHair() {return MyHair = "Curly";} protected: //... }; //another derived class... class MySister:public MyFather { private: char* SisEye; char* SisHair; public: MySister(){} ~MySister(){} char* GetSisEye() {return SisEye = "Black";} char* GetSisHair() {return SisHair = "Blonde";} }; //----main program-------- int main() { //base classes object... MyFather Test1; cout<<"Testing the inheritance program...\n"<