Top Banner
Inheritance Basics Fall 2008 Dr. David A. Gaitros [email protected] .edu
7

Inheritance Basics Fall 2008

Feb 25, 2016

Download

Documents

toyah

Inheritance Basics Fall 2008 . Dr. David A. Gaitros [email protected]. The Reason for Inheritance . “Why write the same code twice?” - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Inheritance Basics Fall 2008

Inheritance BasicsFall 2008

Dr. David A. [email protected]

Page 2: Inheritance Basics Fall 2008

The Reason for Inheritance

“Why write the same code twice?” This sums up the reason for inheritance

quite nicely. Take the example of an employee database. There are many kinds of employees which have different attributes (data points) that need to be tracked for each one.

However, there are some attributes that are common to ALL employees. This code should be written and “inherited” by those modules that handle the specifics for each type of employee.

Page 3: Inheritance Basics Fall 2008

Inheritance

• Examples of relationshipsA class called Geometric_Objects could derive

classes like Circle, Square, and LineA class called Sport could derive classes like

Football, Baseball, and SoccerA class called BankAccount could derive classes

such as Checking, Savings, and DebitA class called vehicle could derive classes called

Car, Train, Bus, Motorcycle, and AirplaneWe could use the Car class as a base class and

derive other classes such as Ford, Toyota, Buick, Honda, etc.

Page 4: Inheritance Basics Fall 2008

Inheritance

Declaring a Derived Class“When we say that some class D is a

derived of some other class B, we are saying that class D has all of the features of class B but with some extra. Class B is called the base class.

//class derivedclassname: public baseclassname//class Sport{ ….}class Football: public Sport{ …}

Page 5: Inheritance Basics Fall 2008

Inheritance

• Layers of Inheritance

class Vehicle { … }class Car: public Vehicle { …}class Honda: public Car { …}

Page 6: Inheritance Basics Fall 2008

Protection Levels

• public – Any member that is public can be directly accessed by name from anywhere. Poor practice to have data values with public access.

• private – Any member that is private can only be accessed directly only by the class in which it is declared.

• protected – Any member that is protected can be access directlry by the class in which it was declared and any classes that are derived.

Page 7: Inheritance Basics Fall 2008