2 From C to C++Object Orientation -:: General Program Template
2
FROM C TO C++OBJECT ORIENTATION -
2.1
General Program Template #include vs. #include using namespace std; int main(void) { // or int main() definitions/declarations/statements return expr; }
Exercise 2.1 Compile and execute this program printing Hello World on the screen.
2.2
Keywords and Identifiers
❍ Keywords asm bool char continue double except finally goto long operator register signed struct throw typedef unsigned volatile
auto break class default dynamic_cast explicit float if mutable private reinterpret_cast sizeof switch true typeid using while
bad_cast case const delete else extern for inline namespace protected return static template try typename virtual
bad_typeid catch const_cast do enum false friend int new public short static_cast this type_info union void
❍ Alternative operators bitand compl not_eq
and and_eq
cs2260@UMSL 2005 Cezary Z. Janikow
bitor or_eq
or xor_eq
xor not
Page 5
2 From C to C++Object Orientation -:: Comments
❍ Do not use identifiers starting with (used for system names) ❍ _ _ letter ❍ _ Letter ❍ Naming standards ❍ variable/object ❍ Class/Type Example 2.1 Naming examples. class Car; class MyCar; int numOfItems; Car myLegend;
2.3
Comments // comment up to eol only /* can also use C style */
2.4
New Console IO
Example 2.2 Illustration of the standard device connection through stream objects. cout:ostream Program cerr:ostream
cin:istream
cs2260@UMSL 2005 Cezary Z. Janikow
Console
Kb
Page 6
2 From C to C++Object Orientation -:: Anywhere declaration/definition
❍ cin and cout are buffered ❍ all are formatted ❍ types are automatically detected on compilation ❍ overloaded > for output and input ❍ endl vs. ‘\n’ Example 2.3 > can be chained and the types are detected by the compiler. cout x >> y;
Exercise 2.2 Program to read a number and print it within a string.
2.5
Anywhere declaration/definition ❍ Anywhere before being used but try keeping together ❍ Also inside for loops for (int i=0; i> x) cout