Inheritance - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Inheritance

Description:

We can define a Mammal class, a Dog class, and a Doberman class, ... enum BREED { YORKIE, CAIRN, DANDIE, SHETLAND, DOBERMAN, LAB }; class Mammal { public: ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 19
Provided by: tom8
Category:

less

Transcript and Presenter's Notes

Title: Inheritance


1
Inheritance
  • Day 12

2
Hierarchies
  • Classes in C can form hierarchical
    relationships like biological classifications
  • Kingdom, Phylum, Class, Order, Family, Genus,
    Species

3
Hierarchies
  • We can define a Mammal class, a Dog class, and a
    Doberman class,
  • where a Doberman is a Dog, and a Dog is a Mammal.
  • The Doberman class inherits the properties of the
    Dog class, and the Dog class inherits the
    properties of the Mammal class.

4
Inheritance and Derivation
  • The class Dog inherits the properties of a class
    Mammal.
  • The class Dog is derived from the class Mammal.
  • The class Mammal is the base class of the Dog
    class.
  • Derived classes are supersets of their base
    class,
  • because they have all the properties of their
    base class (by inheritance), and more.

5
Syntax of Derivation
  • When you declare a class, you can indicate what
    class it derives from by writing a colon after
    the class name, then the class from which it
    derives
  • public indicates the type of derivation
  • dont worry about this now

class Dog public Mammal
6
Simple Inheritance
enum BREED YORKIE, CAIRN, DANDIE, SHETLAND,
DOBERMAN, LAB class Mammal public int
GetAge() const return itsAge int
GetWeight() const return itsWeight void
Speak() const cout void Sleep() const cout protected int itsAge int
itsWeight class Dog public Mammal
public BREED GetBreed() const return
itsBreed void SetBreed(BREED breed)
itsBreed breed void WagTail() cout "Tail wagging..." cout private BREED itsBreed class Cat public
Mammal public void Purr() const cout "Purrrrr...\n"
7
Simple Inheritance
void main() Dog fido cout fido.GetAge() endl fido.Speak() fido.WagTail() Cat
tabby cout tabby.GetWeight() endl tabby.Sleep() tabby.Purr()
8
Public, Private, Protected
  • To facilitate inheritance, we have added the
    protected keyword for declaring member data and
    functions.
  • private members are not available to derived
    classes, or any other classes.
  • public members are available to all classes.
  • protected members are available to this class and
    to classes derived from this class, but are not
    available to all other classes.

9
Constructors and Destructors
  • Dog and Cat objects are Mammal objects
  • When a Dog object is constructed,
  • first the Mammal constructor is called, creating
    a Mammal object,
  • then the Dog constructor is called, creating a
    Dog object.
  • When a Dog is destroyed, first the Dog destructor
    is called, then the Mammal destructor.

10
Passing Arguments to Base Constructors
  • If the base constructor takes a parameter, the
    derived constructor can pass the parameter at
    initialization
  • Data not initialized through parameters of the
    base constructor, must be initialized in the body
    of the derived constructor

Mammal(int age)itsAge(age),itsWeight(5) Dog(in
t age) Mammal(age), // itsBreed(YORKIE)
itsWeight7 //
11
Overriding Functions
  • When a derived class creates a function with the
    same return type and signature as a member
    function in the base class, but with a new
    implementation, it is said to be overriding that
    method.
  • The signature of a function is its name, as well
    as the number and type of its parameters. The
    signature does not include the return type.

12
Overriding Functions
class Mammal public void Speak() const
coutMammal public void Speak() const
coutspike animal.Speak() // Mammal
sound! spike.Speak() //
Woff! spike.MammalSpeak() // Mammal sound!
13
Calling the Base Method
  • If you have overridden the base method, you can
    call it by fully qualifying the name of the
    method

animal.Speak() // Mammal
sound! spike.Speak() //
Woff! spike.MammalSpeak() // Mammal sound!
14
Hiding the Base Class Method
  • If Mammal has a method, Move(), which is
    overloaded, and Dog overrides that method, the
    Dog method will hide all of the Mammal methods
    with that name.

15
Virtual Methods
  • Pointers to base classes can be assigned to
    derived class objects
  • You can then use the pointer to invoke any method
    on either the base class or the derived class.

Mammal pMammal new Dog
pMammal-Sleep() pMammal-WagTail()
16
Virtual Constructors/Destructors
  • If any method in your class is virtual, the
    destructor should be as well.
  • Constructors can not be virtual.
  • Virtual copy constructors can be effectively
    created by making a virtual member function that
    calls the copy constructor.

17
You Cant Get There from Here
  • If the Dog object has a method, WagTail(), which
    is not in the Mammal, you could not use the
    pointer to Mammal to access that method.

18
Slicing
  • Virtual function magic works only on pointers and
    references.
  • Passing an object by value will not enable the
    virtual functions to be invoked.
Write a Comment
User Comments (0)
About PowerShow.com