Lecture Notes for Advanced Programming II
16 January 2001 - Classes
- classes
- collections of related entities
- the collection models a single, well defined entity - measure of class
goodness
- class definitions are usually split into interfaces (
.h
files)
and implementation (.cc
) files - except for templates
- members
- variables, functions, types
- members may be public or private - the default is private
-
class light_switch {
public:
enum state { on, off };
void set_switch(state ss) {
switch_state = ss;
}
private:
state switch_state;
};
- scope
- anything within a class always has direct access to everything in the
class
- anything outside a class can access only the public members of a class
using the dot operator or the arrow operator for class-value pointers
- legal:
light_switch ls; ls.set_switch(on)
- illegal:
light_switch ls; ls.switch_state = on
- the scope operator :: is an alternative way to access class members
-
light_switch::state ls_state; ls_state = light_switch::on
- constructors and destructors
- when an object gets made, the object's constructor is called
- has the same name as the object, has no return type
- the default constructor has no parameters
- the compiler creates a simple default constructor only if no
constructors are defined
- the copy constructor has a single constructor, which is a constant
reference to an object of the same type
- the compiler will not create a default copy constructor
- the copy constructor is important when the class contains dynamic
data - what copies the data?
- when an object gets deleted, its destructor is called
- an object is deleted when it goes out of scope, or
delete
is
called.
- has the same name as the object with a tilde
~
in front; accepts
no arguments, returns no value
- destruction is subtle and be a source of nasty bugs - don't play
around with it
- defining a destructor is important when a class contains dynamic data
- the dynamic data has to be freed by something
- inheritance
- one class can be related to another - parent-child;
superclass-subclass; base class-derived class
- the relation may be extended through several classes - ancestors and
descendents
- the parent-child relation indicates an is-a relation - the child is an
example of the parent
- our old friend - a square is an example of a quadrilateral is an
example of a polygon
- is-a relations are supported by typing - a variable of type square
can also be treated as a variable of type quadrilateral or a variable
of type
- is-a typing supports polymorphism - a powerful way of structuring
software
- a child has access to a parent's non-private members - re-use
inheritance
- re-use inheritance is generally considered to be a poor basis for
class design
- resolving multiple members
- the same member may appear in several ancestors - which one to choose
- choose the bottom one - but there's a problem
- choose the bottom one conflicts with using the parent as a
representative type - drawing geometric figures
- virtual functions resolve the conflict between bottom choice and
polymorphism - a virtual function remembers its descendent type and
uses that one when called as an ancestor
This page last modified on 16 January 2001.