Title: Inheritance
1Inheritance
- Mechanism for deriving new classes from existing
classes
2Think of a Bicycle
3Think of a Tandem Bike
4Think of a Racing Bike
5Think of a Mountain Bike
6Thinking About Bicycles
- A tandem bicycle is a kind of bicycle
- Bicycle with two seats
- A mountain bicycle is a kind of bicycle
- Bicycle with shocks
- A racing bicycle is a kind of bicycle
- Lightweight aerodynamic construction
- Tandem, mountain, and racing bicycles are
specialized bicycles
7Wouldnt It Be Nice
- Be able to create specialized program objects
without starting from scratch - Use a previously defined object
- Specialize it as necessary
- Much less programming required
- Inheritance is the object-oriented programming
mechanism for specialization
8Inheritance
- Ability to define new classes of objects using
existing classes as a basis
- Ability to define new classes of objects using
existing classes as a basis - The new class inherits the attributes and
behaviors of the parent classes
- Ability to define new classes of objects using
existing classes as a basis - The new class inherits the attributes and
behaviors of the parent classes - New class is aspecialized versionof the parent
class
9Inheritance
- A natural way to reuse code
- Programming by extension rather than reinvention
- Object-oriented paradigm is well-suited for this
style ofprogramming
- A natural way to reuse code
- Programming by extension rather than reinvention
- Object-oriented paradigm is well-suited for this
style ofprogramming - Terminology
- Base class (superclass)
- Derived class (subclass)
10Class road_vehicle
- class road_vehicle
- int wheels
- int passengers
- public
- void set_wheels(int num) wheels num
- int get_wheels() return wheels
- void set_pass(int num)_passengers num
- int get_pass() return passengers
-
-
11Class road_vehicle
- class road_vehicle
- int wheels
- int passengers
- public
- void set_wheels(int num) wheels num
- int get_wheels() return wheels
- void set_pass(int num)_passengers num
- int get_pass() return passengers
-
- //Use this definition to declare class truck
- class truck public road_vehicle
- int cargo
- public
- void set_cargo(int size) cargo size
- int get_cargo() return cargo
- void show()
-
12Class road_vehicle
- class road_vehicle
- int wheels
- int passengers
- public
- ...
-
- // Base class road_vehicle, derived class
truck - class truck public road_vehicle
- int cargo
- public
- ...
-
- Truck inherits road_vehicle, so truck
includes all of road_vehicle details and adds
cargo to it
13Class road_vehicle
- class road_vehicle
- int wheels
- int passengers
- public
- void set_wheels(int num) wheels num
- int get_wheels() return wheels
- void set_pass(int num)_passengers num
- int get_pass() return passengers
-
- // Truck inherits supporting member functions
- // And adds a few
- class truck public road_vehicle
- int cargo
- public
- void set_cargo(int size) cargo size
- int get_cargo() return cargo
- void show()
14Inheritance - General Form
- class derived-class access base-class
- body of new class
-
- Example
- class truck public road_vehicle
- int cargo
- public
- void set_cargo(int size) cargo size
- int get_cargo() return cargo
- void show()
-
- access may be public, private, or protected
- For the time being use public in all cases
- All public members of the class will be public
members of the derived class
15Class road_vehicle
- class road_vehicle
- int wheels
- int passengers
- public
- void set_wheels(int num) wheels num
- int get_wheels() return wheels
- void set_pass(int num) passengers num
- int get_pass() return passengers
-
- //Use this definition to declare class
automobile - enum type car, van, wagon
- class automobile public road_vehicle
- type car_type
- public
- void set_type(type t) car_type t
- type get_type() return car_type
- void show()
-
16road_vehicle Hierarchy
road_vehicle
automobile
truck
17road_vehicle Hierarchy
private wheels passengers public
set_wheels() get_wheels() set_pass() get_pass()
road_vehicle
automobile
truck
private car_type public set_type()
get_type() show()
private cargo public set_cargo()
get_cargo() show()
18Access Public
- When the base class is inherited as public, the
public members of the base class are accessible
to the derived class - When the base class is inherited as private, the
public members of the base class become private
members of the derived class. - class derived-class access base-class
- body of new class
-
- Example Inherits the public functions of
road_vehicle - class truck public road_vehicle
- int cargo
- public
- void set_cargo(int size) cargo size
- int get_cargo() return cargo
- void show()
-
19Access Private
- When the base class is inherited as public, the
public members of the base class are accessible
to the derived class - When the base class is inherited as private, the
public members of the base class become private
members of the derived class. - Example Inherits the public functions of
road_vehicle as private members - class truck private road_vehicle
- int cargo
- public
- void set_cargo(int size) cargo size
- int get_cargo() return cargo
- void show()
-
20Access Public
private wheels passengers public
set_wheels() get_wheels() set_pass() get_pass()
road_vehicle
automobile
truck
private car_type public set_type()
get_type() show()
private cargo public set_cargo()
get_cargo() show()
21Access Private
private wheels passengers public
set_wheels() get_wheels() set_pass() get_pass()
road_vehicle
automobile
truck
private car_type public set_type()
get_type() show()
private cargo public set_cargo()
get_cargo() show()
22Protected
- When a member of a class is declared as
protected, that member is not accessible to
other, non member elements of the program. - Unlike a private member, it can be accessed by
other members of the class of which it is part. - Butwhen a protected member is inherited it
differs from a private member - IT CAN BE ACCESSED BY DERIVED CLASSES
- Example
- class road_vehicle
- protected
- int wheels // these are available to derived
- int passengers // classes
- public
- void set_wheels(int num) wheels num
- int get_wheels() return wheels
- void set_pass(int num) passengers num
- int get_pass() return passengers
-
23Project 5
- //Base Class Definition
- class obj
- protected
- int id
- public
- obj() id 0 //Default Constructor
- obj(int num)id num
- void setid(int i) id i
- int getid() return id
- obj()
-
24Project 5
- // Derived Class Definition
- class dobj public obj
- int a3
- public
- dobj() // Default Constructor sets //
id 0, and a 0, 1, 2 - dobj(int num) // Constructor sets id num,
- // a 0, 1, 2
- void operator() // Increments id by one
- void operator--() // Decrements id by one
- int operator(int i) // Returns ai
- dobj()
-
25Project 5
- void main()
- dobj a(14), b(15), c(16), d
- //cout
- cout
- cout
- cout
- cout
- for (int i 0 i
- a b c
- cout
- cout
- cout
- for (i 0 i
-