Notes
Slide Show
Outline
1
"Brad Rippe"
  • Brad Rippe
2
C++ layout
  • #include <iostream>
  • using namespace std;


  • int main() {
  • statement1;
  • …
  • statement;
  • return 0;
  • }
3
Storage Containers
4
Variables
  • int someInt = 0;
  • double someDub = 0.0;


  • cout << someInt << endl;
  • cout << someDub << endl;


  • 0
  • 0.0
5
cin/cout
  • cin
    • Sends data from the keyboard
    • >> - extraction operator
    • cin skips blanks and line breaks looking for data
  • cout
    • Sends output to the screen
    • << - insertion  operator

  • \n – newline - used with literal text
  • endl; - newline – used when \n can’t be used


6
Undefined Variables
  • int someInt;
  • double someDub;


  • cout << someInt << endl;
  • cout << someDub << endl;
7
Undefined Variables
8
Assignments - Memory
9
Identifiers
  • Names for a variables
    • Part of the human readability
  • Must start with alpha character or “_”
  • Other characters must be characters must be alpha, numeric, or an “_”
  • Case-sensitive
    • someNum is not somenum
  • Should be descriptive of the data they contain


  • Course uses the following style
    • Lowercase letter for the first word
    • All subsequent words are capitalized
    • Can’t be a C++ reserved word


10
Keywords
  • asm false sizeof
  • auto float static
  • bool for static_cast
  • break friend struct
  • case goto switch
  • catch if template
  • char inline this
  • class int throw
  • const long true
  • const_cast mutable try
  • continue namespace typedef
11
More on Variables
  • int main() {
  • int someInt = -35;
  • double someDub = 1.25;
  • return 0;
  • }
12
Assignment
  • Literal Values
    • someInt = 27;
    • someDub = 33.33;


  • Expressions
    • someInt = 27+ 23;
    • someDub = 5.55 * 1.0;


  • Values of other Variables
    • someInt = someOtherInt;
    • someDub = someOtherDub;
    • someInt = someInt2 + someInt3;
    • someDub = someDub2 / someDub3;
13
Initialization in Declarations
  • int someInt = 0, someOtherInt = 22, myInt = 87;
  • double myDub = 2.2, otherDub = 4.55;
14
Assignments
  • int someInt = 0;
  • int someOtherInt = 22;
  • int myInt = 87;
  • double myDub = 2.2;
  • double otherDub = 4.55;
15
Input and Output Streams


16
Predefined Input and Output
  • Provided by iostream
  • #include directive


  • #include <iostream>
    using namespace std;


17
Output String Literals
  • Enclosed in double quotes
  • Uses the insertion operator   <<


  • cout << “Hello Class\n”
  •      << “Do you prefer Coke or Pespi?\n”
  •      << “What was the score of the Laker “
  • << “Heat Game?\n”;
18
Escape Sequences
  • Text is defined in double quotes “TEXT”
  • Special Characters
  • “\n” – newline
  • “\v” – vertical tab
  • “\r” – carriage return
  • “\a” – alert bell
  • “\”” – double quote
  • “\’” – single quote
  • “\t” – tab
  • “\b” – backspace
  • “\\” – back slash







19
Newlines
  • “\n” is usually used when string literals are output


    • cout << “I love this stuff!\nIsn’t this cool?\n”;
    • cout << “The current lotto numbers are:\n”
    •   << lottoNums << endl;


  • endl – sends a newline to the output stream and flushes the output buffer.
20
cin
  • Assigning values from the keyboard


    • cin >>  numOfHomeOnMarket
    •     >>  avgSellingPrice;



    • >283 634000.99[ENTER]


    • or


    • >283[ENTER]
    • >634000.33[ENTER]
21
Integer Data Types
  • int
    • 32 bits
    • contains whole numbers
    • Ranges from  -2,147,483,648
      to 2,147,483,647
  • double
    • 64 bits
    • Fraction – usually around 14
      decimal places
    • Ranges from 10-308 to 10308
  • Float
    • 32 bits
    • Fraction
    • Ranges from 10-38 to 10 38
22
Format Decimal Point Values

  • double radius = 6.66666667;
  • cout.setf(ios::fixed);
  • cout.setf(ios::showpoint);
  • cout.precision(2);
  • cout << radius << endl;
