COMP2004
Programming Practice
2002 Summer School


Kevin Pulo
School of Information Technologies
University of Sydney


(page 1)


Exam information


Main Quadrangle



(page 2)


Course Overview




(page 3)


Course Overview






(page 4)


Course Overview





(page 5)


Basic C++




























(page 6)


Passing parameters




(page 7)


Pointers


int main() {
int i = 42;
int *p = &i;
*p = 3;
p = NULL;
if (p)
cout << *p << endl;
}

















(page 8)


Arrays


int main() {
int a[10];
int b[ ] = { 1, 2, 3 };
char c[ ] = "a c-style string";
char *d = c;

a[3] = b[1];
cout << *c << *d << endl;
d += 3;
cout << d[1] << *(c + 4) << endl;
}


(page 9)


Arguments to main()


int main(int argc, char **argv) {
if (argc >= 3) {
cout << argv[2] << endl;
}
}

bash$ prog These are the args the


(page 10)


Memory Allocation






(page 11)


Memory Leaks


int main() {
int *data = new int(4);
data = new int(12);
data = new int(42);
delete data;
}


(page 12)


Object-oriented C++



Student s;
Student *sp = &s;
s.sid = "9222194";
sp->courses.push_back("comp2004");


(page 13)


Example minimal class


class A {
public:
A();
A(const A& o);
~A();
A& operator=(const A& o) {
if (this == &o) return *this;
...
}
};


(page 14)


Const correctness


class List {
int length() const;
void clear();
}
l.clear(); // not allowed











(page 15)


Operator overloading


const List& list) {
...
return os;
}













(page 16)


Type conversions





(page 17)


Inheritance


...
};



(page 18)


Accessibility





(page 19)


Virtual methods


void activate(Alarm& a) {
a.turn_on();
}
BuzzerAlarm b;
activate(b);


(page 20)


Pure virtual methods


class Alarm {
virtual void turn_on() = 0;
};



(page 21)


Namespaces


namespace lib {
string func() { ... }
}

cout << lib::func() << endl;
using namespace lib;
cout << func() << endl;


(page 22)


Unix development tools




(page 23)


make


OBJS = main.o student.o course.o
CXXFLAGS = -Wall -g
LDFLAGS = -lm

all: prog
prog: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) \
-o prog $(OBJS)
clean:
rm -f $(OBJS)


(page 24)


gdb


Segmentation fault (core dumped)


(page 25)


Simple shell




(page 26)


Shell / environment variables




(page 27)


If



if cmp file1.txt file2.txt; then
echo "files same"
elif diff -i file1.txt file2.txt; then
echo "files same except case"
else
echo "files different"
fi


(page 28)


Alternate if




(page 29)


stdout -> shell variable


echo "f1.h f2.h f3.h" > file.lst
filelist=`cat file.lst`
cat $filelist
cat `cat file.lst`
cat $(cat file.lst)


(page 30)


while


count=1
while [ $count -le 10 ]; do
echo "$count"
count=$(($count + 1))
done


while :; do
if [ -e "file.txt" ]; then
break
fi
sleep 1
done


(page 31)


Command line parameters


#!/gnu/usr/bin/bash
while [ $# -ge 2 ]; do
echo "$1" "$2"
shift
done


bash$ sample a bc defgh a bc
bc defgh


(page 32)


case


case "$1" in
-d*)
cmd="diff"
;;
-c*)
cmd="cmp"
;;
*)
exit 1
;;
esac
$cmd file1.txt file2.txt


(page 33)


for and pipes


#!/gnu/usr/bin/bash
for infile in tests/*.in; do
name=`basename $infile .in`
if prog < $infile | cmp - \
tests/$name.out; then
echo "$name : passed"
else
echo "$name : failed"
fi
done


(page 34)


Useful Unix utilities





(page 35)


Course survey




(page 36)


Advanced C++




(page 37)


Template Functions


template
typename vector::size_type
find(const vector &v,
const T &value) {
for (typename vector::size_type
i = 0; i < v.size(); ++i)
if (v[i] == value) return i;
return v.size();
}
vector vi;
find(vi, 42);


(page 38)


Template Classes


template
class Node {
T value;
Node *next;
...
};
  • Heavy reliance on operators
  • All template code in .h files
    • Only time this is allowed


(page 39)


Exceptions






(page 40)


Exceptions


struct Error { int i;
Error(int i) : i(i) {} };
struct OtherError { };


try {
throw Error(42);
} catch (Error e) {
cout << e.i << endl;
throw OtherError();
} catch (...) {
throw;
}


(page 41)


Exception Safety




(page 42)


Exception Unsafe Code


void some_function(string name) {
Person *fred = new Person();

fred->setName(name);

delete fred;
}


(page 43)


Fixing with try/catch


void some_function(string name) {
Person *fred = new Person(name);
try {
fred->setName(name);
} catch (...) {
delete fred;
throw;
}
delete fred;
}


(page 44)


Fixing with auto_ptr


void some_function(string name) {
Person *fredp = new Person(name);
auto_ptr fred(fredp);

fred->setName(name);
}


(page 45)


String streams




(page 46)


New style


int main() {
string s = "42 15";
istringstream is(s);
int i, j;
is >> i >> j;

ostringstream os;
os << i << "." << j;
s = os.str();
cout << s << endl;
}


(page 47)


C++ STL library




(page 48)


Iterators





(page 49)


Iterator Ranges




(page 50)


Common output iterators






(page 51)


Containers




(page 52)


STL Container definitions




(page 53)


Container abstractions




(page 54)


Sequence types




(page 55)


Container abstractions




(page 56)


Associative Container types




(page 57)


Adapter containers




(page 58)


Function Objects




(page 59)


STL Function Objects




(page 60)


Adapter Function Objects




(page 61)


Algorithms




(page 62)


Algorithms




(page 63)


Mutating Algorithms




(page 64)


Mutating Algorithms




(page 65)


Exam information


Main Quadrangle




(page 66)