Module 10: Introduction TO C And C++

Google+ Pinterest LinkedIn Tumblr +

MODULE 10

————————————————-
#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 “<

cout<<“Circle circumference = 2*PI*r = “<<(2*PI*r)<
s = CIRAREA(r+p);
cout<<“Circle area = PI*r*r = “<
t = REC(u, v);
cout<<“Rectangle area = u*v = “< system(“pause”);
return 0;
}

————————————————————————————-

#define Module10
#define MyVersion 1.1
#include
#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 “< #endif

#ifdef MyRevision
cout<<“\nMy Revision is defined\n”< #else
cout<<“\nMyRevision is not defined!\n”< #endif

#undef MyVersion
#ifndef MyVersion
cout<<“MyVersion is not defined\n”< #else
cout<<“MyVersion is “< #endif
system(“pause”);
return 0;
}

——————————————————————————-

//Program using the user defined
//header file, boolean.h
#include
#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!!!”< //else, FALSE = 0, do…
else
cout<<“What a bad day…today!!!”< system(“pause”);
return 0;
}

—————————————————————————–

//#error directive…
#include
#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…”< cout<<“\nPredefined macro __LINE__ : “<<__LINE__< cout<<“Predefined macro __FILE__ : “<<__FILE__< cout<<“Predefined macro __TIME__ : “<<__TIME__< cout<<“Predefined macro __DATE__ : “<<__DATE__< cout<<“Some compiler specific __MSDOS__: “<<__MSDOS__< cout<<“Some compiler specific __BORLANDC__: “<<__BORLANDC__< cout<<“Some compiler specific __BCPLUSPLUS__: “<<__BCPLUSPLUS__<
system(“pause”);
return 0;
}

————————————————————————————————–

#include
#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 {
cout<<“Assertion not invoked because “< cout<<“Try key in x > y, assertion will be invoked!”< }
else
cout<<“Assertion invoked, program terminated!”< system(“pause”);
return 0;
}

——————————–gcc—————————————-

////////testassert.cpp/////////
//DEBUG will enable assert().
#define DEBUG
#include
#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 {
cout<<“Assertion not invoked because “< cout<<“Try key in x > y, assertion will be invoked!”< }
else
cout<<“Assertion invoked, program terminated!”< return 0;
}

=============================================================================================;

Share.

About Author

Leave A Reply