C++ Program Structure
# includes of header files, similar to importing
Programming Constructs packages in Java
Topics int // return type
Program structure main() // entry point in code
Declarations
Operators
{ // beginning of main
Control Flow declaration // define names & types of variables
If-then-else
Varieties of Loops statements // code
Switch Statements
return 0; // return value of your program
} // end of main
Example Program Equivalent Example Program
#include <iostream
<iostream>
> #include <iostream
<iostream>
>
using namespace std;
int
main() int
{ main()
std::cout
std::cout << "Hello World!" << std::endl
std::endl;; {
return 0; cout << "Hello World!" << endl;
endl;
} return 0;
}
Page 1
Breaking down the code C++ Preprocessor
#include <iostream>
iostream> #define CONSTANT1 “You will learn a lot \
Include the contents of the file iostream.h in this class"
Case sensitive – lower case only
No semicolon at the end of line
int main() or main(int
main(int argc,
argc, char*
char* argv[])
argv[]) int main(int
main(int argc,
argc, char*
char* argv[])
argv[])
This function is called when the program starts running. {
cout cout << CONSTANT1;
Prints out a string or series of strings return 0;
}
After the preprocessor Conditional Compilation
int main(int
main(int argc,
argc, char*
char* argv)
argv) #define ECE106
{
int main(int
main(int argc,
argc, char*
char* argv)
argv)
cout <<“You will learn a lot in this class”; {
return 0; #ifdef ECE106
cout << "You will learn a lot in this class";
}
#else
cout << "Some other class";
#endif
return 0;
}
Page 2
Like Java, like C Precedence of C++ operators
Operators Associativity
Operators same as Java: () [] -> . left to right
Arithmetic ! ~ ++ -- + - * & (type) sizeof right to left
i = i+1; i++; i--; i *= 2; * / % left to right
+, -, *, /, %, + - left to right
Relational and Logical << >> left to right
<, >, <=, >=, ==, != < <= > >= left to right
&&, ||, &, |, ! == != left to right
Syntax same as in Java: & left to right
if ( ) { } else { } ^ left to right
while ( ) { } | left to right
&& left to right
do { } while ( ); || left to right
for(i=1; i <= 100; i++) { } ?: right to left
switch ( ) {case 1: … } = += -= *= /= %= &= ^= != <<= >>= right to left
continue; break; , left to right
Note: Unary +, -, and * have higher precedence than binary forms
Simple Data Types Programmer gotchas
datatype size values Uninitialized variables
char 1 -128 to 127 catch with –Wall compiler option
short 2 -32,768 to 32,767
int 4 -2,147,483,648 to 2,147,483,647 #include <stdio.h>
stdio.h>
long 4 -2,147,483,648 to 2,147,483,647
float 4 3.4E+/-
3.4E+/-38 (7 digits)
int main(int
main(int argc,
argc, char*
char* argv[])
argv[])
double 8 1.7E+/-
1.7E+/-308 (15 digits long)
{
int i;
Can do type casts just like in Java
Can find out the size of any type or object with sizeof (type) factorial(i);
return 0;
}
Page 3
If Example “Do-While” Loop Example
C++ Code
C++ Code int func
(int x)
int max(int x, int y) {
{ int result = 1;
if (x > y) do {
return x; result *= x;
else x = x-1;
return y; } while (x > 1);
} return result;
}
Only continue looping when “while” condition holds
General “Do-While” Translation “While” Loop Example
C++ Code C++ Code
do int func_while
Body (int x)
while (Test); {
int result = 1;
while (x > 1) {
Body can be any C statement result *= x;
x = x-1;
Typically compound statement:
};
{ return result;
Statement1; }
Statement2;
…
Statementn;
}
Is this code equivalent to the do-while version?
Test is expression returning integer
= 0 interpreted as false ≠ 0 interpreted as true
Page 4
“For” Loop Example
int func_for(int x, unsigned p) {
“For” Loop Example
int result; General Form
for (result = 1, int i = 0; i < p; i++) { for (result = 0,
result *= x; int i = 1; i < p; for (Init; Test; Update )
} i++) {
return result; result *= x; Body
} }
Init Test Update
result = i < p i++
0, i = 1
{
Body result *= x;
}
→ Switch
→ “While”
“For”→ typedef enum
{ADD, MULT, MINUS, DIV, MOD, BAD}
op_type; Statements
For Version While Version
Implementation Options
for (Init; Test; Update ) Init; char unparse_symbol(op_type op)
{ Series of conditionals
while (Test ) {
Body switch (op) { Avoids many if’s
Body
Update ; case ADD : Possible when cases
} return '+'; are small integer
Do-While Version case MULT: constants
return '*';
Init; case MINUS: In example code
if (Test) return '-'; No default given
do { case DIV: Instead of return can
Body return '/'; have break
Update ; case MOD:
} while (Test); return '%';
case BAD:
return '?';
}
}
Page 5
typedef enum Switch typedef enum Switch
{ADD, MULT, MINUS, DIV, MOD, BAD} {ADD, MULT, MINUS, DIV, MOD, BAD}
op_type; Statements op_type; Statements
Implementation Options Implementation Options
char unparse_symbol(op_type op) char unparse_symbol(op_type op){
{ Series of conditionals int val; Series of conditionals
switch (op) { Avoids many if’s switch (op) { Avoids many if’s
case ADD : Possible when cases case ADD : Possible when cases
return '+'; are small integer val = '+'; break; are small integer
case MULT: constants case MULT: constants
return '*'; val = '*'; break;
case MINUS: In example code case MINUS: In example code
return '-'; No default given val = '-'; break; No default given
case DIV: Instead of return can case DIV: Instead of return can
return '/'; have break val = '/'; break; have break
case MOD: case MOD:
return '%'; val = '%'; break;
case BAD: case BAD:
return '?'; val = '?'; break;
default: return ‘0’;//no match default: val = ‘0’;//no match
} }
} return val;
}
typedef enum Switch Summarizing
{ADD, MULT, MINUS, DIV, MOD, BAD}
op_type; Statements C Control
Implementation Options if-then-else
char unparse_symbol(op_type op){ do-while
int val; Series of conditionals
Avoids many if’s
while
switch (op) {
case ADD : Possible when cases switch
val = '+'; are small integer
case MULT: constants
val = '*';
case MINUS: In example code
val = '-'; What happens if I omit
case DIV: the breaks ??
val = '/'; Fall-through after the
case MOD: first match
val = '%'; In the example, the
case BAD:
returned value will
val = '?';
always be ‘0’
default: val = ‘0’;
}
return val;
}
Page 6
Binary Representations
Base 2 Number Representation
Memory Organization: Bytes Represent 1521310 as 111011011011012
Represent 1.2010 as 1.0011001100110011[0011]… 2
Topics 0/1 are bits
Why bits? A byte = 8 bits
Representing information as bits
Binary/Hexadecimal
Byte representations
» numbers
» characters and strings
» Instructions
Byte-Oriented Memory Organization Encoding Byte Values
Programs Refer to Memory Addresses Byte = 8 bits al
m ry
x ci i na
Conceptually very large array of bytes Binary 000000002 to 111111112 He De B
0 0 0000
Address space private to particular “process” Decimal: 010 to 25510 1 1 0001
Program being executed Hexadecimal 0016 to FF16 2 2 0010
3 3 0011
Program can clobber its own data, but not that of others Base 16 number representation 4 4 0100
Use characters ‘0’ to ‘9’ and ‘A’ to ‘F’ 5 5 0101
Compiler + Run-
Run-Time System Control Allocation 6 6 0110
Write FA1D37B16 in C as 0xFA1D37B 7 7 0111
Where different program objects should be stored » Or 0xfa1d37b 8 8 1000
Multiple mechanisms: static, stack, and heap 9 9 1001
A 10 1010
In any case, all allocation within single address space B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
Page 7
Machine Words Main Points
Machine Has “Word Size” It’s All About Bits & Bytes
Nominal size of integer-valued data Numbers
Including addresses Programs
Machines support multiple data formats Data has size in bytes
Fractions or multiples of word size
Word size
Always integral number of bytes
Page 8