============================MODULE20======================================= | | | 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) ========================================================================= ========================================================================= //demonstrate the static variable... //For VC++/VC++ .Net change the header files accordingly //#include //#include //using namespace std; #include #include int funcstatic(int) { //local variable should exist locally... int sum = 0; sum = sum + 10; return sum; } int main() { int r = 5, s; //test the function calls several times... cout<<"Without static keyword\n"; cout<<"----------------------\n\n"; s = funcstatic(r); cout<<"1st time function call, s = "< #include "object.h" //extern... int global1 = 30; int global2 = 40; //-----class implementation part----- object::object(void) { objectvar = 0; } int object::set(int newvalue) { int local1 = 10; //non extern with same variables name.... global1 = 60; global2 = 70; //Display the local variable locally... cout<<"In object.cpp file, local function, local1 variable = "< #include int main() { //p = 10 is a constant value, cannot be modified //during the program execution... const int p = 10; cout<<"q = p + 20 = "<<(p + 20)<<" where, p = 10"< const int ArrayOne = 64; //The following code is legal in C++, but not in C char StoreChar[ArrayOne]; int main() { } //No output for this example ---------------------------------------------------------------------------------- //a const pointer to a variable... #include #include int main() { //declare the pointers and let they point //to something... //non const pointer... char *BuffOne = NULL, *BuffTwo = NULL; //a constant pointer... //assign the BuffOne pointer to PtrOne pointer char *const PtrOne = BuffOne; //Let it point to some data... *PtrOne = 'z'; cout<<"The value pointed by constant pointer is "<<*PtrOne< #include int main() { const char *BuffOne = "Testing"; cout<<"The data pointed by BuffTwo is "< #include //---Class declaration part----- class Date { int month; public: //we would test the month only... Date (int mnt, int dy, int yr); //A write function, so can't be const void SetMonth(int mnt); //A read only function declaration int GetMonth() const; }; //---Class implementation part--- Date::Date(int,int,int) { } void Date::SetMonth(int mnt) { //Modify the non const member variable data month = mnt; } //A read only function implementation int Date::GetMonth() const { //Does not modify anything return month; } //----main program---- void main() { Date TheDate(7,4,2004); //non const member function, OK TheDate.SetMonth(11); cout<<"Month of the sample date is "< using namespace std; int funcstatic(int) { //local variable should exist locally... static int sum = 0; sum = sum + 10; return sum; } int main() { int r = 5, s; //test the function calls several times... cout<<"Without static keyword\n"; cout<<"----------------------\n\n"; s = funcstatic(r); cout<<"1st time function call, s = "<