Lecture Notes for Advanced Programming II

16 January 2001 - Classes


  1. classes

    1. collections of related entities

    2. the collection models a single, well defined entity - measure of class goodness

    3. class definitions are usually split into interfaces (.h files) and implementation (.cc) files - except for templates

  2. members

    1. variables, functions, types

    2. members may be public or private - the default is private

      1. class light_switch {
        
          public:
        
            enum state { on, off };
            
            void set_switch(state ss) {
              switch_state = ss;
              }
        
          private:
        
            state switch_state;
          };
        

  3. scope

    1. anything within a class always has direct access to everything in the class

    2. 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

      1. legal: light_switch ls; ls.set_switch(on)

      2. illegal: light_switch ls; ls.switch_state = on

    3. the scope operator :: is an alternative way to access class members

      1. light_switch::state ls_state; ls_state = light_switch::on

  4. constructors and destructors

    1. when an object gets made, the object's constructor is called

    2. has the same name as the object, has no return type

    3. the default constructor has no parameters

      1. the compiler creates a simple default constructor only if no constructors are defined

    4. the copy constructor has a single constructor, which is a constant reference to an object of the same type

      1. the compiler will not create a default copy constructor

      2. the copy constructor is important when the class contains dynamic data - what copies the data?

    5. when an object gets deleted, its destructor is called

      1. an object is deleted when it goes out of scope, or delete is called.

      2. has the same name as the object with a tilde ~ in front; accepts no arguments, returns no value

      3. destruction is subtle and be a source of nasty bugs - don't play around with it

      4. defining a destructor is important when a class contains dynamic data - the dynamic data has to be freed by something

  5. inheritance

    1. one class can be related to another - parent-child; superclass-subclass; base class-derived class

      1. the relation may be extended through several classes - ancestors and descendents

    2. the parent-child relation indicates an is-a relation - the child is an example of the parent

      1. our old friend - a square is an example of a quadrilateral is an example of a polygon

      2. 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

      3. is-a typing supports polymorphism - a powerful way of structuring software

    3. a child has access to a parent's non-private members - re-use inheritance

      1. re-use inheritance is generally considered to be a poor basis for class design

  6. resolving multiple members

    1. the same member may appear in several ancestors - which one to choose

    2. choose the bottom one - but there's a problem

    3. choose the bottom one conflicts with using the parent as a representative type - drawing geometric figures

    4. 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.