Title: Object-Oriented Design Inheritance and Class Examples
1Object-Oriented DesignInheritance and Class
Examples
2Inheritance Example -- Class.zip
- One reason to use inheritance is that it permits
you to reuse code from a previous project, but
gives you the flexibility to slightly modify it
if the old code doesn't do exactly what you need
for the new project. - It doesn't make sense to start every new project
from scratch since some code will certainly be
repeated in several programs and you should
strive to build on what you did previously.
3INHERITANCE
- A SIMPLE CLASS TO START WITH
- Example program ------gt VEHICLE.H
4VEHICLE.H
- It consists of four simple methods which can be
used to manipulate data pertaining to our
vehicle. - We will eventually refer to this as a base class
or parent class.
5THE IMPLEMENTATION FOR VEHICLE Example program
------gt VEHICLE.CPP
- The initialize() method assigns the values input
as parameters to the wheels and weight variables. - We have methods to return the number of wheels
and the weight, and finally, we have one that
does a trivial calculation to return the loading
on each wheel.
6USING THE VEHICLE CLASS Example program ------gt
TRANSPRT.CPP
- Inheritance uses an existing class and adds
functionality to it to accomplish another,
possibly more complex job. - It declares four objects of the vehicle class,
initializes them, and prints out a few of the
data values to illustrate that the vehicle class
can be used as a simple class because it is a
simple class. We are referring to it as a simple
class as opposed to calling it a base class or
derived class as we will do next.
7OUR FIRST DERIVED CLASS Example program ------gt
CAR.H
- The vehicle class is inherited due to the "
public vehicle" added to line 7. - This derived class named car is composed of all
of the information included in the base class
vehicle, and all of its own additional
information. - Even though we did nothing to the class named
vehicle, we made it into a base class because of
the way we are using it here.
8OUR FIRST DERIVED CLASS Example program ------gt
CAR.H
- To go a step further, even though it will be used
as a base class in an example program later,
there is no reason it cannot continue to be used
as a simple class in the previous example
program. - In fact, it can be used as a simple class and a
base class in the same program. The question of
whether it is a simple class or a base class is
answered by the way it is used.
9OUR FIRST DERIVED CLASS Example program ------gt
CAR.H
- In this case, the vehicle base class can be used
to declare objects that represent trucks, cars,
bicycles, or any number of other vehicles you can
think up. - The class named car however can only be used to
declare an object that is of type car because we
have limited the kinds of data that can be
intelligently used with it. - The car class is therefore more restrictive and
specific than the vehicle class. The vehicle
class is more general than the car class.
10OUR FIRST DERIVED CLASS Example program ------gt
CAR.H
- If we wished to get even more specific, we could
define a derived class using car as the base
class, name it sports_car, and include such
information as red_line_limit for the tachometer
which would be silly for the family station
wagon. - The car class would therefore be used as a
derived class and a base class at the same time,
so it should be clear that these names refer to
how a class is used.
11HOW DO WE DECLARE A DERIVED CLASS?
- A derived class is defined by including the
header file for the base class as is done in line
5, then the name of the base class is given
following the name of the derived class separated
by a colon as is illustrated in line 7. - All objects declared as being of class car
therefore are composed of the two variables from
the class vehicle because they inherit those
variables, and the single variable declared in
the class car named passenger_load.
12HOW DO WE DECLARE A DERIVED CLASS?
- An object of this class will have three of the
four methods of vehicle and the two new ones
declared here. - The method named initialize() which is part of
the vehicle class will not be available here
because it is hidden by the local version of
initialize() which is a part of the car class. - The local method will be used if the name is
repeated allowing you to customize your new class.
13A Graphical Representation of an object of this
class.
14Information Hiding
- Note that the implementation for the base class
only needs to be supplied in its compiled form. - The source code for the implementation can be
hidden for economic reasons to aid software
developers. - Hiding the source code also allows the practice
of information hiding. The header for the base
class must be available as a text file since the
class definitions are required in order to use
the class.
15THE CAR CLASS IMPLEMENTATION Example program
------gt CAR.CPP
- The first thing you should notice is that this
file has no indication of the fact that it is a
derived class of any other file, that can only be
determined by inspecting the header file for the
class. - Since we can't tell if it is a derived class or
not, it is written in exactly the same way as any
other class implementation file.
16ANOTHER DERIVED CLASS Example program ------gt
TRUCK.H
- This class adds two more variables and three more
methods. - A very important point that must be made is that
the car class and the truck class have absolutely
nothing to do with each other, they only happen
to be derived classes of the same base class or
parent class as it is sometimes called.
17A Graphical Representation of the truck class
18Example program ------gt TRUCK.H
- Note that both the car and the truck classes have
methods named passengers() but this causes no
problems and is perfectly acceptable. - If classes are related in some way, and they
certainly are if they are both derived classes of
a common base class, you would expect them to be
doing somewhat similar things. - In this situation there is a good possibility
that a method name would be repeated in both
child classes.
19THE TRUCK IMPLEMENTATION Example program ------gt
TRUCK.CPP
- Examine the file named TRUCK.CPP for the
implementation of the truck class. - It has nothing unusual included in it.
20USING ALL THREE CLASSES Example program ------gt
ALLVEHIC.CPP
- It uses the parent class vehicle to declare
objects and also uses the two child classes to
declare objects. - This was done to illustrate that all three
classes can be used in a single program.
21Example program ------gt ALLVEHIC.CPP
- All three of the header files for the classes are
included in lines 3 through 5 so the program can
use the components of the classes. - Notice that the implementations of the three
classes are not in view here and do not need to
be in view. - This allows the code to be used without access to
the source code for the actual implementation of
the class. However, it should be clear that the
header file definition must be available.
22Example program ------gt ALLVEHIC.CPP
- In this example program, only one object of each
class is declared and used but as many as desired
could be declared and used in order to accomplish
the programming task at hand. - The classes were developed, debugged, and stored
away previously, and the interfaces were kept
very simple.
23Compile and Link
- The three classes and the main() program can be
compiled in any order desired. - All four must be compiled prior to linking the
four resulting object (or binary) files together. - Be sure you do the required steps to compile and
execute this program because the effective use of
C will require you to compile many separate
files and link them together.
24WHY THE ifndef VEHICLE_H ?
- When we define the derived class car, we are
required to supply it with the full definition of
the interface to the vehicle class since car is a
derived class of vehicle and must know all about
its parent. - We do that by including the vehicle class into
the car class, and the car class can be compiled.
The vehicle class must also be included in the
header file of the truck class for the same
reason.
25WHY THE ifndef VEHICLE_H ?
- When the preprocessor gets to the car class, it
includes the vehicle class because it is listed
in the car class header file, but since the
vehicle class was already included in line 3 of
ALLVEHIC.CPP, it is included twice and we attempt
to re-declare the class vehicle. - Of course it is the same declaration, but the
system doesn't care, it simply doesn't allow
re-declaration of a class.
26Linked ListsExamples
27The Main Concept
- Static Array
- Pre-allocation Required
- Waste-full if you allocate too much
- Fatal if you allocate too little
- Data has to be shifted when an item is inserted
or deleted at specified position - Dynamic Array
- Determine the size before allocating memory
- Costly reallocation, insertion, deletion
- Data has to be shifted
- Linked List
- Truly dynamic, memory is allocated as needed
28Linked Lists
- To maintain a given order you don't have to
store data in this order. - Each item of the list points to the next one.
- You can always determine the successor, no
matter where it is physically. - You can insert and delete items without shifting
the whole list. - The size of the list can be increased easily by
adding successors.
29(a) A linked list of integers (b) insertion (c)
deletion
30Pointers
- A pointer variable (or just pointer) contains the
location of address in memory, of a memory cell. - By using a pointer to a particular memory cell,
you can locate the cell and determine its content.
int p,q q1234 pq
31Pointers dynamic memory allocation
- A pointer can point at the memory area allocated
at execution time.
int p pnew int10 (p2)1234 p12222 d
elete p
int p pnew int p1234 delete p
32A pointer to an integer
33(a) Declaring pointer variables (b) pointing to
statically allocated memory (c) assigning a value
34(d) Allocating memory dynamically (e) assigning
a value (f) copying a pointer
35(g) Allocating memory dynamically and assigning a
value (h) assigning NULL to a pointer variable
(i) de-allocating memory
36An incorrect pointer to a de-allocated node
37Programming with pointer variables and
dynamically allocated memory
38Programming with pointer variables and
dynamically allocated memory
39Programming with pointer variables and
dynamically allocated memory
40Pointer-based Linked Lists
- A linked list contains components that are linked
to one another. - Each component (a node) contains
- a data item,
- a pointer to the next item.
41A node
42A Node
struct Node int item Node next
- Any time you allocate memory using new, you must
de-allocate it by using delete. - head points at the first element on the list.
Node p,head headNULL pnew
Node p-gtitem1 p-gtnextNULL headp ... delete
p
43A head pointer to a list
44A misconception
- Memory is allocated for Node structure, head
points at the allocated area. - head points at 'nothing'
- The allocated memory is lost (a memory leak)
- You shouldn't allocate memory for head, it is
just a pointer.
headnew Node headNULL
45A lost cell
46The List Traversal
Let a current pointer point to the first node in
the linked list. while (the current pointer is
not NULL) Display the data portion of the
current node Set the current pointer to the next
pointer of the current node
Node cur ... for (curhead cur!NULL
curcur-gtnext) cout ltlt cur-gtitem ltlt endl
47The effect of the assignment cur cur-gtnext
48Deleting a Specified Node
- Assumption The linked list exists.
- There are three pointers set
- head the first element of the list,
- cur the current element of the list,
- prev the previous element of the list.
- Deletion
prev-gtnextcur-gtnext cur-gtnextNULL delete
cur curNULL
49Deleting a node from a linked list
50Deleting the First Node
- The previous method doesn't work for the first
element, because prev has nothing to point at. - This is a special case.
- When the first node is deleted, the value of head
has to be changed - headhead-gtnext
- The memory occupied by the first element has to
be de-allocated.
51Deleting the first node
52Summary of the deletion process
- Locate the node using the List Traversal
algorithm (keep in mind the special treatment of
the first node). - Disconnect the node from the list by changing the
pointers. - Return the node to the system (de-allocate
memory).
53Inserting a new node
- cur points at the current node,
- prev points at the previous node,
- newPtr points at the new node which will be
inserted before the current node. - 1st special case insertion at the beginning.
- 2nd special case insertion at the end (actually
not so special). -
newPtrnew Node newPtr-gtdata999 newPtr-gtnextcu
r prev-gtnextnewPtr
54Inserting a new node into a linked list
55Inserting at the beginning of the List
- head has to point to the new node.
- the new node has to point at the beginning of the
linked list. -
newPtr-gtnexthead headnewPtr
56Inserting at the beginning of a linked list
57Inserting at the end of the List
- It is not a special case.
- It works exactly as for other nodes (except the
head) - Add the new node when cur is NULL.
-
58Inserting at the end of a linked list
59Summary of the Insertion process
- Determine the point of insertion (using the List
Traversal algorithm) - Create a new node and store the new data in it.
- Connect the new node to the linked list by
changing appropriate pointers.
60Determining cur and prev
- The list should be traversed until the
appropriate place for a new node is found. - Let's create a linked list of sorted numbers.
- Insertions at the beginning and at the end are
covered.
prevNULL curhead while(cur ! NULL and
newValue gt cur-gtitem) prevcur curcur-gtnext
61When prev points to the last node and cur is
NULL, insertion will be at the end of the linked
list
62When prev is NULL and cur points to the first
node, insertion or deletion will be at the
beginning of the linked list
63A pointer-Based ADT List
private struct ListNode // a node
ListItemType item ListNode next
int size //number of items ListNode head
ListNode find(int index) const //
Returns a pointer to // the index-th node
// in the linked list.
typedef desired-type-of-list-item
ListItemType class List public //
constructors and destructor List()
// copy constructor List(const List
aList) List() // list
operations bool isEmpty() const int
getLength() const void insert(int index,
ListItemType newItem) void remove(int
index) void retrieve(int index,
ListItemType dataItem) const
64Array vs. Pointer
- an Array-Based List
- Static arrays fixed size, some space is wasted.
- Dynamic arrays the initial size is approximated,
some space can be wasted, reallocation is costly. - To delete or insert an item all other items has
to be shifted (costly) - a Pointer-Based List
- memory allocation on demand, no direct access to
a specified item (in order to find it the list
has to be searched from its head).
65Recursive Traversal
- Write the contents of a list of characters.
struct Node char item Node next Node
stringPtr void writeString(Node stringPtr)
if (stringPtr ! NULL) // write the first
character cout ltlt stringPtr-gtitem
// write the string minus its first character
writeString(stringPtr-gtnext) // end if
// end writeString
66Recursive Insertion
void linkedListInsert(Node headPtr, ItemType
newItem) if ((headPtr NULL) (newItem lt
headPtr-gtitem)) // base case insert
newItem at beginning // of the linked list
to which headPtr points Node newPtr new
Node if (newPtr NULL) throw
ListException( "ListException insert
cannot allocate memory") else
newPtr-gtitem newItem newPtr-gtnext
headPtr headPtr newPtr //
end if else linkedListInsert(headPtr
-gtnext, newItem) // end linkedListInsert
67A Pointer Implementation
- It is very common to implement a linked list
using pointers and either structures or objects
for the nodes. - Examine the file l-simple.zip for a simple
implementation using pointers.
68Linked List Example -- l-list.zip
- This is an example of a singly linked linear
list. It is in three files, list_node.h,
main.cpp, and list_node.cpp. - This illustrates the general idea of linked lists
done in the classical, procedural style,
characterized by struct for a node. - Any node can be treated as the beginning of
linked list.
69Linked List Example -- l-class.zip
- Implementation of a simple linked list using
classes.
70End of Lecture
- All Programming Assignments due by the Final Exam
day. (12/9) - Final Exam preparation Thurs., 12/2