正在加载图片...
C++Reference Card Looping Pointers C++Data Types Data Type coutnd do-whil Loop Example do orchango th Operators biLe (expresston);hle (x1) oan:升s日 T0gnyeoeapossdpomes oI=(notequal-o】 for (;test;update) nd) statement 》 as5 signment0.*=,/=,=,+=,-= []<endl; Console Input/Output aetoar9a: Dynamic Memory Enter an integer:" Functions ptr new type[sizeli File Input/Output intntt eturntype functton(type pl type p2) Oneanaaboae8ar7ena inputFile.close(): 1 outrile.close() Structures Decision Statements SE2end 0ecatioa Eample ge ype2 eLement2 Passing Paramters by Value unction and a;ptr e2aro uaacome otrName->: ett .C++ Reference Card C++ Data Types Data Type Description bool boolean (true or false) char character ('a', 'b', etc.) char[] character array (C-style string if null terminated) string C++ string (from the STL) int integer (1, 2, -1, 1000, etc.) long int long integer float single precision floating point double double precision floating point These are the most commonly used types; this is not a complete list. Operators The most commonly used operators in order of precedence: 1 ++ (post-increment), -- (post-decrement) 2 ! (not), ++ (pre-increment), -- (pre-decrement) 3 *, /, % (modulus) 4 +, - 5 <, <=, >, >= 6 == (equal-to), != (not-equal-to) 7 && (and) 8 || (or) 9 = (assignment), *=, /=, %=, +=, -= Console Input/Output cout << console out, printing to screen cin >> console in, reading from keyboard cerr << console error Example: cout << "Enter an integer: "; cin >> i; cout << "Input: " << i << endl; File Input/Output Example (input): ifstream inputFile; inputFile.open("data.txt"); inputFile >> inputVariable; // you can also use get (char) or // getline (entire line) in addition to >> ... inputFile.close(); Example (output): ofstream outFile; outfile.open("output.txt"); outFile << outputVariable; ... outFile.close(); Decision Statements if Example if (expression) if (x < y) statement; cout << x; if / else Example if (expression) if (x < y) statement; cout << x; else else statement; cout << y; switch / case Example switch(int expression) switch(choice) { { case int-constant: case 0: statement(s); cout << "Zero"; break; break; case int-constant: case 1: statement(s); cout << "One"; break; break; default: default: statement; cout << "What?"; } } Looping while Loop Example while (expression) while (x < 100) statement; cout << x++ << endl; while (expression) while (x < 100) { { statement; cout << x << endl; statement; x++; } } do-while Loop Example do do statement; cout << x++ << endl; while (expression); while (x < 100); do do { { statement; cout << x << endl; statement; x++; } } while (expression); while (x < 100); for Loop for (initialization; test; update) statement; for (initialization; test; update) { statement; statement; } Example for (count = 0; count < 10; count++) { cout << "count equals: "; cout << count << endl; } Functions Functions return at most one value. A function that does not return a value has a return type of void. Values needed by a function are called parameters. return_type function(type p1, type p2, ...) { statement; statement; ... } Examples int timesTwo(int v) { int d; d = v * 2; return d; } void printCourseNumber() { cout << "CSE1284" << endl; return; } Passing Parameters by Value return_type function(type p1) Variable is passed into the function but changes to p1 are not passed back. Passing Parameters by Reference return_type function(type &p1) Variable is passed into the function and changes to p1 are passed back. Default Parameter Values return_type function(type p1=val) val is used as the value of p1 if the function is called without a parameter. Pointers A pointer variable (or just pointer) is a variable that stores a memory address. Pointers allow the indirect manipulation of data stored in memory. Pointers are declared using *. To set a pointer's value to the address of another variable, use the & operator. Example char c = 'a'; char* cPtr; cPtr = &c; Use the indirection operator (*) to access or change the value that the pointer references. Example // continued from example above *cPtr = 'b'; cout << *cPtr << endl; // prints the char b cout << c << endl; // prints the char b Array names can be used as constant pointers, and pointers can be used as array names. Example int numbers[]={10, 20, 30, 40, 50}; int* numPtr = numbers; cout << numbers[0] << endl; // prints 10 cout << *numPtr << endl; // prints 10 cout << numbers[1] << endl; // prints 20 cout << *(numPtr + 1) << endl; // prints 20 cout << numPtr[2] << endl; // prints 30 Dynamic Memory Allocate Memory Examples ptr = new type; int* iPtr; iPtr = new int; ptr = new type[size]; int* intArray; intArray = new int[5]; Deallocate Memory Examples delete ptr; delete iPtr; delete [] ptr; delete [] intArray; Once a pointer is used to allocate the memory for an array, array notation can be used to access the array locations. Example int* intArray; intArray = new int[5]; intArray[0] = 23; intArray[1] = 32; Structures Declaration Example struct name struct Hamburger { { type1 element1; int patties; type2 element2; bool cheese; }; }; Definition Example name varName; Hamburger h; name* ptrName; Hamburger* hPtr; hPtr = &h; Accessing Members Example varName.element=val; h.patties = 2; h.cheese = true; ptrName->element=val; hPtr->patties = 1; hPtr->cheese = false; Structures can be used just like the built-in data types in arrays
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有