CPSC 427a: Object-Oriented Programming

Michael J. Fischer

Lecture 9
September 30, 2010

Remarks on Problem Set 2

Seth Hamman

Coding conventions for this course

Coding conventions (cont.)

Error checking and error handling

What should your program do with “bad” inputs? Options:

  1. Detect and report/handle all bad inputs.
  2. Detect and report bad inputs that would cause unexpected behavior such as a crash, divide by 0, and so forth, but don’t check for inputs that violate the input specs if they are handled reasonably by your program. (E.g., a 6-digit number where the specs said to expect a 5-digit number.)
  3. Don’t bother with error checking.

(1) is most desirable.

(2) is acceptable in this course but not in industrial strength code where the specs and code should agree.

(3) is not acceptable.

Design choices

Programming involves design choices.

There is not always one “right” choice; rather, there are tradeoffs, and the choice requires judgement.

Use report.txt to discuss and justify the design choices you make in your own programs.

PS2 issues

Classes

What is a class?

Class relationships

Classes relate to and collaborate with other classes.

Many ways in which one class relates to other.

We first explore derivation, where one class modifies and extends another.

Derivation

What is derivation? One class can be derived from another.

Syntax:

    class A {  
    public:  
       int x;  
       ...  
    };  
    class B : public A {  
       int y;  
       ...  
    };

A is the base class; B is the derived class.

B inherits data members from A.

Instances A base class instance is contained in each derived class instance.

Similar to composition, except for inheritance.

Function members are also inherited.

Data and function members can be overridden in the derived class.

Derivation is a powerful tool for allowing variations to a design.

Some uses of derivation

Derivation has several uses.

Construction, Initialization, and Destruction

Structure of an object A simple object is like a struct in C.

It consists of a block of storage large enough to contain all of its data members.

An object of a derived class contains an instance of the base class followed by the data members of the derived class.

Example:

  class B : A { };

  B b;

Then “inside” of b is an A-instance!

Referencing a composed object

Constrast the previous example to

  class B { A a; };

  B b;

Here B composes A.

The embedded A object can be referenced using data member name a.

Referencing a base object

How do we reference the base object embedded in a derived class?

Example:

    class A { public: int x; int y; };

    class B : A { int y; };

    B b;

Initializing an object

Whenever a class object is created, one of its constructors is called.

If not specified otherwise, the default constructor is called.

This is the one that takes no arguments.

If you do not define the default constructor, then the null constructor (which does nothing) is used.

This applies not only to the “outer” object but also to all of its embedded objects.

Construction rules

The rule for an object of a simple class is:

  1. Call the constructor/initializer for each data member object in sequence.
  2. Call the constructor for the class.

The rule for an object of a derived class is:

  1. Call the constructor for the base class recursively.
  2. Call the constructor/initializer for each data member object of the derived class in sequence.
  3. Call the constructor for the derived class.

Destruction rules

When an object is deleted, the destructors are called in the opposite order.

The rule for an object of a derived class is:

  1. Call the destructor for the dervied class.
  2. Call the destructor for each data member object of the derived class in reverse sequence.
  3. Call the destructor for the base class.

Constructor ctors

Ctors (short for constructor/initializors) allow one to supply parameters to implicitly-called constructors.

Example:

class B : A {  
  B( int n ) : A(n) {};  
      // Calls A constructor with argument n  
};

Initialization ctors

Ctors also can be used to initialze primitive (non-class) variables.

Example:

class  B {  
  int x;  
  const int y;  
  B( int n ) : x(n), y(n+1) {}; // Initializes x and y  
};

Multiple ctors are separated by commas.

Ctors present must be in the same order as the construction takes place – base class ctor first, then data member ctors in the same order as their declarations in the class.

Initialization not same as assignment

Previous example using ctors is not the same as writing

   B( int n ) { y=n+1; x=n; };

Copy constructors