Inheritance - EECE 230 Lecture Notes S Karaki 60 9 9.1...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 60 9. Inheritance 9.1 Introduction Inheritance allows us to tie classes together in hierarchies. New classes are created from existing classes while adding to their attributes and their methods. Inheritance is one of the key concepts that distinguish objects-oriented programming from traditional programming. Using inheritance we can arrange definitions in hierarchies to reflect natural categories. In this chapter we are discussing public inheritance only. Consider the following class hierarchy diagram showing relations among several classes: Fig.9.1: Class inheritance hierarchy - BoxCar, TankCar, etc. are subclasses derived classes - Box is a super class of BoxCar - RailroadCar is super class of BoxCar, TankCar, and Engine - Container is a super class (base class) of Box and Cylinder The arrows in Fig. 9.1 above show the subclass to super-class relations. Class objects inherit member variables and functions from the class to which they belong and from all that class’s super-classes. Container percentLoad Box height width length volume function Cylinder radius length volume function RailroadCar yearBuilt age function BoxCar TankCar Engine
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
EECE 230 – Lecture Notes: S. Karaki 61 For example a BoxCar object has its own copy of every variable declared in BoxCar, Box, Container and RailroadCar classes. In general, a member variable is declared in a super-class if it is useful for all its subclasses (e.g. percentLoad in Container class). 9.2 Base Class Definition We start the class definitions from the super-classes or base classes, i.e., Container and RailroadCar, since neither is a subclass of any other class. int currentYear = 2001; class Container { public: int percentLoad; Container( ){ } }; class RailroadCar { public: int yearBuilt; RailroadCar ( ){ } int age( ) {return currentYear- yearBuilt;} }; General format to define a subclass derived class: class className: public superClassName { }; Example: class Box: public Container { public: double height, width, length; Box ( ){ } double volume ( ) {return height* width* length;} }; The Box class is a derived class and Container is its base class .
Background image of page 2
EECE 230 – Lecture Notes: S. Karaki 62 The Cylinder class is also derived from the Container class as follows: const double pi = 3.14159; class Cylinder: public Container { public: double radius, length; Cylinder( ){ } double volume( ) { return pi* radius* radius* length; } }; A derived class can only be defined after defining all its base classes. With public inheritance the public (& protected) members of the base class are inherited as public (& protected) members of the derived class. Private members of a base are not accessible from its derived classes. Now define the classes at the bottom of hierarchy:
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/26/2010 for the course EECE 230 taught by Professor Samikaraki during the Spring '07 term at American University of Beirut.

Page1 / 12

Inheritance - EECE 230 Lecture Notes S Karaki 60 9 9.1...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online