OVERVIEW/REVIEW OF C++ ====================== http://www.cplusplus.com/ http://en.wikipedia.org/wiki/C%2B%2B http://en.wikipedia.org/wiki/C_(programming_language) http://www.research.att.com/~bs/homepage.html #include's / Header Files ------------------------- I/O stream library: cin, cout, endl, << ("insertion") operator, >> ("extraction") operator http://www.cplusplus.com/reference/iostream/ I/O manipulators, used with cout and other output streams: setw, setprecision, showpoint, fixed, dec, oct, hex, boolalpha, ... string class http://www.cplusplus.com/reference/string/string/ C math library: pow, sqrt, abs, exp, log, sin, cos, tan, ... http://www.cplusplus.com/reference/clibrary/cmath/ File stream library: ifstream, ofstream, fstream classes using namespace std ; Select the Standard C++ libraries main ---- int main( ) { // no return 0 ; required in Standard C++ } int main( int argc, char ** argv ) // to use command-line arguments { } Object-Oriented Programming --------------------------- http://en.wikipedia.org/wiki/Object-oriented_programming Built-In Datatypes ------------------ char single byte or ASCII character (8 bits) int signed integer (32 bits) double double-precision floating-point number (64 bits) bool Boolean value (true or false) string a sequence of characters ^ use these, ignore these ... unsigned char short unsigned short unsigned int long unsigned long float Arithmetic Expressions ---------------------- for ints and doubles (can be used with chars too, but not recommended) ( ) Parentheses (brackets); force the order of evaluation * multiplication / division % modulus/remainder (ints only) + addition - subtraction - negation, when to the left of a number or variable Expressions are evalulated from left to right, in priority: Remember "BEDMAS" from grade school??? brackets (highest priority) exponents (not directly available in C++) negation goes here division, multiplication (and modulus) addition, subtraction For example, int a, b, c, d ; . . . int result = a * b + c * d ; Two multiplications are done first (a * b, followed by c * d), then the addition. To force the addition to be done first, use brackets: int result = a * ( b + c ) * d ; Similarly, int result = a + b * c + d ; The multiplication is done first (b * c), followed by a + b * c, and a + b * c + d. To force the additions to be done first: int result = ( a + b ) * ( c + d ) ; To use exponents, call the pow() function: double pow( double base, double exponent ) For example, to get x cubed: double x ; double x_cubed = pow( x, 3.0 ) ; NOTE the "3.0" forces the compiler to interpret the 3 as a double value. THIS IS IMPORTANT, because we don't want the compiler to mistakenly convert a double result to an int result (and throw away any decimal places.) Bitwise Arithmetic (chars and ints only) ------------------ Symbol Meaning Example Explanation ------ -------------- ------- ---------------------------------- & bitwise AND a & b ANDs each pair of bits in a and b | bitwise OR a | b ORs each pair of bits in a and b ^ bitwise XOR a ^ b XORs each pair of bits in a and b (exclusive-OR) ~ bitwise NOT ~a flips each bit in a (1s flip to 0s; (negation, or 0s flip to 1s) 1's complement) << bitwise left a << 2 shifts bits 2 places left, shifts in shift 0 bits at the end (least significant bit); equivalent to multiplying a by 2 squared a << 3 is the same as multiplying a by 2 cubed a << 4 is the same as multiplying a by 2 to the fourth power etc. >> bitwise right b >> 2 shifts bits 2 places right, shifts in shift 0 bits at the beginning (most significant bit) equivalent to dividing b by 2 squared b >> 3 is the same as dividing b by 2 cubed b >> 4 is the same as dividing b by 2 to the fourth power etc. Example: int ip_address = 192 << 24 | 197 << 16 | 62 << 8 | 35 ; int netmask = 255 << 24 | 255 << 16 | 255 << 8 ; int network_part = ip_address & netmask ; int host_part = ip_address & ~netmask ; int ip_address_recombined = network_part | host_part ; cout << "IP address is " << ( ( ip_address_recombined >> 24 ) & 255 ) << '.' << ( ( ip_address_recombined >> 16 ) & 255 ) << '.' << ( ( ip_address_recombined >> 8 ) & 255 ) << '.' << ( ip_address_recombined & 255 ) << endl ; Logical Expressions ------------------- - used to answer "yes or no" questions - because the computer is essentially a collection of transistors biased as switches, which can only pass current (+5V, for example, logic high, bit value 1, true, yes) or NOT pass current (0V, logic low, bit value 0, false, no). - used in if, while, do...while, and for structures - evaluate to true or false (both of which are built-in bool constants in C++) - you can also assign the result of a logical expression to a bool variable - You can use brackets here - && (AND) and || (OR) can be used to join expressions - ! is logical negation (NOT) -- flips a true result to false and a false result to true Expression Meaning Negative or Alternate Forms ---------- -------------------------- --------------------------- [Here, a and b can be chars, ints, doubles, bools or strings] a == is a equal to b ! ( a != b ) != is a not equal to b ! ( a == b ) < is a less than b ! ( a >= b ) ( b >= a ) > is a greater than b ! ( a <= b ) ( b <= a ) <= is a less than or ! ( a > b ) equal to b ( a < b ) || ( a == b ) ( b >= a ) >= is a greater than or ! ( a < b ) equal to b ( a > b ) || ( a == b ) ( b <= a ) http://en.wikipedia.org/wiki/Boolean_algebra_(logic) http://en.wikipedia.org/wiki/De_Morgan%27s_laws De Morgan's Laws: NOT ( P AND Q ) = NOT P OR NOT Q NOT ( P OR Q ) = NOT P AND NOT Q where P and Q are logical expressions ! ( ( a < b ) && ( c < d ) ) is the same as ! ( a < b ) || ! ( c < d ) is the same as ( a >= b ) || ( c >= d ) ! ( ( a >= b ) || ( c >= d ) ) is the same as ! ( a >= b ) && ! ( c >= d ) is the same as ( a < b ) && ( c < d ) Example Program to Display The Outcome of a Logical Expression #include #include using namespace std ; int main( ) { int a, b ; cout << "Enter first integer: " ; cin >> a ; cout << "Enter second integer: " ; cin >> b ; bool result = a < b ; cout << a << " < " << b << " evaluates to " << boolalpha << result << endl ; } END OF PART ONE