CIS 22B - Notes for Tuesday 11/24

Announcements and Reminders

  • Assignment 9 is due now
  • Online time tomorrow night 7 pm
  • No class Thursday
  • Exercise 10 is due Thursday
  • TA Session Friday 3 pm
  • Assignment 10 is due next Tuesday

Recording

Inheritance

Inheritance is a relationship between two classes such that one class takes on (inherits) the properties and behaviors (types, data members and member functions) of another class. The derived class inherits from a base class. This process facilitates code reuse and is a formal method of expressing relationships between types.
Notes

5 Examples
  1. First Inheritance Example
  2. Adding functionality to a class using inheritance
  3. Inherit the deck class
  4. Derive Savings and Checking from Account
  5. Triangle classes - two levels of inheritance
Videos

Inheritance
Protected Members
Derived Class Constructors and Destructors

Lab Exercise #10

This lab exercise is due Thursday, 11/26 at 1:30 pm

Put your name, the compiler and operating system used, and Lab Exercise 10 in a comment at the top of your program. Email your source code. 

Create a Rectangle and a Square class.  Derive the Square from the Rectangle class.  Make use of the fact that a Square is the same as a Rectangle with equal length and width.  
Use constructor initializers in both classes.  
Override
 the describe function in the Rectangle class.  Do not override the area function in the square class.
Use this main() and produce the following output:

int main()
{
    Rectangle R(5,2);
    R.describe();
    cout << "area = " << R.area() << endl;
    Square S(3);
    S.describe();
    cout << "area = " << S.area() << endl;
}

******  OUTPUT  ******

Rectangle: length = 5  width = 2
area = 10
Square: size = 3
area = 9