CS304: Lecture 9 - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

CS304: Lecture 9

Description:

CS304: Lecture 9. OOP: Inheritance. Deitel, Ch. 12. http://www.cis.uab. ... string food; void Pet::eat(){ cout 'Eating ' food endl; void Pet::speak ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 28
Provided by: matthe49
Category:

less

Transcript and Presenter's Notes

Title: CS304: Lecture 9


1
CS304 Lecture 9
  • OOP Inheritance
  • Deitel, Ch. 12
  • http//www.cis.uab.edu/cs304

2
Inheritance
  • Inheritance is a form of software reuse in which
    the programmer creates a class that absorbs an
    existing classs data and behaviors and enhances
    them with new capabilities.
  • Saves time by preventing rewritten code
  • Reduces errors by encouraging reuse of debugged
    code

3
The Inheritance Relationship
Base Class
Derived Class A
Derived Class B
4
Vocabulary
  • An existing class that is inherited from is the
    base class.
  • A class that inherits from another class is a
    derived class.
  • A direct base class is the base class from which
    a derived class explicitly inherits.
  • An indirect base class is inherited from two or
    more levels up in the class hierarchy.

5
Vocabulary (contd)
  • Single Inheritance means that a class is derived
    from one base class. (This is Javas style of
    inheritance)
  • Multiple Inheritance allows a derived class to
    inherit from multiple (possibly unrelated)
    classes. C allows both.

6
Inheritance
  • Inheritance exhibits an is-a relationship.
  • This is in contrast to object composition, which
    was the has-a relationship
  • As an example, consider a class rectangle and a
    class quadrilateral
  • What is the relationship between the two?
  • Other examples
  • Students, Shapes, Employee Etc.

7
Types of Inheritance
  • Public
  • Our main focus
  • Private members of a base class are not
    accessible directly from a derived class, but
    these private members are still inherited
  • Friends are not inherited
  • Public members remain public
  • Protected members are still protected

8
Types of Inheritance (contd)
  • Protected Inheritance
  • Public and protected members are protected in
    derived class, private remains hidden
  • Private Inheritance
  • Public and protected members are private in
    derived class, private remains hidden
  • Public is the most common type of inheritance

9
Protected Members
  • Protected is an access modifier (like public and
    private)
  • Intermediate level of protection
  • Protected members are unavailable to other
    classes (like private), but are accessible by
    derived classes

10
Inheritance example
  • class Petpublic Pet() weight(1), food("Pet
    Chow") Pet() void setWeight(int w)
    weight w int getWeight() return
    weight void setfood(string f) food
    f string getFood() return food void
    eat() void speak()protected int
    weight string food

11
  • void Peteat() cout ltlt "Eating " ltlt food ltlt
    endl
  • void Petspeak()cout ltlt "Growl" ltlt endl

12
Public Inheritance Example
  • class Pet
  • class Rat public Pet

13
Derived Class Rat
  • class Rat public Pet public Rat() Rat()
    void sicken() cout ltlt "Speading Plague"
    ltlt endl

14
Derived Class Cat
  • class Cat public Pet public Cat()
    numberToes(5) Cat() void
    setNumberToes(int toes) numberToes
    toes int getNumberToes() return
    numberToesprivate int numberToes

15
in action! (Inaction?)
  • int main() Rat charles Cat fluffycharles.setW
    eight(25)cout ltlt "Charles weighs " ltlt
    charles.getWeight() ltlt " lbs. " ltlt
    endlcharles.speak() charles.eat()
    charles.sicken() fluffy.speak() fluffy.eat()
    cout ltlt "Fluffy has " ltlt fluffy.getNumberToes()
    ltlt " toes " ltlt endl return 0

16
Construction/Destruction
  • When constructing a derived class member, the
    constructor for the parent class is either
    explicitly or implictly called
  • Explicit base-class member initializer
  • Implicit default constructor
  • Example If Pet(int weight) were another
    constructorRat(int weight) Pet(weight)

17
OOP PolymorphismDeitel, Ch. 13
18
Definitions
  • Polymorphism Program in general rather than in
    the specific
  • Deal with objects in the same class hierarchy in
    the same manner as the base class
  • Example From UabEmployee class
  • Theme Extensibility!

19
What Good is Polymorphism?
  • Treating objects as its base class allows
    grouping of derived and base types
  • Eliminates need for unnecessary switch logic
  • Imagine an array of UabEmployees

20
Using Derived Types
  • It is possible to use functions from base classes
  • BaseclassfunctionName()
  • We can do this, because Derivedclass is-a
    Baseclass

21
An Inheritance issue What happens here?
  • class b public a
  • public
  • void a1()
  • cout ltlt "b1\n"
  • int main()
  • a test new b
  • test-gta1()
  • b test2
  • test2.a1()
  • class a
  • public
  • void a1()
  • cout ltlt "a1\n"
  • void a2()
  • cout ltlt "a2\n"

22
Why on Earth does it do that?!?
  • test, in the previous example, is a base-class
    pointer.
  • Invoking the base function is perfectly legal,
    due to the is-a relationship
  • Cant really assign a base-class object to a
    derived class pointer
  • We want a way to specify that the function should
    be dynamically bound as opposed to statically
    bound
  • Dynamic binding allows a program to choose the
    correct implementation of a function at runtime

23
Virtual functions
  • The virtual keyword can let a derived class
    override the base classs behavior through a base
    class pointer
  • class a
  • public
  • virtual void a1()
  • cout ltlt "a1\n"
  • virtual void a2()
  • cout ltlt "a2\n"

24
Type Fields and switch Statements
  • Take, as example, a shape class. If each member
    (and derived member) has an attribute called
    shapeType, we can use this attribute to choose
    a function call at runtime
  • Whats the problem with this, from a software
    engineering standpoint?

25
Segue into Abstract Classes/Pure Virtuals
  • Classes which are never actually instantiated are
    abstract classes.
  • These are usually used as base classes in
    inheritance, so they are also called abstract
    base classes.
  • Abstract classes are incomplete. Derived classes
    fill in the missing pieces.
  • Classes that are able to be instantiated are
    called concrete classes.

26
Pure Virtuals
  • An abstract class has at least one pure virtual
    function, which is a virtual function with no
    implementation.
  • virtual void draw() const 0
  • 0 is often called the pure specifier
  • Virtual functions are placeholders
  • Abstract functions are also placeholders in a
    sense
  • Pointers to abstract functions point to derived
    objects

27
Virtual Destructors
  • What happens if we have a derived object that
    defines its own non-virtual destructor, but is
    destroyed through a base-class pointer?
  • Problem solved by declaring the base class to
    have a virtual destructor
  • virtual classname()
  • Please read section 13.8
  • (dynamic casting and type id)
Write a Comment
User Comments (0)
About PowerShow.com