Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance.

Similar presentations


Presentation on theme: "Inheritance."— Presentation transcript:

1 Inheritance

2 Topics Inheritance Constructors and Inheritance
Overriding Functions and Variables Designing with Inheritance

3 Objectives After completing this topic, students should be able to:
Explain the concept of inheritance Correctly design and use classes that use inheritance in a C++ program * Know how to correctly call the parent constructor * know how to override functions and data in the parent class * Know when to use the protected attribute

4 Inheritance is the act of deriving a new class
from an already existing class. It is analogous to creating a new house blueprint from an existing one. Inheritance is useful when we find a natural hierarchical relationship between classes. Inheritance makes it possible to re-use existing code, thus saving time and minimizing bugs.

5 Example and Terminology

6 Suppose that we are creating a new role playing game, and we want to
have the following creatures: dwarves elves fairies

7 All of these are “creatures “. They all have
A name A strength value and they all can fight. dwarves elves fairies

8 We could define a class for each such as:
public class Dwarf { private: string name; int strength; int weapons; public: Dwarf( ); Dwarf(string, int, int); int getFightPoints( ); int getWeapons( ); }; public class Elf { private: string name; int strength; int magicSpells; public: Elf( ); Elf(string, int, int); int getFightPoints( ); int getMagicSpells( ); }; public class Fairy { private: string name; int strength; int wisdom; public: Fairy( ); Fairy(string, int, int); int getFightPoints( ); int getWisdom( ); };

9 Note that all of these classes have some things in common
public class Dwarf { private: string name; int strength; int weapons; public: Dwarf( ); Dwarf(string, int, int); int getFightPoints( ); int getWeapons( ); }; public class Elf { private: string name; int strength; int magicSpells; public: Elf( ); Elf(string, int, int); int getFightPoints( ); int getMagicSpells( ); }; public class Fairy { private: string name; int strength; int wisdom; public: Fairy( ); Fairy(string, int, int); int getFightPoints( ); int getWisdom( ); };

10 And They Have Some Things
That Are Different public class Dwarf { private: string name; int strength; int weapons; public: Dwarf( ); Dwarf(string, int, int); int getFightPoints( ); int getWeapons( ); }; public class Elf { private: string name; int strength; int magicSpells; public: Elf( ); Elf(string, int, int); int getFightPoints( ); int getMagicSpells( ); }; public class Fairy { private: string name; int strength; int wisdom; public: Fairy( ); Fairy(string, int, int); int getFightPoints( ); int getWisdom( ); };

11 This relationship is called
We gain a lot of productivity and functionality if we factor the common elements out into a base class. Creature name strength Base class Sometimes also called parent class super class This relationship is called Inheritance. Derived Class Sometimes also called child class sub class Dwarf Elf Fairy

12 This is known as an inheritance hierarchy
Creature Common things are defined in a base class name strength Unique things are defined in derived classes Dwarf Elf Fairy

