C++ Language Reference Comments: /* Block: It is primarily used for multiline comments. */ // Line comment: used on a separate line or after a C++ statement on the same line Console IO cin >> variable [ >> variable ] ...; cout << expression or manipulator [ << expression or manipulator ] ...; getline(cin, strVar); cin.get([varChar]); Arithmetic Operators: | Assignment Operators: () overrides order | = a single equals sign is the simple assignment increment ++ | operator; it assigns the result from evaluating decrement -- | the expression on the right side to the variable negative - | left of the = sign; Compound operators follow: multiplication * | *= "times equals" assigns product of both to left division / | /= "divide equals" assigns quotient to left side addition + | += "plus equals" assigns sum subtraction - | -= "minus equals" assigns difference modulus % | %= "modulus equals" assigns remainder Assignment Statement: variable = expression; the value of the expression should match the variabble type Relational Operators: equals == | < less than not equal != | >= greater than or equal to greater than > | <= less than or equal to (note: the exclamation point (!) means "not") Control Structure - if/else if (booleanExpression) {statement block} [else if (booleanExpression) {statement block} ] ... [else {statement block}] Repetition Structure - do ... while loop do { statement block } while (booleanExpression); Repetition Structure - while loop while (booleanExpression) { statement block } Repetition Structure - for loop for (initial statement; loopCondition; updateStatement) { statement block } Control Structure -- switch statement switch (expression) { case value1: statement block 1 [break;] case value2: statement block 2 [break;] ... [default: statement block n; ] } User-defined Functions A function without parameters: dataType|void functionName() { statements } A Function with Parameters: dataType|void functionName(formal parameter list) { statements } return expr; where expr is a variable, constant value or expression Formal Parameter List: dataType variable, dataType& variable, ... The trailing ampersand(&) indicates that parameter is passed by a reference to the location (memory address) of the corresponding actual parameter rather than by value where the formal parameter receives a copy of the content. Function Call: functionName (actual parameter list); Predefined Functions for Arithmetic Operations Function Description --------- ----------------------------------------------------- abs(i-j) absolute value of difference between integers i & j ceil(x) ceiling of double named x floor(x) floor of double named x min(i,j) find minimum value of pair of integers max(i,j) find maximum value " rand( ) generate random number (include srand(seed)) sqrt(x) square root of double expression pow(x,y) for doubles x & y, raise x to the power of y Predefined string functions The concatenation (+) operator causes strings to be joined. Function Description --------- ----------------------------------------------------- strVar.length() where strVar is a variable of type string, and the function returns the number of characters strVar.find(strExp[, position]) where strExp is a string expression evaluating to a string, or a character, and position indicates where (relative to 0) to begin the search strVar.substr(expr1, expr2) where expr1 and expr2 are expressions evaluating to unsigned integers. The starting position is specified by expr1, and the length is indicated by expr2 strVar1.swap(strVar2) where the contents of strVar1 and strVar2 are interchanged; also swap(strVar1,strVar2); toupper(ch) function to change case strVar.c_str() generates a null-terminated sequence of characters (C-string) with the same content as the string object. atof() parses the C-string strVar interpreting its content as a floating point number and returns its value as a double. atoi() parses the C-string strVar interpreting its content as an integer number and returns its value as an int. File IO #include ifstream fileStreamVariable; ofStream fileStreamVariable; fileStreamVariable.open(pathName); getline(fileStreamVariable, inputStreamVariable); fileStreamVariable >> inputStreamVariable ... ; fileStreamVariable << outputStreamVariable ... ; fileStreamVariable.close( ); Array A collection of a fixed number of components all of the same data type: dataType arrayName[intExp]; Index: A non-negative integer that specifies the position of a component in an array. Examples: item[4] = 100; item[4] = "tea" Vector General Form: vector vecList; Methods: vecList.push_back(element),vecList.size( ),vecList.index[index] ... Class The general form or syntax for a class is: class className { classMembersList statements }; Defining a constructor: public: className([parameters]) {[assign parameter values to member variables]} Defining methods: public|private: type methodName([parameterS]) {return variable|assign valueS}; Accessing class members (variable or function): classObjectName.memberName Invoking a constructor with parameters: className classObjectName([argument1,argument2, ...] ); Invoking a default constructor: className classObjectName; Invoking a method: classObjectName.methodName([argument1,argument2,...]); For a derived class definition, the syntax is: class className: memberAccessSpecifier baseClassName {memberList statements}; Pointer The syntax for pointer definition: dataType *identifier. Related operators: Address operator (&) Dereferencing operator (*):