EE6411 2009 C++ Unit01 Basic Programming
EE6411 2009 C++ Unit01 Basic Programming
EE6411/ED5021: Object Oriented Programming with C++ 1 EE6411/ED5021: Object Oriented Programming with C++ 2
Keywords Comments
asm, auto, bool, break, case, catch,
char, class, const, const_cast, • C-style comments:
continue, default, delete, do, double, /* This is a comment */
dynamic_cast, else, enum, explicit, /* Comments are not re-
export, extern, false, float, for,
goto, if, inline, int, long, mutable, stricted to a single line,
namespace, new, operator, private, must not be nested, cannot
protected, public, register, appear in string */
reinterpret_cast, return, short, • C++ style comments:
signed, sizeof, static, static_cast,
struct, switch, template, this, throw,
// this is C++ comment, these
true, try, typedef, typeid, typename, // are restricted to single
union, unsigned, using, virtual, void, // line
volatile, wchar_t, while
EE6411/ED5021: Object Oriented Programming with C++ 5 EE6411/ED5021: Object Oriented Programming with C++ 6
1
Basic Data Types Basic Types (cont.)
• Floating Point Types:
• Signed Integer: float, double, long double
short, int, long Sizes: float <= double <= long double
Sizes: short <= int <= long • Boolean Type:
Keyword “signed” is optional bool
• Unsigned Integer: Symbolic values true/false, automatic
unsigned short, unsigned int, conversion from integer: 0=false, else true
unsigned long • Empty Type:
void
• Character Type:
used for functions with no parameter, functions with
char, signed char, unsigned char
no return type, generic pointer
EE6411/ED5021: Object Oriented Programming with C++ 7 EE6411/ED5021: Object Oriented Programming with C++ 8
EE6411/ED5021: Object Oriented Programming with C++ 9 EE6411/ED5021: Object Oriented Programming with C++ 10
2
Integer Constants Floating Constants
• type int (plain number), e.g z = 55; • Can have fractional part, e.g. x = 3.14;
• long (suffix l/L), e.g. z = 55L; • Can have exponent, e.g. x = 314e-2;
• signed or unsigned (suffix u/U) , e.g. z = 55, • Are only approximations, due to internal
z = -55, z = 55U, z = 55ul; representation
• decimal (start with digit ≠ 0), e.g. z = 55; • => Be aware of possible rounding errors !!!
• octal (start with 0), e.g. z = 055; // = 45 • By default of type double, use suffix f/F for float
• hexadecimal (prefix 0x), e.g. and suffix l/L for long double
z = 0x55; // = 85
EE6411/ED5021: Object Oriented Programming with C++ 13 EE6411/ED5021: Object Oriented Programming with C++ 14
EE6411/ED5021: Object Oriented Programming with C++ 15 EE6411/ED5021: Object Oriented Programming with C++ 16
3
The First Program: Hello World Basic I/O
• C++ has no native I/O commands
• Standard I/O Library: #include <iostream>
• I/O operates on streams => abstracts hardware,
I/O to console & files essentially the same
• Three standard streams:
C C++ Description
stdin cin Standard Input Stream
stdout cout Standard Output Stream
stderr cerr Standard Error Stream
EE6411/ED5021: Object Oriented Programming with C++ 19 EE6411/ED5021: Object Oriented Programming with C++ 20
I/O Manipulators
Simple Variable Input & Output
boolalpha Use symbolic bool
int i; noboolalpha Use numeric bool
double x; dec Integer are decimal
cout << “Please enter integer: “ << endl; hex Integer are hex
cin >> i; oct Integer are octal
cout << “Please enter double: “ << endl; flush Flush stream
cin >> x; endl Print newline and flush
cout << “Integer i = “ << i << left Left justification
“\n double x = “ << x << endl; right Right justification
setw(w) Minimum field width = w
setprecision(p) Number of digits = p
EE6411/ED5021: Object Oriented Programming with C++ 21 EE6411/ED5021: Object Oriented Programming with C++ 22
#include <iostream>
#include <iomanip> Statements
using namespace std;
int main() { • Statements are closed with ‘;’
cout << setprecision(2) << 1000.243 • Formally statement has neither type nor value
<< endl;
• Statements are executed in sequential order
cout << setw(10) << “Hello” << endl;
return 0; • Control structures change order
} 1. Empty Statement: ;
Output: 2. Expression Statement: expression;
3. Compound Statement: {
1e+003
statements
Hello
}
EE6411/ED5021: Object Oriented Programming with C++ 23 EE6411/ED5021: Object Oriented Programming with C++ 24
4
Statements (cont.) Statements (cont.)
4. if-Statement:
if (expression) statement
7. switch-Statement:
5. if-else-Statement: switch(expression) {
if (expression) statement1 case constant1: statements1
else statement2
case constant2: statements2
6. if-else-if-Statement:
if (expression1) statement1 ...
else if (expression2) statement2 case constantN: statementsN
…
else if (expressionN) statementN default: statements
else statement }
EE6411/ED5021: Object Oriented Programming with C++ 25 EE6411/ED5021: Object Oriented Programming with C++ 26
EE6411/ED5021: Object Oriented Programming with C++ 29 EE6411/ED5021: Object Oriented Programming with C++ 30
5
Arithmetic Operators Bitwise Operators
+ Pos. sign - Neg. sign
& Bitwise AND | Bitwise OR
++ Increment -- Decrement
^ XOR ~ One’s complement
* Multiplication / Division
>> Shift right << Shift left
% Modulus division &= |= OR assignment
AND assignment
+ Addition - Subtraction ^= XOR assignment
= Assignment %= Modulus Assignment >>= Shift right assignment <<= Shift left assignment
*= Mult Assignment /= Div. Assignment
+= Add Assignment -= Sub Assignment
EE6411/ED5021: Object Oriented Programming with C++ 31 EE6411/ED5021: Object Oriented Programming with C++ 32
EE6411/ED5021: Object Oriented Programming with C++ 33 EE6411/ED5021: Object Oriented Programming with C++ 34
6
Automatic Type Conversion Rules
Expressions With Multiple Types
• If one operand is of type long double, then the other is
• Expressions have type & value converted also to long double.
• Else, if one operand is double, then the other is converted
• Operands within expression must be of equal types also to double.
• Conflicting types will be adjusted (where possible) • Else, if one operand is float, then the other is converted
• Examples: (assume int a, double x) also to float.
5/2 • Else any operand of type char and short is converted to int.
• If then any operand is of type long, the other is also
5/2.0 converted to long.
a = 3.141 • Comparisons between signed and unsigned values are
x = a = 3.14 * 5 machine dependent => Conversion of unsigned types are
more complicated.
• Assignment: RHS always converted to LHS-type
EE6411/ED5021: Object Oriented Programming with C++ 37 EE6411/ED5021: Object Oriented Programming with C++ 38
EE6411/ED5021: Object Oriented Programming with C++ 39 EE6411/ED5021: Object Oriented Programming with C++ 40
• static_cast<type> (expression);
Substitute for C-cast operator
• dynamic_cast<type> (expression);
Run-time cast that verifies validity of cast, target
class must be pointer/reference type
• const_cast<type> (expression);
Alters const/volatile property of expression
• reinterpret_cast<type>(expression);
Converts fundamentally different types (meaning),
e.g. int->pointer, types must be similar (internally)
EE6411/ED5021: Object Oriented Programming with C++ 41 EE6411/ED5021: Object Oriented Programming with C++ 42
7
Functions – The Units of a Program
The Function main()
• Functions divide programs into separate parts
• Is entry point from operating system
• Function is self-contained code
• Simple main: int main() {
• Should perform a (single) well-defined action
statements
• Can have any number of parameter of any type }
• In function call parameter are given value by • Complete Declaration:
expressions: any expression of correct type is valid int main(int argc, char *argv[]);
• Can have optional return-value: any expression of
correct type can be returned
• argc: number of parameter (name is first)
• argv: parameter values as strings
EE6411/ED5021: Object Oriented Programming with C++ 43 EE6411/ED5021: Object Oriented Programming with C++ 44
EE6411/ED5021: Object Oriented Programming with C++ 45 EE6411/ED5021: Object Oriented Programming with C++ 46
Functions
• Declaration => make function known Declaration vs Definition
double max(double a, double b);
double max(double, double); • Declaration makes entity known to system
• Definition => define instructions – No memory is allocated, no entity is created
double max(double a,double b) { – Entity must be defined somewhere else
statements – bool odd(int x);
} – extern int numOfElements;
• Multiple Declarations, Single Definition • Definition creates entity
• Must be “known” before they can be called: – Memory is allocated for function/object/variable
– Implies declaration & can be declared somewhere else
1. Define function before it’s called
– bool odd(int x) { return (x & 1);}
2. Declare all functions at beginning and define – int numOfElements;
them later
EE6411/ED5021: Object Oriented Programming with C++ 47 EE6411/ED5021: Object Oriented Programming with C++ 48
8
Importance of Systematic Program
Top-Down Construction of Functions
Construction
• Programs are “sensitive” => small changes can 1. Formal specification of problem
have massive effects
2. Problem solving (generic – hardest part)
• First American probe to Venus ended up around
the sun as the line 3. Describe solution in high-level steps
DO 100 I = 1 . 10 4. Refine each step iteratively
Was used instead of • Use generic steps initially
DO 100 I = 1 , 10 • Converge slowly towards target-language
5. Implement algorithm in programming
language
EE6411/ED5021: Object Oriented Programming with C++ 49 EE6411/ED5021: Object Oriented Programming with C++ 50