Lecture Notes for Advanced Programming II
13 March 2001 - Virtual Classes
- to review
- classes without inheritance are great
- use inheritance to get polymorphism amoung classes
- virtual members give useful polymorphism - search from the bottom
- the virtual issues -
- what should be virtual
- destructors
- coupling
- what should be virtual
- make everything virtual - but that's expensive
- cost in time and space
- destructors
- what's the problem?
- non-virtual destructors in the parent class
- the child-class destructors can get skipped
- a puzzle
class p {
public:
~p() { cout << "delete a p.\n"; }
};
class c : public p {
public:
virtual ~c() { cout << "delete a c.\n"; }
};
void main(void) {
p * pp = new c;
delete pp;
c * cp = new c;
delete cp;
}
- what's the output
- coupling
- coupling is a measure of independence among modules the more coupling
between two modules, the less independent they are
- low coupling is important - easy to create, understand, modify
- coupling is hard to quantify
- value couples less strongly than behavior - behavior is function; value
is data
This page last modified on 27 February 2001.