CIS05 Comparative Languages Inheritance and Polymorphism - PowerPoint PPT Presentation

About This Presentation
Title:

CIS05 Comparative Languages Inheritance and Polymorphism

Description:

7/27/09. Marc Conrad - University of Luton. 1. CIS05 ... 'The Athlete became fitter' std::endl; void displayFitness() { std::cout 'The fitness is: ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 16
Provided by: MarcC71
Category:

less

Transcript and Presenter's Notes

Title: CIS05 Comparative Languages Inheritance and Polymorphism


1
CIS05 Comparative LanguagesInheritance and
Polymorphism
  • Marc Conrad
  • D104a (Park Square Building)
  • Marc.Conrad_at_luton.ac.uk
  • These slides
  • What is Inheritance?
  • C example of inheritance.
  • Static binding and dynamic linking.

2
What is Inheritance?
  • http//www.cs.nmsu.edu/rth/cs/cs177/map/inheritd.
    html
  • Inheritance is a way of relating two classes so
    that one class may use another class's members
    without redefining them.
  • http//www.parashift.com/c-faq-lite/basics-of-in
    heritance.html
  • Inheritance is what separates abstract data type
    (ADT) programming from OO programming.
  • The basic principle is simple
  • A class get the state and behaviour of another
    class and adds additional state and behaviour.

3
What is the purpose of Inheritance?
  • Specialisation
  • Extending the functionality of an existing class
  • Generalisation
  • Sharing commonality between two or more classes
  • Polymorphism
  • Implementation of different behaviour to the same
    message (depending on the type of the object).

4
Terminology
  • Derived class or subclass or child class.
  • A class which inherits some of its attributes and
    methods from another class.
  • Base class or superclass or parent class.
  • A class from which another class inherits.
  • Ancestor.
  • A classs ancestors are those from which its own
    superclasses inherit.
  • Descendant.
  • A classs descendants are those which inherit
    from its subclasses.

5
(Reminder the Person class)
include ltiostreamgt class Person private
char name20 int yearOfBirth public
Person(char theName, int theYear)
yearOfBirth theYear sprintf(name,
theName) void jump()
stdcout ltlt name ltlt
" jumps" ltlt stdendl int
getAge(int thisYear) return thisYear
- yearOfBirth
  • State
  • name
  • year of birth
  • Behaviour
  • jump
  • get age

6
Inheritance - Example
  • include "Person.h"
  • class Athlete public Person
  • private
  • int fitness
  • public
  • Athlete(char theName, int yearOfBirth)
  • Person(theName, yearOfBirth)
  • fitness 0
  • void jump()
  • Personjump()
  • fitness fitness 1
  • stdcout ltlt
  • "The Athlete became fitter" ltlt
    stdendl
  • void displayFitness()
  • stdcout ltlt "The fitness is "
  • ltlt fitness ltlt stdendl
  • The Athlete inherits state and behaviour from the
    Person class.

7
Inheritance - Syntax
  • include "Person.h"
  • class Athlete public Person
  • private
  • int fitness
  • public
  • Athlete(char theName, int yearOfBirth)
  • Person(theName, yearOfBirth)
  • fitness 0
  • void jump()
  • Personjump()
  • fitness fitness 1
  • stdcout ltlt
  • "The Athlete became fitter" ltlt
    stdendl
  • void displayFitness()
  • stdcout ltlt "The fitness is "
  • ltlt fitness ltlt stdendl
  • The colon indicates an inheritance
    relationship.
  • In C an inheritance relationship can be public
    or private.
  • A class can inherit more than one class
  • class C public B, public A

Further reading multiple inheritance
http//www.parashift.com/c-faq-lite/multiple-in
heritance.htmlfaq-25.5
8
Inheritance - Constructors
  • include "Person.h"
  • class Athlete public Person
  • private
  • int fitness
  • public
  • Athlete(char theName, int yearOfBirth)
  • Person(theName, yearOfBirth)
  • fitness 0
  • void jump()
  • Personjump()
  • fitness fitness 1
  • stdcout ltlt
  • "The Athlete became fitter" ltlt
    stdendl
  • void displayFitness()
  • stdcout ltlt "The fitness is "
  • ltlt fitness ltlt stdendl
  • There is a special syntax for passing constructor
    arguments to the parent class(es).
  • Note that the base class is constructed first.

9
Inheritance - Overriding of methods
  • include "Person.h"
  • class Athlete public Person
  • private
  • int fitness
  • public
  • Athlete(char theName, int yearOfBirth)
  • Person(theName, yearOfBirth)
  • fitness 0
  • void jump()
  • Personjump()
  • fitness fitness 1
  • stdcout ltlt
  • "The Athlete became fitter" ltlt
    stdendl
  • void displayFitness()
  • stdcout ltlt "The fitness is "
  • ltlt fitness ltlt stdendl
  • The jump() method is overridden (re-implemented).
  • Using the ltparent_classgtmethod_name() syntax we
    can call the original method of the parent class.

10
Inheritance - Protected Attributes
  • include "Person.h"
  • class Athlete public Person
  • private
  • int fitness
  • public
  • Athlete(char theName, int yearOfBirth)
  • Person(theName, yearOfBirth)
  • fitness 0
  • void jump()
  • Personjump()
  • fitness fitness 1
  • stdcout ltlt
  • "The Athlete became fitter" ltlt
    stdendl
  • void displayFitness()
  • stdcout ltlt "The fitness is "
  • ltlt fitness ltlt stdendl
  • Note that we cannot call the attribute name of
    the parent class in the child class.
  • There is a solution to this make the attributed
    of the parent class protected instead of private.

11
Test program for an inherited athlete object.
  • include "Athlete.h
  • void doExercise(Athlete who, int howMuch)
  • for( int i 0 i lt howMuch i )
  • who-gtjump()
  • int main()
  • Athlete fabian("Fabian", 1998)
  • doExercise(fabian, 3)
  • fabian.displayFitness()
  • stdcout ltlt "Fabian is "
  • ltlt fabian.getAge(2004) ltlt
  • " years old" ltlt stdendl
  • return 0
  • jump() is defined in Person and overridden in the
    Athlete.
  • displayFitness() is defined in Athlete (only).
  • getAge() is inherited from Person.

12
Dynamic binding vs. Static linkingExample 1
  • Consider the following code
  • Person pFabian new Athlete("Tom",2000)
  • pFabian-gtjump()
  • Which jump() method is called?
  • a) The jump() method of Person?
  • b) The jump() method of Athlete?
  • Dynamic binding vs. Static linking

