1 / 7

Inheritance Basics Fall 2008

Inheritance Basics Fall 2008 . Dr. David A. Gaitros dgaitros@admin.fsu.edu. The Reason for Inheritance . “Why write the same code twice?”

toyah
Download Presentation

Inheritance Basics Fall 2008

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Inheritance BasicsFall 2008 Dr. David A. Gaitros dgaitros@admin.fsu.edu

  2. 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.

  3. Inheritance • Examples of relationships A class called Geometric_Objects could derive classes like Circle, Square, and Line A class called Sport could derive classes like Football, Baseball, and Soccer A class called BankAccount could derive classes such as Checking, Savings, and Debit A class called vehicle could derive classes called Car, Train, Bus, Motorcycle, and Airplane We could use the Car class as a base class and derive other classes such as Ford, Toyota, Buick, Honda, etc.

  4. 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 { … }

  5. Inheritance • Layers of Inheritance class Vehicle { … } class Car: public Vehicle { … } class Honda: public Car { … }

  6. 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.

More Related