13 Let’s look at the base class definition
class Creature { protected: string name; int strength; public: Creature( ); public int getFightPoints( ); . . . }; Creature name strength the protected modifier (#) tells us that the variable is accessible from within the Creature class and from within any derived classes. That is, functions of the derived class can see the protected data members defined in the base class.

14 And the Dwarf class Creature Dwarf class Dwarf : public Creature {
The : means that this class inherits from the Creature class. This is shown by the arrow in The class diagram below. Inheritance means That a Dwarf object will have everything that a Creature object has plus anything uniquely defined in the Dwarf class. Creature class Dwarf : public Creature { private: int weapons; public: Dwarf( ); . . . }; name strength Dwarf weapons

15 object named Zore, then Zore has the following properties: A name
So If I create a Dwarf object named Zore, then Zore has the following properties: A name strength weapons - This comes from the Dwarf class These come from the Creature class

16 When data is declared as protected in a parent class,
functions in a child class can see this data Protected Creature data A dwarf function

17 But data that is declared as private in a parent class
cannot be seen by functions in a derived class. Private Creature data A dwarf function

18 and… functions in the parent class can never
see any data fields in the child part of the object. Creature function Dwarf data

19 This kind of inheritance is known as
an is-a relationship. That is, a Dwarf is a Creature. An object of the Dwarf class can be used anyplace that an object of the Creature class can be used.

20 A creature’ fight score is equal to their strength value.
Let’s look at how we deal with differences between classes by Looking at the getFightPoint( ) function. getFightPoints( ) computes and returns the points this creature uses in one round of combat. The creature with the highest fight point value wins. A creature’ fight score is equal to their strength value. Dwarf Elf Fairy

21 Creature int Creature::getFightPoints( ) { return strength; }
We could write the following code in the base Creature class: name strength int Creature::getFightPoints( ) { return strength; }

22 How Fight Scores Differ
Elves are magical creatures, so an Elf’s fight point value is equal to its strength value plus the number of magic spells it has. Dwarves depend on their weapons, so a Dwarf’s fight point value is equal to its strength value plus the number of weapons it has. Fairies are known for their wisdom, so a Fairy’s fight point value is equal to its strength value plus its wisdom value.

23 Creature If we create a Dwarf object “dw1”, and invoke
the getFightPoints function, this method would normally be executed, because a Dwarf is-a Creature. Creature name strength int Creature:: getFightPoints ( ) { return strength; } Dwarf

24 Creature But wait … this is what we want the
Dwarf getFightPoints function to look like: name strength hitpoints int Dwarf::getFightPoints( ) { return strength + weapons; } Dwarf

25 Now both the Creature class and the Dwarf class have a function named getFightPoints( ). If we have a Dwarf object dw1, how do we get the compiler to use the correct getFightPoints( ) function? int Creature::getFightPoints( ) { return strength; } Creature name strength hitpoints int Dwarf::getFightPoints( ) { return strength + weapons; } Dwarf

26 Function Over-riding Because we have a function in the base class and a similar function in the derived class that has exactly the same signature, the function in the derived class is said to over-ride the function of the same name in the base class. The function in the base class is said to be hidden by the function in the derived class. When the getFightPoints( ) function in the Creature class is over-ridden, the compiler will use the getFightPoints( ) function declared in the dwarf class, when a getFightPoints( ) message is sent to an object of the Dwarf class.

27 So, if I create a Dwarf object named
dw1, and send dw1 a getFightPoints( ) message . . . dw1.getFightPoints( ) dw1 This function gets executed. int Dwarf::getFightPoints( ) { return strength + weapons; }

28 Dwarves have a unique property named
weapons. The class definition looks like: dwarf class Dwarf : Creature { private int weapons; public: Dwarf( ); Dwarf(string, int, int); int getFightPoints( ); int getWeapons( ) }

29 Now create a Dwarf object …
Dwarf dw1 = new Dwarf( “Rohan”, 350, 3); Creature Part When the constructor is called, the computer looks at the Dwarf class to see how much storage to allocate. It notes that a Dwarf is a Creature, so it looks at the Creature class also. Enough storage is allocated for the data members of both the base and the derived classes. name strength Dwarf Part weapons dw1

30 The Dwarf Constructor Dwarf::Dwarf(string arg_name, int arg_strength, int arg_weapons) : Creature(arg_name, arg_strength) { weapons = _weapons; } Constructors are not inherited. In order to initialize data declared in the parent class we must invoke the base class constructor. If you do not explicitly invoke the Creature constructor, the default Creature constructor is called automatically. The base class constructor finishes executing before the derived class constructor does.

31 Hiding Variables If a derived class declares a variable using the same name as a variable in a parent class, the variable in the child class is said to hide the variable in the parent. We don’t often hide variables in the base class.

32 Protected Members and Class Access
protected member access specification: A class member labeled protected is accessible to member functions of derived classes as well as to member functions of the same class Like private, except accessible to members functions of derived classes See Inheritance1.h, inheritance1.cpp, pr11-19.cpp

33 Base Class Access Specification
Base class access specification determines how private, protected, and public members of base class can be accessed by derived classes

34 Base Class Access - public inheritance
C++ supports three inheritance modes, also called base class access modes: - public inheritance class Child : public Parent { }; - protected inheritance class Child : protected Parent{ }; - private inheritance class Child : private Parent{ };

35 Base Class Access vs. Member Access Specification
Base class access is not the same as member access specification: Base class access: determine access for inherited members Member access specification: determine access for members defined in the class

36 Member Access Specification
Specified using the keywords private, protected, public class MyClass { private: int a; protected: int b; void fun(); public: void fun2(); };

37 Base Class Access Specification
class Child : public Parent { protected: int a; public: Child(); }; base access member access

38 Base Class Access Specifiers
public – object of derived class can be treated as object of base class (not vice- versa) protected – more restrictive than public, but allows derived classes to know some of the details of parents private – prevents objects of derived class from being treated as objects of base class.

39 appear in derived class
Effect of Base Access How base class members appear in derived class Base class members private: x protected: y public: z private base class x inaccessible private: y private: z protected base class private: x protected: y public: z x inaccessible protected: y protected: z public base class private: x protected: y public: z x inaccessible protected: y public: z

40 Creature Magical Creature If elves and fairies are both magical
creatures, we might envision another layer of base and derived class Creature name strength hitpoints Dwarf inherits from Creature Magical Creature inherits from Creature Magical Creature spells Dwarf Elf and Fairy inherit from Magical Creature Elf Fairy

41 Designing With Inheritance
Usually we start with a notion of some very specific classes (Fairy, Elf, Dwarf … ) and move to more general classes by factoring out common data and operations.

42 Designing With Inheritance
For example, suppose that we wanted to write a program that deals with instruments in an orchestra Violin Oboe Viola Clarinet Kettle Drum . . .

43 Designing With Inheritance
Now design a class that contains all of the things that orchestra instruments have in common. * Instrument name * Owner * Where it is in the orchestra Then write methods to manage this data

44 Designing With Inheritance
Look at all of the instruments in the orchestra. can they be organized into some different groups where each group has some things in common. Instrument Woodwinds Brass Percussion String

45 Instrument Woodwinds Brass Percussion String Clarinet French Horn Kettle Drum Violin Trumpet Cymbals Oboe Cello


Download ppt "Inheritance."

Similar presentations


Ads by Google