13
Dynamic binding vs. Static linkingExample 2
  • include "Athlete.h
  • void doExercise( Person who, int howMuch)
  • for( int i 0 i lt howMuch i )
  • who-gtjump()
  • int main()
  • Athlete fabian("Fabian", 1998)
  • doExercise(fabian, 3)
  • fabian.displayFitness()
  • stdcout ltlt "Fabian is "
  • ltlt fabian.getAge(2004) ltlt
  • " years old" ltlt stdendl
  • return 0
  • jump() is defined in Person and overridden in
    Athlete.
  • Question 1 which jump() method is called?
  • Question 2 Why?

14
The keyword virtual
include ltiostreamgt class Person private
char name20 int yearOfBirth public
Person(char theName, int theYear)
yearOfBirth theYear sprintf(name,
theName) virtual void jump()
stdcout ltlt name ltlt
" jumps" ltlt stdendl
int getAge(int thisYear) return
thisYear - yearOfBirth
  • The keyword virtual enables dynamic linking of
    the jump() method.

Note that dynamic linking is the default in
Java. In C it must be enabled explicitly.
Further reading www.cs.wustl.edu/schmidt/PDF/C
-dynamic-binding4.pdf
15
Polymorphism
  • Dynamic linking implements polymorphic behaviour.
  • Not the compiler decides how a method is executed
    but the object that receives the message.
  • Objects of different type respond differently to
    the same message.
Write a Comment
User Comments (0)
About PowerShow.com