==============================Module10===================================== | | | 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++/VC++ .Net, chnage to the following //headers...change accordingly for other examples //#include //#include //using namespace std; #include #include #define THREETIMES(x) (x)*(x)*(x) #define CIRAREA(y) (PI)*(y)*(y) #define REC(z, a) (z)*(a) #define PI 3.14159 int main(void) { float p = 2.5; float r = 3.5, s, t, u = 1.5, v = 2.5; cout<<"Power to three of "< #include int main(void) { cout<<"Sample using #define, #ifdef, #ifndef\n"; cout<<" #undef, #else and #endif...\n"; cout<<"-------------------------------------\n"; #ifdef Module10 cout<<"\nModule10 is defined.\n"; #else cout<<"\nModule10 is not defined.\n"; #endif #ifndef MyVersion cout<<"\nMyVersion is not defined\n"; #else cout<<"\nMyVersion is "< #include //notice this... #include "boolean.h" int main(void) { //new type stored in boolean.h... boolean HappyTime; HappyTime = TRUE; //if TRUE = 1, do... if(HappyTime) cout<<"I'm happy today lor!!!"< #include #if MyVAL != 2 #error MyVAL must be defined to 2 #endif int main() { system("pause"); return 0; } //No output, error message during the //compilation ---------------------------------------------------------------------------- //#error directive... #include #include #define MyVAL 2 #if MyVAL != 2 #error MyVAL must be defined to 2 #endif int main() { system("pause"); return 0; } //No output ------------------------------------------------------------------------- //#error directive... #include #include #if MyChar != 'X' #error The MyChar character is not 'X' #endif int main() { system("pause"); return 0; } //No output, with error message during //the compilation ------------------------------------------------------------------------- //#pragma directive... #include #include //displays either "You are compiling using //version xxx of BC++" (where xxx is the version number) //or "This compiler is not Borland C++", date, time //console or not... by using several related //predefined macro such as __DATE__ etc #ifdef __BORLANDC__ #pragma message You are compiling using Borland C++ version __BORLANDC__. #else #pragma message ("This compiler is not Borland C++") #endif #pragma message time: __TIME__. #pragma message date: __DATE__. #pragma message Console: __CONSOLE__. int main() { system("pause"); return 0; } //No output ------------------------------------------------------------------------ //#pragma directive... #include #include //displays either "You are compiling using //version xxx of BC++" (where xxx is the version number) //or "This compiler is not Borland C++", date, time //console or not... by using several related //predefined macro such as __DATE__ etc #ifdef __BORLANDC__ #pragma message You are compiling using Borland C++ version __BORLANDC__. #else #pragma message ("This compiler is not Borland C++") #endif #pragma message ("time:" __TIMESTAMP__) #pragma message ("date:" __DATE__) #pragma message ("file:" __FILE__) int main() { system("pause"); return 0; } ---------------------------------------------------------------------- //#pragma directives... #include #if _M_IX86 != 500 #pragma message("Non Pentium processor build") #endif #if _M_IX86 == 600 #pragma message("but Pentium II above processor build") #endif #pragma message("Compiling " __FILE__) #pragma message("Last modified on " __TIMESTAMP__) int main() { return 0; } --------------------------------------------------------------------- #include #include #define HELLO(x) printf("Hello, " #x "\n"); #define SHOWFUNC(x) Use ## Func ## x int main(void) { //new concatenated identifier, UseFuncOne char * SHOWFUNC(One); //new concatenated identifier, UseFuncTwo char * SHOWFUNC(Two); SHOWFUNC(One) = "New name, UseFuncOne"; SHOWFUNC(Two) = "New name, UseFuncTwo"; HELLO(Birch); printf("SHOWFUNC(One) -> %s \n",SHOWFUNC(One)); printf("SHOWFUNC(One) -> %s \n",SHOWFUNC(Two)); system("pause"); return 0; } ------------------------------------------------------------------ #include #include int main(void) { cout<<"Let test the free macros, standard and compiler specific..."< #include #include void TestString(char *string); void main() { //first test array of char, 10 characters... //should be OK for the 3 test conditions... char test1[] = "abcdefghij"; //second test pointer to string, 9 characters... //should be OK for the 3 test conditions... char *test2 = "123456789"; //third test array char, empty... //should fail on the 3rd condition, cannot be empty... char test3[] = ""; printf("Testing the string #1 \"%s\"\n", test1); TestString(test1); printf("Testing the string #2 \"%s\"\n", test2); TestString(test2); printf("Testing the string #3 \"%s\"\n", test3); TestString(test3); } void TestString(char * string) { //set the test conditions... //string must more than 8 characters... assert(strlen(string) > 8); //string cannot be NULL assert(string != NULL); //string cannot be empty.... //test3 should fail here and program abort... assert(string != '\0'); } ------------------------------------------------------------------------------------------------ //assert macro and DEBUG, NDEBUG //NDEBUG will disable assert(). //DEBUG will enable assert(). #define DEBUG #include #include #include int main() { int x, y; //Tell user if NDEBUG is defined and do assert. #if defined(NDEBUG) cout<<"NDEBUG is defined. Assert disabled,\n"; #else cout<<"NDEBUG is not defined. Assert enabled.\n"; #endif //prompt user some test data... cout<<"Insert two integers: "; cin>>x>>y; cout<<"Do the assert(x < y)\n"; //if x < y, it is OK, else this program will terminate... assert(x < y); if(x y, assertion will be invoked!"< #include using namespace std; int main() { int x, y; //Tell user if NDEBUG is defined and do assert. #if defined(NDEBUG) cout<<"NDEBUG is defined. Assert disabled,\n"; #else cout<<"NDEBUG is not defined. Assert enabled.\n"; #endif //prompt user some test data... cout<<"Insert two integers: "; cin>>x>>y; cout<<"Do the assert(x < y)\n"; //if x < y, it is OK, else this program will terminate... assert(x < y); if(x y, assertion will be invoked!"<