============================MODULE22======================================= | | | 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) ========================================================================= ========================================================================= //For VC++ (Version > 4.0) / VC++ .Net change the header files //to the following accordingly...Same for other examples lol! //#include //#include //using namespace std; #include #include int main() { int sum = 1000; int count = 21; double average1 = sum/count; cout<<"Before conversion = "<(sum)/count; cout<<"After conversion = "< #include //enum data type enum color {blue, yellow, red, green, magenta}; int main() { int p1 = 3; cout<<"integer type, p1 = "< (p1)"< (p1); cout<<"enum type, c1 = "< #include int main() { //p = 10 is a constant value, cannot be modified const int p = 20; cout<<"const p = "< #include struct One { //test function... void funct1() { cout<<"Testing..."< (c); cout<<"The reference = "<<&noconst< #include double funct1(double& f) { //do some work here... f++; cout<<"f = "< #include class One { public: void funct() {cout<<"Testing..."<(Test3).funct(); //remove const and volatile... const_cast (Test1); } int main() { TestConstVol(); system("pause"); return 0; } ------------------------------------------------------------------------------------------ //removing the const-ness of the //this pointer #include #include class Test { public: void GetNumber(int); //Read only function... void DisplayNumber() const; private: int Number; }; void Test::GetNumber(int Num) {Number = Num;} void Test::DisplayNumber() const { cout<<"\nBefore removing const-ness: "<(this)->Number+=2; cout<<"\nAfter removing const-ness: "< #include class Test { //using mutable mutable int count; mutable const int* ptr; public: //Read only function can't //change const arguments. int funct(int num = 10) const { //should be valid expression... count = num+=3; ptr = # cout<<"After some operation, the new value: "<<*ptr< #include //base class class Base1 {}; //derived class... class Derived1:public Base1 {}; //another derived class class Derived2:public Derived1{}; //dynamic_cast test function... void funct1() { //instantiate an object… Derived2* Test1 = new Derived2; //upcasting, from derived class to base class, //Derived1 is a direct from Base1 //making Test2 pointing to Derived1 sub-object of Test1 Derived1* Test2 = dynamic_cast(Test1); cout<<"Derived1* Test2 = dynamic_cast(Test1);"<(Test1); cout<<"\nBase1* Test3 = dynamic_cast(Test1);"< #include //base class class Base1 { public: virtual void funct1(){}; }; //another base class... class Base2 { public: virtual void funct2(){}; }; //dynamic_cast test function... void funct3() { //instantiate objects… Base1 * Test1 = new Base1; Base2 * Test2 = new Base2; //making Test3 pointing to an object of type Base1 void* Test3 = dynamic_cast(Test1); cout<<"void* Test3 = dynamic_cast(Test1);"<(Test2); cout<<"\nTest3 = dynamic_cast(Test2);"< #include //base class class Base1 { public: virtual void funct1(){}; }; //derived class... class Derived1:public Base1 { public: virtual void funct2(){}; }; //dynamic_cast test function... void funct3() { //instantiate objects… Base1* Test1 = new Derived1; Base1* Test2 = new Base1; //making Test1 pointing to Derived1 Derived1* Test3 = dynamic_cast(Test1); cout<<"Derived1* Test3 = dynamic_cast(Test1);"<(Test2); cout<<"\nDerived1* Test4 = dynamic_cast(Test2);"< #include //base class class Base1 {}; class Derived1:public Base1{}; class Derived2:public Base1{}; //derived class... class Derived3:public Derived1, public Derived2 { public: virtual void funct1(){} }; //dynamic_cast test function... void funct2() { //instantiate an object… Derived3 *Test1 = new Derived3; //-------start comment out--------- //may fail, ambiguous...from Derived3 direct //conversion to Base1... //if you use good compiler, please comment out this //part, there should be run time error:-) Base1* Test2 = dynamic_cast(Test1); cout<<"Base1* Test2 = dynamic_cast(Test1);"< "<(Test1); cout<<"\nDerived1* Test3 = dynamic_cast(Test1);"<(Test3); cout<<"\nBase1* Test4 = dynamic_cast(Test3);"< #include //base class class Base1 { public: virtual void funct1(){}; }; class Derived1:public Base1 { public: virtual void funct2(){}; }; class Derived2:public Base1{ public: virtual void funct3(){}; }; //derived class... class Base2 { public: virtual void funct4(){}; }; class Derived3:public Derived1,public Derived2,public Base2 {}; //dynamic_cast test function... void funct5() { //instantiate an object //Test1 of type Base2... //or test1 of type Derived2... //you can choose either one:-) Base2* Test1 = new Base2; //Derived2* Test1 = new Derived2; //start with downcast, type Base2/Derived2 to Derived3... Derived3* Test2 = dynamic_cast(Test1); cout<<"Firstly, Derived3* Test2 = dynamic_cast(Test1);"<(Test2); cout<<"\nThen, Derived1* Test3 = dynamic_cast(Test2);"<(Test1); cout<<"\nThen, Derived1* Test4 = dynamic_cast(Test1);"< #include //a class with virtual function... //polymorphic… class Base1 { public: virtual void FuncBase1() {}; }; //another class with virtual function... class Base2 { public: virtual void FuncBase2() {}; }; //derived class from Base1 and Base2 classes //public virtual and private... class Derived1:public virtual Base1, private Base2 {}; //dynamic_cast test function... void DynamicCastSample() { //instantiate an object of type Derived1 class... Derived1 DerivedObj; //simple assignment, derived to base class, upcasting... //cast needed to break private protection... Base2* Base2Obj = (Base2*) &DerivedObj; //another assignment, derived to base class, upcasting... //public inheritance, no need casting.. Base1* Base1Obj = &DerivedObj; //base class to derived class, downcast Derived1& Derived1Obj = dynamic_cast(*Base2Obj); if(!&Derived1Obj) cout<<"Conversion is failed!...."<(Base2Obj); if(!Base1Obj) cout<<"Conversion is failed!...."<(Base1Obj); if(!Base2Obj) cout<<"Conversion is failed!...."<(&Derived1Obj); if(!Base1Obj) cout<<"Conversion is failed!...."<(&Derived1Obj); if(!Base2Obj) cout<<"Conversion is failed!...."< #include #include //T - True, F - False #define T 1 #define F 0 //a base class class A { }; //a derived class class B : A { }; int main() { char c; float f; //using typeinfo operator, == for comparison if (typeid(c) == typeid(f)) cout<<"c and f are the same type."< #include #include //polymorphic base class... class __rtti Test { //This makes Test a polymorphic class type. virtual void func() {}; }; //derived class... class Derived : public Test {}; int main(void) { //Instantiate Derived type object... Derived DerivedObj; //Declare a Derived type pointer Derived *DerivedPtr; //Initialize the pointer DerivedPtr = &DerivedObj; //do the run time checking... if(typeid(*DerivedPtr) == typeid(Derived)) //check the type of *DerivedPtr cout<<"Ptr *DerivedPtr type name is "< #include #include class Base { public: virtual void funct(){} }; class Derived:public Base{}; int main() { Derived* Test1 = new Derived; Base* Test2 = Test1; cout<<"The type name of Test1 is: "; cout< #include unsigned int* Test(int *q) { //convert int pointer to unsigned int pointer unsigned int* code = reinterpret_cast(q); //return the converted type data, a pointer... return code; } int main(void) { //array name is a pointer... int a[10]; cout<<"int pointer unsigned int pointer"< using namespace std; class MyStack { public: //create a stack with initial size MyStack(int initsize); ~MyStack(void); }; MyStack::MyStack(int initsize) { static x; cout<<"Constructor: Pass #"< using namespace std; class MyStack { public: //create a stack with initial size explicit MyStack(int initsize); ~MyStack(void); }; MyStack::MyStack(int initsize) { static x; cout<<"Constructor: Pass #"< #include using namespace std; class Base { public: virtual void funct(){} }; class Derived:public Base{}; int main() { Derived* Test1 = new Derived; Base* Test2 = Test1; cout<<"The type name of Test1 is: "; cout< using namespace std; //base class class Base1 {}; //derived class... class Derived1:public Base1 {}; //another derived class class Derived2:public Derived1{}; //dynamic_cast test function... void funct1() { //instantiate an object. Derived2* Test1 = new Derived2; //upcasting, from derived class to base class, //Derived1 is a direct from Base1 //making Test2 pointing to Derived1 sub-object of Test1 Derived1* Test2 = dynamic_cast(Test1); cout<<"Derived1* Test2 = dynamic_cast(Test1);"<(Test1); cout<<"\nBase1* Test3 = dynamic_cast(Test1);"<