23
Integer Data Types
  • char
    • 8 bits
    • ASCII character
    • Alphanumeric and
      punctuation
  • short
    • 16 bits
    • Ranges from -32767 to 32767
  • long
    • 32 bits
    • Fraction
    • Ranges from ~ -2 billion t0 ~ 2 billion
24
More Data Types
  • bool
    • 8 bits
    • either true or false
  • string
    • Class type
    • Defined in double quotes
    • Must include the <string> library
    • This is not a primitive type
25
Bool       int
  • The following actions are possible but generally
     not  recommended!
  • Values of type bool can be assigned to int
    variables
    • True is stored as 1  (any positive integer)
    • False is stored as 0
  • Values of type int can be assigned to bool
    variables
    • Any non-zero integer is stored as true
    • Zero is stored as false
26
Variable Declarations & Assignments
  • Declarations
  • type nameOfTheVar;


  • Assignments
  • nameOfTheVar = someValue;


  • One Statement
  • type nameOfTheVar = someValue;



27
Arithmetic Operators
  • Can be used for literal values and variables (in order of precedence)
    • Parentheses ( )
    • Multiplication *
    • Division  /
    • Modulus %
    • Addition +
    • Subtraction –


  • Same precedence as algebraic operations
    • Please Excuse My Dear Aunt Sally
  • Operands can be number or variable
  • Calculating  Right Circular Cylinder   ∏r2h
    • PIE*(r * r)*h
28
Operator Precedence
  • If you were a computer, what would you give as a result for:

  • y = 2 * 5 * 5 + 3 * 5 + 7;


29
 
30
Short Hand Operators
  • foo = foo op bar                         foo op= bar
31
Some Rules
  • If both operands are type int
    • Then the result is an int type


    • 23 * 38 = integer result
    • 23 / 38 = integer result


  • If one of the operands is a double
    • Then the result is a double type
    • 23.56 * 38 = double result
    • 23.56 / 38 = double result
    • 5 / 2.0 = double result
32
Flow of Control
  • cout << "We can output "
  • << "variables values\n";
  • cout << "12/4 = " << 12/4 << endl;
  • cout << "4+12 = " << 4+12 << endl;
  • cout << "18%4 = " << 18%4 << endl;
33
Sequential Execution
34
If statements
  • if (Boolean Expression) {
    • statement1;
    • statement2;
    • ...
  • }


  • if (Boolean Expression) {
    • statement1;
  • }
35
if structure diagram
36
Comparisons Operators
37
“And” and “Or” Operators
  • The “and” operator &&
    • Allow multiple boolean expressions
    • if ((fuel >=  30) && (milesTraveled  > 5000)) {
      • thrust;
      • }


  • The “or” operator  ||
    • Allow multiple boolean expressions
    • if ((fuel >= 30) || (milesTraveled  < 5000)) {
      • thrust;
      • }


38
if / else structure
39
if / else structure diagram
40
Loops
  • Repetition statement (iteration statement)
  • Set of statements is executed as long as the condition remains true


  • Pseudocode
    • While there are more items on my shopping list
      • Purchase next item and cross it off my list
41
while structure
42
while structure diagram
43
Do-while structure
44
do-while structure diagram
45
Infinite loops
  • Try to avoid this situation at all costs
  • The end result is abnormal termination of your application
  • THIS IS A BAD THING!!!
46
Increment and Decrement Operators
47
Comments
  • Single line Comments
    • // - single line comment
      • // Name: Brad Rippe
      • // File: Assignment23.cpp
      • // Date: 1/21/07


  • Multiple line Comments
    • /*  … */
    • /*
    • Name: Brad Rippe
    • File: Assignment23.cpp
    • Date: 1/21/07
    • */
48
Constants
  • Program cannot modify the variable
  • Uses the keyword “const” in the declaration
  • Known as the const modifier
  • Identifier is capitalized
  • Words are separated by “_”


  • const double PIE = 3.14159;
  • const string CON_DRIVER = “SQLServerDriver”;
49
Programming Style
50
Course Style Guide
51
Resources
  • Course Web Site
    • http://staffwww.fullcoll.edu/brippe/csci123
  • Course Style Guide
    • http://staffwww.fullcoll.edu/brippe/csci123/style.aspx
  • Course Syllabus
    • http://staffwww.fullcoll.edu/brippe/csci123/syllabus.aspx
  • Course Schedule (tentative)
    • http://staffwww.fullcoll.edu/brippe/csci123/schedule.aspx