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)