Documente Academic
Documente Profesional
Documente Cultură
Course layout
Week 1
Week 2
return 0; semi-colons
}
run
build
C++
Data types
C++ is a programmer’s language - need to know the
basics…
There are 6 atomic data types:
1) char - character (1 byte)
2) int - integer (usually 4 bytes)
3) float - floating point (usually 4 bytes)
4) double - double precision floating point (usually 8 bytes)
5) bool - true or false (usually 4 bytes)
6) void - explicitly says function does not return a value
and can represent a pointer to any data type
Size of the data types depends on machine architecture
e.g. 16 bit, 32 bit or 64 bit words
Other data types are derived from atomic types e.g. long
int
Can use ‘typedef’ to alias your own data type names;
defining C++ classes creates new types
Variables and scope C++
a++;
b--;
means the same as
a = a + 1;
b = b - 1;
Statement categories:
1) Selection
2) Iteration
3) Jump
4) Expression
5) Try (exception handling; look it up)
(!flag)
evaluates ‘true’ if a is
(a>0.0 && flag) zero or flag is false
(a==0.0 || !flag)
ch of type ‘char’
switch (ch) { (could be ‘int’)
case ‘a’:
doMenuOptionA();
control jumps here break;
if ch equals ‘b’ case ‘b’: This is a label,
doMenuOptionB(); and so has
break; a ‘:’ after it
control jumps to .
.
end brace
. Note! As in ‘if’, there
}
is NO ‘;’ after braces
Iteration: ‘for’ loop C++
char c = ‘\0’;
initialise the char variable
while (c != ‘q’) {
cout << “Type a key (q to
finish): “;
cin >> c;
} The loop body will execute
forever - until “q” is pressed
cout << “Finished\n”;
C++
Jump: ‘goto’, ‘return’, ‘break’ and ‘continue’
Personal bigotry: using ‘goto’ just shows bad design; do not use
int count = 0;
infinite loop int i;
int a = 0;
for (i=0; i<10; i++) {
while (true) { if (i<3 || i>6) {
a++; continue;
if (a >= 10) { }
break; count++;
} jump out of loop } Jump to beginning
} (if loops nested, only of loop and begin
jump out of inner) next iteration
C++
Expressions
An expression statement is anything that ends with a ‘;’
func();
function call
a = b + c;
o Two unary operators are used frequently with pointers: * and &
pe = e = &e[0];
class Andy {
// definition of stuff
};
#include <iostream.h>
ifstream inFile;
ofstream outFile;
float number;
inFile.open(“readme.txt”);
outFile.open(“writeme.txt”); Keep going until the
end of file is reached
while (!inFile.eof()) {
inFile >> number;
outFile << number;
} These are methods of the base
classes ‘ios’ and ‘fstream’…
inFile.close(); ‘inheritance’ covered next week!
outFile.close();
Pre-processor instructions
C++
Pre-processor instructions allow the programmer to choose
what source code the compiler sees - very useful for writing
programs that are portable between Unix and Windows
ifdef WIN32
#include <someWindowsHeader.h>
void myFunc(/* Windows data
types */);
#else
#include <someUnixHeader.h>
void myFunc(/* Unix data types
*/);
#endif
Can use verbose output in a function to help with debugging
#ifdef _DEBUG
cout << “Debug: at end of ‘func1’ x = “ << x << “\n”;
#endif