CSCI261C/E Lecture 3: C++ Fundamentals (continued) August 31, 2011
?
Alan Turing 1912 - 1954 “Turing Machine” aka “Universal Machine” the man himself bombe code-breaking machine
Review • C++ program structure • Preprocessing directives • main() • {code blocks} • Variable declarations & types (intro) • I/O ( cin, cout )
Comments // a one line comment /* a multi- line comment */ /* omg! */
Semicolon; ...is like a period at the end of a sentence. EXCEPT for preprocessing directives (they are special) #include<somelibrary> dude, no semicolon!
C++ is Case-Sensitive double Porsche; double porsche; double pOrScHe; These are three different variables!
Variables • are names pointing to values (like algebra) • should start w/ lower case • should not contain special characters • ($%^&@#! and the like)
Syntax Notation Guide [ this is optional ]
Declaring Variables [modifier] type name [= initial value]; [modifier] type name [(initial value)]; int x; int q = 0; double height(23.0); const int DAYS = 7; double z, y(0); // two at once, for style char initial = ‘j’; // character values need single-quotes
Modifiers const (for now)
Types • short, int, long • float, double, long double • bool • char • string (#include<string>)
An Abrupt Introduction to Classes and Objects
...to be continued
Symbolic Constant const double PI = 3.14159; • it’s what you think it is (just a constant) • cannot change its value • for style, use ALL CAPS • often declared outside of main()
Operators • think algebra • =, +, -, *, /, % • assignment (=) is not equality (==)! • follow precedence rules (p52)
Increment & Decrement (with style) int x; x = 1; x++; x = x + 1;
Order Matters int x, y; int x, y; x = 10; x = 10; y = ++x - 3; y = x++ - 3; x = x + 1; y = x - 3; y = x - 3; x = x + 1;
Other “Tricks” • multiple assignment (p56)
Operations w/ Multiple Types int age = 21; double grade = 4.0; age = 21.5; // age = 21 grade = 2; // grade = 2.0 grade = ‘a’; // grade = ?* * the ascii numeric value for the lower case letter ‘a’
simple text formatting with cin and cout • see p58 - 62
Functions in the Standard C++ Library #include<cmath> Gives you the power of... fabs(x) sqrt(x) pow(x,y) log(x) and more!
Functions (in brief) Are like “mini programs” your program can use Are abstractions Take input (usually) Return a value (usually) const double PI = acos(-1.0);
Homework • First read 2.8 (p 78 - 82) • Continue reading all of chapter 2 • Complete assignment 03_aircraft
More recommend