Visual C++ Environment, Preprocessor and Hungarian Notation
Total Page:16
File Type:pdf, Size:1020Kb
Lecture 2 Visual C++ Environment, Preprocessor and Hungarian Notation Visual C++ Environment: For now, we will barely touch the massive features that this environment offers. Creating a project: Visual C++ 4.0: Visual C++ 5.0: File File New New Project Workspace Projects Tab Console Application Win32 Console Application Browse to set location Location Button to Browse Assign a project name Assign a project name Create (hit enter key) File File New New Text file C++ Source File -or- Header enter program, etc. enter code File SaveAs filename (.cpp or .h) Insert Project File into project Add to Project Files To see project structure: on left viewer window: on left viewer window: select FileView select Files To compile a single file: Build Compile To build the project Build Build To compile and run ("Go"): Build Execute To debug: Build Debug Example: // Page 64 program #include <iostream.h> int main() { int counter, grade, total, average; total = 0; counter = 1; while (counter <= 10 ) { cout << "Enter grade: "; cin >> grade; total = total + grade; counter = counter + 1; } average = total / 10; cout << "Class average is " << average << endl; return 0; } The preprocessor highlights: #include <file> // standard libraries #include "file" // directory then standard libraries #define PI 3.14159 // anything after symbolic constant is substituted for PI #undef PI #if !defined(NULL) #define NULL 0 #endif #ifdef DEBUG cout << "Got here\n"; #endif a common trick to avoid reprocessing a header file: #ifndef somethingh #define somethingh // body of something.h #endif #error tokens // tokens displayed and preprocessing stops #pragma tokens // some implementation action #: #define NameOut(x) cout << #x << endl; #x substitutes "x" i.e., NameOut(Gary) gives cout << "Gary" << endl; ##: concatenates two tokens. #define Glue(x,y) x ## y i.e., Glue(Pass, Exam) gives PassExam "Hi " "There" gets translated to "Hi There" by preprocessor assert macro (see assert.h) assert( condition ); if condition is false, assert prints error message and calls abort. To turn off: #define NDEGUB Hungarian notation: Developed by Charles Simonyi at Microsoft to make naming conventions standard and understandable. variable name = prefix + base type + qualifier (also called constructor + tag + qualifier ) Base type (tag) is ALWAYS present. Base types are usually application dependent and stand for something relevant to the tasks at hand. Eg. char chA; // chA is a one-byte character double dBob; // dBob is a double Color coGreen; // coGreen might hold the structure of a green color // co is the tag. Color is some typedef structure. Some Standard Base Types (tags) f - Flag (boolean, logical) fl - File structure returned from operating system ch - One-byte character sz - Zero-terminated string fn - Function n - Integer (usually a short int) e - Enumeration (enumeration types) st - Structure w - Word (typically 16 bits) b - Byte (typically 8 bits) l - Long (typically 32 bits) u - Unsigned uw - Unsigned word (typically 16 bits) ul - Unsigned long bit - Single bit v - Void d - Double (double precision) r - float (single precision) Prefixes (constructors) are used with a base type to describe more complex items. Eg. char* pchA; // pchA is a pointer to a one-byte character double rgdBob[10]; // rgdBob is an array of doubles int idBob; // index for array of doubles Color gcoGreen; // gcoGreen holds the structure of a green color // and has global scope. Some Standard Prefixes (constructor): p - Pointer lp (or lptr) - Far pointer (long pointer) rg - Array, or pointer to array i - Index into an array c - Count d - Difference between two instances of a type h - Handle mp - Array followed by index type and then element type e - Element of an array f - Bit within a type g or v - Global Qualifiers what we usually think of as the name. In the above examples, A, Bob and Green are qualifiers. They usually indicate what the variable is used for. They may be multiple words. Capitalize the first letter of each and make the rest lower case. Constants are usually all caps. Some Standard Qualifiers First - First element in a set or array Last - Last element in a set or array Lim - Upper limit of elements in a set, invalid value Min - First element in a set, often refers to actual first element Max - Upper limit of elements in a set, invalid value for(int i=nMin;i<nMax;i++) Mac - Current upper limit of elements in a set, can change Mic - Current first element in a set, can change Most - Last element in a set, used when paired with Min Mac - 1 usually Sav - Temporary saved value Nil - Special illegal value Null - The 0 value T - Temporary value Src - Source, used in transfer operations Dest - Destination Example: // Page 64 program - rewritten #include <iostream.h> int main() { int nCounter = 1; int nTotal = 0; while (nCounter <= 10 ) { int nGrade; cout << "Enter grade: "; cin >> nGrade; nTotal+= nGrade; nCounter++; } double dAverage = (double)nTotal / 10.; cout << "Class average is " << dAverage << endl; return 0; } Procedure Names: Capitalize first letter. If it returns something, start the name with the type returned. Follow this with some verbs describing what the procedure does. Can follow this with the type of parameters. Eg. int NCountEntriesPsz( char* pszName ) More will be covered once we hit C++..