ObjectOriented Design Inheritance and Class Examples 1 Inheritance

Object-Oriented Design Inheritance and Class Examples 1

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

INHERITANCE A SIMPLE CLASS TO START WITH Example program ------> VEHICLE. H 3

VEHICLE. 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. 4

THE IMPLEMENTATION FOR VEHICLE Example program ------> 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. 5

USING THE VEHICLE CLASS Example program ------> 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. 6

OUR FIRST DERIVED CLASS Example program ------> 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. 7

OUR FIRST DERIVED CLASS Example program ------> 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. 8

OUR FIRST DERIVED CLASS Example program ------> 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. 9

OUR FIRST DERIVED CLASS Example program ------> 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. 10

HOW 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. 11

HOW 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. 12

A Graphical Representation of an object of this class. 13

Information 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. 14

THE CAR CLASS IMPLEMENTATION Example program ------> 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. 15

ANOTHER DERIVED CLASS Example program ------> 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. 16

A Graphical Representation of the truck class 17

Example program ------> 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. 18

THE TRUCK IMPLEMENTATION Example program ------> TRUCK. CPP § § Examine the file named TRUCK. CPP for the implementation of the truck class. It has nothing unusual included in it. 19

USING ALL THREE CLASSES Example program ------> 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. 20

Example program ------> 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. 21

Example program ------> 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. 22

Compile 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. 23

WHY 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. 24

WHY 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 redeclaration of a class. 25

Linked Lists Examples 26

The Main Concept § Static Array § § § Dynamic 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 Determine the size before allocating memory Costly reallocation, insertion, deletion Data has to be shifted Linked List § Truly dynamic, memory is allocated as needed 27

Linked 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. 28

(a) A linked list of integers; (b) insertion; (c) deletion 29

Pointers § § 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; q=1234; p=q; 30

Pointers: dynamic memory allocation § A pointer can point at the memory area allocated at execution time. int *p; p=new int; *p=1234; p=new int[10]; *(p+2)=1234; p[1]=2222; delete p; delete [] p; 31

A pointer to an integer 32

(a) Declaring pointer variables; (b) pointing to statically allocated memory; (c) assigning a value 33

(d) Allocating memory dynamically; (e) assigning a value; (f) copying a pointer 34

(g) Allocating memory dynamically and assigning a value; (h) assigning NULL to a pointer variable; (i) de-allocating memory 35

An incorrect pointer to a de-allocated node 36

Programming with pointer variables and dynamically allocated memory 37

Programming with pointer variables and dynamically allocated memory 38

Programming with pointer variables and dynamically allocated memory 39

Pointer-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. 40

A node 41

A 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; head=NULL; p=new Node; p->item=1; p->next=NULL; head=p; . . . delete p; 42

A head pointer to a list 43

A misconception head=new Node; § head=NULL; § § § 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. 44

A lost cell 45

The 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 (cur=head; cur!=NULL; cur=cur->next) cout << cur->item << endl; 46

The effect of the assignment cur = cur->next 47

Deleting 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->next=cur->next; cur->next=NULL; delete cur; cur=NULL; 48

Deleting a node from a linked list 49

Deleting 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: § head=head->next; § The memory occupied by the first element has to be de-allocated. 50

Deleting the first node 51

Summary 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 (deallocate memory). 52

Inserting a new node § § § cur – points at the current node, prev – points at the previous node, new. Ptr – points at the new node which will be inserted before the current node. new. Ptr=new Node; new. Ptr->data=999; new. Ptr->next=cur; prev->next=new. Ptr; § § 1 st special case: insertion at the beginning. 2 nd special case: insertion at the end (actually not so special). 53

Inserting a new node into a linked list 54

Inserting 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. new. Ptr->next=head; head=new. Ptr; 55

Inserting at the beginning of a linked list 56

Inserting 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. 57

Inserting at the end of a linked list 58

Summary 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. 59

Determining 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. prev=NULL; cur=head; while(cur != NULL and new. Value > cur->item) { prev=cur; cur=cur->next; } 60

When prev points to the last node and cur is NULL, insertion will be at the end of the linked list 61

When prev is NULL and cur points to the first node, insertion or deletion will be at the beginning of the linked list 62

A pointer-Based ADT List typedef desired-type-of-list-item List. Item. Type; class List { public: // constructors and destructor: List(); // copy constructor List(const List& a. List); ~List(); // list operations: bool is. Empty() const; int get. Length() const; void insert(int index, List. Item. Type new. Item); void remove(int index); void retrieve(int index, List. Item. Type& data. Item) const; private: struct List. Node // a node { List. Item. Type item; List. Node *next; }; int size; //number of items List. Node *head; List. Node *find(int index) const; // Returns a pointer to // the index-th node // in the linked list. }; 63

Array 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). 64

Recursive Traversal § Write the contents of a list of characters. struct Node { char item; Node *next; }; Node *string. Ptr; void write. String(Node *string. Ptr) { if (string. Ptr != NULL) { // write the first character cout << string. Ptr->item; // write the string minus its first character write. String(string. Ptr->next); } // end if } // end write. String 65

Recursive Insertion void linked. List. Insert(Node* &head. Ptr, Item. Type new. Item) { if ((head. Ptr == NULL) || (new. Item < head. Ptr->item)) { // base case: insert new. Item at beginning // of the linked list to which head. Ptr points Node *new. Ptr = new Node; if (new. Ptr == NULL) throw List. Exception( "List. Exception: insert cannot allocate memory"); else { new. Ptr->item = new. Item; new. Ptr->next = head. Ptr; head. Ptr = new. Ptr; } // end if } else linked. List. Insert(head. Ptr->next, new. Item); } // end linked. List. Insert 66

A 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 simplementation using pointers. 67

Linked 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. 68

Linked List Example -- l-class. zip § Implementation of a simple linked list using classes. 69

End of Lecture § All Programming Assignments due by the Final Exam day. (12/9) § Final Exam preparation Thurs. , 12/2 70
- Slides: 70