Docstoc

C++ Cheat Sheet

Document Sample
C++ Cheat Sheet Powered By Docstoc
					C++ Syntax (Cheat Sheet) -----------------------Terms in <> are tokens which describe generically what goes in there. All other terms are literally themselves. EXAMPLE: <name> = <expression> represents x = 5 or name = "John" + "Stewman" Also, a <statement> can be a function call, an assignment statement, an if or if-else statement, a while or do-while loop, a switch statement, a for loop, etc. // comment /* another comment */ bool int float char long short string 'a' 124 -25.0 1.33e5 "hello" true + - / * % < <= > >= == != && || ! <statement>; <statement>; ... #include < <library_name> > #include "stuff.h" using namespace <name> enum <name> { <value_name_list> }; <name>, <name>, <name>, ... #define <name> <value> // types // literal constants (values) // arithmetic operations // comparison operations // boolean operations // statements // include file directive // include header file directive // namespace directive // enumeration definition // value name list // defined constant compiler directive // constant definition // object definition(s) // object definition and initialization // another object defn & initialization // array declaration // function prototype

const <type> <name> = <value>; <type> <name>, <name>, ... ; <type> <name> = <value>; <type> <name> ( <value> );

<element_type> <name> [ <number_of_elements> ]; <type> <name> ( <formal_parameter_list> ); <type> <name>, <type> <name>, ...

// formal parameter list // function definition

<type> <name> ( <formal_parameter_list> ) { <definitions> <statements> } <function_name> ( <actual_parameters> );

// call to void function

// call to (or use of) function which returns a value <name> = <function_name> ( <actual_parameters> ); <name>, <name>, ... { <definitions> <statements> } <name> = <expression>; // actual parameters // block -- can replace ANY statement // has its own LOCAL SCOPE // assignment statement

cout << fixed cin >>

setprecision(2) endl

// output stream related terms // input stream related terms // declare and open input file stream // declare and open output file stream // open file with name in char array <fname> // close a stream // convert string object to char array // if statement // if-else statement

ifstream <name>("filename"); ofstream <name>("filename"); <stream_name>.open( char *<fname> ) <stream_name>.close() <sting_name>.c_str() if ( <boolean_expression> ) <statement>; if ( <boolean_expression> ) <statement>; else <statement>; switch ( <expression> ) { case <constant> : <statements> break; case <constant> : <statements break; default : <statements> } while ( <boolean_expression> ) <statement>; do { <statements> } while ( <boolean_expression> );

// switch statement

// while loop // do-while loop

for ( <initialization>; <continuation_expression>; <increment_statement>) <statement>; class <class_name> { public: <function_prototypes> protected: <function_prototypes> private: <function_prototypes> <data_attributes> }; // class header (prototype)

<class_name>::<function_name> ( <parameter_list> ) // member function { // implementation <declarations> <statements> } <object_name>.<function_name>(<actual_parameters>); // member function call struct <structure_name> { <type> <name>, <name>, ... ; <type> <name>, <name>, ... ; } // structure definition // field definition(s)

typedef <type> <name>; // definition of new type name typedef <element_type> name <dimensions>; // definition of new array type <dimensions> = [ <int_value> ] [ <int_value> ] ... template < typename T > <type> *<name>; &<name> *<ptr> <ptr>-><member> (*<ptr>).<member> <object_name>.<member> new <type> or new <type>[ <size> ] delete <ptr> or detlete [] <ptr> // template header // // // // // // pointer declaration address of <name> data <ptr> points to access to member through pointer access to member of object ditto

// operator to dynamically allocate memory // return memory to dynamic memory store