Constructs4 C++
Constructs4 C++
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;
}
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
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;
}
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
} }
→ 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;
}
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
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