CSC 143 O 1 - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

CSC 143 O 1

Description:

Trouble: penguins can fly! C is stricter than the English language. ... Could also do: Penguins can fly, but it is an error for them to try to do so. How? ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 9
Provided by: francois2
Category:
Tags: csc | penguins

less

Transcript and Presenter's Notes

Title: CSC 143 O 1


1
CSC 143
  • Inheritance and Object Oriented Design

2
Public Inheritance means "isa"
  • If class D publicly inherits from class B, every
    object of type D is also an object of type B (not
    vice-versa)

class Person class Student public Person
void dance(const Person p) // anyone can
dance void study(const Student s) // only
students study
Person p Student s // p is a Person, s a
Student dance(p) // fine, p is a
Person dance(s) // fine, s is a Student,
therefore isa Person study(s) // OK study(p) //
error! p is not a Student
3
IsA Design (1)
class Bird public virtual void fly() // birds
can fly class Penguin public Bird //
penguins are birds
  • Trouble penguins can fly!
  • C is stricter than the English language. In
    C, "Birds can fly" means "All birds can fly"
  • Solution?

4
IsA Design (2)
  • Refine the hierarchy

5
IsA Design (3)
class Bird //no fly function class
FlyingBird public Bird virtual void
fly() class NonFlyingBird //no fly
function class Penguin public NonFlyingBird
// no fly function
  • Could also do Penguins can fly, but it is an
    error for them to try to do so. How?

6
Interface ? Implementation
  • Interface function declaration
  • Implementation code for the function
  • Member function interfaces are always inherited.
    If a function applies to a class, it must also
    apply to its subclasses.
  • If you want the subclasses to inherit
  • the interface only, make the member function pure
    virtual (virtual 0)
  • the interface and a default implementation, make
    the member function virtual
  • Non virtual means inherit the interface as well
    as a mandatory implementation

7
Example A Shape Class
  • class Shape
  • public
  • virtual void draw() const 0 // Abstract
    class
  • virtual void error(const string msg)
  • int ObjectID() const
  • class Rectangle public Shape
  • class Ellipse public Shape
  • Rectangle and Ellipse must provide a draw
    function
  • Every class must support an error function (but
    you don't have to write your own if you don't
    want to)
  • Every object has an ID. That ID is determined by
    the definition of ShapeobjectID. Don't change
    it!

8
Virtual Destructor
  • Make sure that base classes have a virtual
    destructor
  • If D is derived from B, what happens in the
    following?
  • B pB new D
  • delete pB
  • If you try to delete a derived class object
    through a base pointer and the base class doesn't
    have a virtual destructor, the result is
    undefined.
Write a Comment
User Comments (0)
About PowerShow.com