Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.

Similar presentations


Presentation on theme: "1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures."— Presentation transcript:

1 1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures

2 expanded by J. Goetz ADT Sorted List Operations Transformers –MakeEmpty –InsertItem –DeleteItem Observers –IsFull –LengthIs –RetrieveItem Iterators –ResetList –GetNextItem change state observe state process all

3 3 expanded by J. Goetz class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos 3

4 4 expanded by J. Goetz What is a Circular Linked List? l A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element. In the linear linked list we have to have a pointer to the beginning to access all nodes. l Here we can start at any node in the list and traverse the whole list ‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’ listData

5 5 expanded by J. Goetz Circular Linked List listData points to the last item in the list, so we have access to the first and the last node: listData->info refers to the last node listData->next->info refers to the first node The pointer never becomes NULL. So we never stop when the traversing pointer becomes NULL. Are good for applications that require access to both ends of the list. Implementation is not shorter or simpler.

6 6 expanded by J. Goetz // File: Circle.cpp // This file contains the member functions for the Circular Linked List // list coded in Chapter 6.1 of the text. // There is no need to change any of the declaration in class SortedType #include template struct NodeType { ItemType info; NodeType* next; }; template class SortedType { public: // constructors // void InsertItem(ItemType item); void DeleteItem(ItemType); …….. // other members go here private: NodeType * listData; int length; };

7 7 expanded by J. Goetz template // similar to RetrieveItem for Linear Linked List SortedType void FindItem(NodeType * listData, ItemType item, NodeType *& location, NodeType *& predLoc, bool& found) //helper function, hidden within implementation p.339 // Assumption: ItemType is a type for which the operators ''<'' and "==" are defined as either an appropriate built-in type or a class that overloads these operations. // Pre: List is not empty. // Post:If there is an element someItem whose key matches item's key, then found = true; otherwise, found = false. // If found, location contains the address of someItem and predLoc contains the address of someItem's predecessor; // otherwise, location contains the address of item's logical successor and predLoc contains the address of item's logical predecessor. { // A. Set values bool moreToSearch = true; location = listData->next; // in the Linear Linked List SortedType was: location = listData; predLoc = listData; //(1) in the Linear Linked List SortedType was: predLoc = NULL; found = false; // B. Find the place while (moreToSearch && !found) { if (item info) moreToSearch = false; //exit3: we have passed the location where item belongs (see Post: description), //exit right away if the item SMALLEST than any in the list, then predLoc = listData from (1) else if (item == location->info) found = true; //exit2: item is FOUND, if only ONE element was: predLoc = listData from (1) else { // move running pointers predLoc = location; location = location->next; moreToSearch = (location != listData->next); //exit1: it can be location = listData->next; (it was location != NULL for //Linear Linked List SortedType ) so the first element again, item is NOT FOUND }

8 8 expanded by J. Goetz template // concept is similar to InsertItem for Linear Linked List SortedType void SortedType ::InsertItem(ItemType item) // Circular Linked List p.340 { NodeType * newNode; NodeType * predLoc; NodeType * location; bool found; // A. Set values: allocate space and store item newNode = new NodeType ; newNode->info = item; // B. store item //C. Find the place where the new element belongs if (listData != NULL) { FindItem(listData, item, location, predLoc, found); // D. Put the new element into the list using predLoc received from FindItem() newNode->next = predLoc->next; //(1) // case a: GENERAL – link new node with the next one predLoc->next = newNode; //(2) link new node with the previous one if (listData->info < item) // case d: General+. If this is the last node (the LARGEST ELEM) in the list. listData = newNode; //(3) reassign listData } // note: general case covers case c: inserting to front of the list-the SMALLEST one; see exit 1, 2 else // E. // case b: Inserting into an EMPTY list. { listData = newNode; // make listData point to the new node newNode->next = newNode; // make the new node point itself } length++; }

9 9 expanded by J. Goetz template // Circular Linked List void SortedType ::DeleteItem(ItemType item) // concept is similar to Linear Linked List SortedType //p.342 { NodeType * location; NodeType * predLoc; bool found; // A. Find FindItem(listData, item, location, predLoc, found); // B. Remove element from the list at location found in FindItem() if (predLoc == location) // check on return from FindItem listData = NULL; //case c: Only ONE node in list else { predLoc->next = location->next; // case a: GENERAL - jump over the node we are deleting // note: general case covers case b: inserting to front of the list-the SMALLEST one; see exit3 in FindItem if (location == listData) // check on return from FindItem - General+ case listData = predLoc; //case d: Deleting last node in list – the LARGEST item } // C. Deallocate location delete location; length--; }

10 10 expanded by J. Goetz ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’ What is a Doubly Linked List? listData l A doubly linked list is a list in which each node is linked to both its successor and its predecessor. l In a circular linked list we cannot access its predecessors and cannot traverse in reverse, a doubly linked list allows the user to print the list in the ascending and descending order.

11 11 expanded by J. Goetz Each node contains two pointers template struct NodeType { ItemType info; // Data member NodeType * back; // Pointer to predecessor NodeType * next; // Pointer to successor };. back. info. next 3000 ‘A’ NULL

12 12 expanded by J. Goetz template // similar to FindItem for Circular Linked List SortedType – see removal of predLoc void FindItem(NodeType * listData, ItemType item, NodeType *& location, bool& found) //helper function, hidden within implementation p.345 // Assumption: ItemType is a type for which the operators ''<'' and "==" are defined as either an appropriate built-in type or a class that overloads these operations. // Pre: List is not empty. // Post:If there is an element someItem whose key matches item's key, then found = true; otherwise, found = false. // If found, location contains the address of someItem; // otherwise, location contains the address of item's logical successor { // A. Set values bool moreToSearch = true; location = listData; // location = listData->next; // in the Linear Linked List SortedType was: location = listData; //predLoc = listData; //(1) in the Linear Linked List SortedType was: predLoc = NULL; found = false; // B. Find the place while (moreToSearch && !found) { if (item info) moreToSearch = false; //exit3: we have passed the location where item belongs (see Post: description), //exit right away if the item SMALLEST than any in the list, then predLoc = listData from (1) else if (item == location->info) found = true; //exit2: item is FOUND, if only ONE element was: predLoc = listData from (1) else { // move a pointer // predLoc = location; location = location->next; moreToSearch = (location != listData->next); //exit1: it can be location = listData->next; (it was location != NULL for //Linear Linked List SortedType ) so the first element again, item is NOT FOUND }

13 13 expanded by J. Goetz Doubly Linked List The algorithms for the insertion and deletion more complicated because there are more pointers to keep track.

14 14 expanded by J. Goetz Linking the New Node into the List

15 15 expanded by J. Goetz Deleting from a Doubly Linked List

16 16 expanded by J. Goetz What are Header and Trailer Nodes? listData INT_MIN 5 8 13 INT_MAX A goal: simplify linked list by making sure that we never have end cases i.e. never insert or delete at the ends of the list A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. A Trailer Node is a node at the end of a list that contains a key larger than any possible key. Both header and trailer are placeholding nodes used to simplify list processing.

17 17 expanded by J. Goetz Recall Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack. A stack is a LIFO “last in, first out” structure.

18 expanded by J. Goetz Stack ADT Operations MakeEmpty -- Sets stack to an empty state. IsEmpty -- Determines whether the stack is currently empty. IsFull -- Determines whether the stack is currently full. Push (ItemType newItem) -- Adds newItem to the top of the stack. Pop () -- Removes the item at the top of the stack and returns it in item. Top (ItemType& item) -- Returns a copy of the top item 18

19 19 expanded by J. Goetz class StackType StackType MakeEmpty Pop and Top Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

20 20 expanded by J. Goetz What happens... When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop; Top Push IsFull IsEmpty Private data: topPtr ~StackType 20 30

21 21 expanded by J. Goetz // FUNCTION CODE template void MyFunction( StackType SomeStack ) // SomeStack – formal parameter // Uses pass by value {. } 21 Passing a class object by value

22 22 expanded by J. Goetz Shallow Copy vs. Deep Copy A shallow copy copies only the class data members, and does not copy any pointed-to data. A deep copy copies not only the class data members, but also makes separately stored copies of any pointed-to data.

23 23 expanded by J. Goetz Pass by value makes a shallow copy 20 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); // function call Private data: 7000 6000 topPtr 7000 Private data: topPtr 7000 MyStack SomeStack (formal parameter) shallow copy

24 24 expanded by J. Goetz What’s the difference? A shallow copy shares the pointed to data with the original class object. A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

25 25 expanded by J. Goetz Making a deep copy 20 30 Private data: 7000 6000 topPtr 7000 SomeStack (formal parameter) 20 30 Private data: 5000 2000 topPtr 5000 MyStack deep copy

26 26 expanded by J. Goetz // FUNCTION CODE template void MyFunction( StackType SomeStack ) // Uses pass by value { ItemType item; SomeStack.Pop(); SomeStack.Top(item);. } WHAT HAPPENS IN THE SHALLOW COPY SCENARIO? 26 Suppose MyFunction Uses Pop

27 27 expanded by J. Goetz MyStack.topPtr is left dangling ? 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); Private data: topPtr 6000 MyStack SomeStack (formal parameter) shallow copy Private data: 7000 6000 topPtr 7000

28 28 expanded by J. Goetz MyStack.topPtr is left dangling ? 30 Private data: topPtr 6000 MyStack SomeStack ( formal parameter) shallow copy Private data: 7000 6000 topPtr 7000 NOTICE THAT NOT JUST FOR THE SHALLOW COPY, BUT ALSO FOR ACTUAL PARAMETER MyStack, THE DYNAMIC DATA HAS CHANGED!

29 29 expanded by J. Goetz As a result... This default method used for pass by value is not the best way when a data member pointer points to dynamic data. Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

30 30 expanded by J. Goetz More about copy constructors When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value. You do not call the copy constructor. Like other constructors, it has no return type. Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

31 31 expanded by J. Goetz Copy Constructor If a copy constructor is present, the default method of initialization (member by member copying) is inhibited. Instead, the copy- constructor is implicitly invoked whenever one class object is initialized by another Copy constructor is a special member function of a class that is implicitly called in these three situations: 1. passing object parameters by value, 2. initializing an object variable in a declaration, 3. returning an object as the return value of a function.

32 32 expanded by J. Goetz // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType( const StackType & anotherStack ); // If a Copy constructor is present, the default method of initialization (member by member copying) is inhibited. Instead, the copy-constructor is implicitly invoked whenever one class object is initialized by another // Implicitly called for pass by value.. ~StackType( ); // DESTRUCTOR is called automatically when the class // instance goes out of scope // POST: Memory for nodes has been deallocated. private: NodeType * topPtr ; }; 32

33 33 expanded by J. Goetz CLASS CONSTRUCTOR CLASS COPY CONSTRUCTOR CLASS DESTRUCTOR DESTRUCTOR is called automatically when the class instance goes out of scope Classes with Data Member Pointers Need

34 34 expanded by J. Goetz template // COPY CONSTRUCTOR p.342 StackType :: // 2 running pointers ptr1 => ptr2 StackType( const StackType & anotherStack ) // copy anotherStack to self {NodeType * ptr1 ; //points to the node to be copied from(of anotherStack) NodeType * ptr2 ; //points to the last node copied to the self stack if ( anotherStack.topPtr == NULL ) topPtr = NULL ; //so empty self stack; topPtr a private data else // allocate memory for the first node {topPtr = new NodeType ; //(1)allocate memory topPtr->info = anotherStack.topPtr->info ; //(2)copy info ptr1 = anotherStack.topPtr->next ; //(3)copy the first link pointer ptr2 = topPtr ; //(4)ptr2 starts from topPtr while ( ptr1 != NULL ) // deep copy other nodes { ptr2->next = new NodeType ; //(5) create a new next node ptr2 = ptr2->next ; //(6) move ptr2 to the last of the self ptr2->info = ptr1->info; //(7) copy info to the corresponding one ptr1 = ptr1->next ; //(8) move ptr1 to the next one of anotherStack } ptr2->next = NULL ; //(9)while condition gives ptr1 = NULL so the self ends } }gtt5 34

35 35 expanded by J. Goetz // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. // POST: Stack is created and empty. StackType( const StackType & anotherStack ); // Copy constructor. friend void copy (StackType, StackType &) //A friend function is not a member of the class, but it has permission to access private class members directly. ~StackType( ); // Destructor. // POST: Memory for nodes has been deallocated. private: NodeType * topPtr ; };

36 36 expanded by J. Goetz Copy routine is a friend of the class StackType so has access to private variables in the class template // COPY FUNCTION - copy anotherStack to mycopy object p.356 //StackType :: Copy is no a member function, so 2 stacks should be passed //by parameters //2 running pointers ptr1 => ptr2 void Copy (StackType anotherStack, StackType & mycopy) {NodeType * ptr1 ; //points to the node to be copied from(of anotherStack) NodeType * ptr2 ; //points to the last node copied to the mycopy stack if ( anotherStack.topPtr == NULL ) mycopy.topPtr = NULL //so empty mycopy stack; topPtr a private data else // allocate memory for the first node { mycopy.topPtr = new NodeType ; //(1)allocate memory mycopy.topPtr->info = anotherStack.topPtr->info ; //(2)copy info ptr1 = anotherStack.topPtr->next ; //(3)copy the first link pointer ptr2 = mycopy.topPtr ; //(4)ptr2 starts from topPtr while ( ptr1 != NULL ) // deep copy other nodes { ptr2->next = new NodeType ; //(5) create a new next node ptr2 = ptr2->next ; //(6) move ptr2 to the last of the copy object ptr2->info = ptr1->info; //(7) copy info to the corresponding one ptr1 = ptr1->next ; //(8) move ptr1 to the next one of anotherStack } ptr2->next = NULL ; //(9)while condition gives ptr1 = NULL so the mycopy ends } // almost identical to the copy constructor, the difference is marked by mycopy

37 37 expanded by J. Goetz What about the assignment operator? The default method used for assignment operator (=) of class objects makes a shallow copy. If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

38 38 expanded by J. Goetz // DYNAMICALLY LINKED IMPLEMENTATION OF STACK template class StackType { public: StackType( ); // Default constructor. StackType( const StackType & anotherStack ); // Copy constructor. void operator= ( StackType ); // Overloads assignment operator.... ~StackType( ); // Destructor. private: NodeType * topPtr ; }; // The function definition looks like this template void StackType ::operator= (StackType anotherStack) { //body } 38

39 expanded by J. Goetz Using Overloaded Binary operator= When a Member Function was defined myStack = yourStack myStack.operator=(yourStack) When a Friend Function was defined myStack = yourStack operator=(myStack, yourStack) 39

40 40 expanded by J. Goetz C++ Operator Overloading Guides 1All operators except these ::. sizeof ?: may be overloaded. 2At least one operand must be a class instance. 3You cannot change precedence, operator symbols, or number of operands. 4Overloading ++ and -- requires prefix form use by default, unless special mechanism is used. 5To overload these operators = ( ) [ ] member functions (not friend functions) must be used. 6An operator can be given multiple meanings if the data types of operands differ. 7 Many meanings for an operator can coexist as long as the compiler can distinguish among the data types of the operands.

41 41 expanded by J. Goetz Using Overloaded Binary operator+ When a Member Function was defined myStack + yourStack myStack.operator+(yourStack) When a Friend Function was defined myStack + yourStack operator+(myStack, yourStack)

42 42 expanded by J. Goetz Linked list in an Array of Recordes Linked structures problems: –Write a linked list to a file is meaningless on the next run time –Dynamic allocation of each node is time expensive, specifically for the operating system code An array index remains valid on the next run of the program, so we can implement an array of nodes

43 43 expanded by J. Goetz A Sorted linked list stored in an Array of Nodes

44 44 expanded by J. Goetz A Sorted linked list Stored in an Array of Nodes Each node tells us the array index of the succeeding node See the chain of next indexes – following the links in the next member you can get the linked list the linked structure uses array indexes as “pointers” null is represented by no nodes[-1] exists or by NUL = - 1 no contain value elements in the list constitute free space

45 45 expanded by J. Goetz An Array with linked list of values and the list of free space Write own function to: –allocate nodes from the free space – GetNode() –delete nodes and return to the pool of free space – FreeNode() list, free are external pointers to the list of values and the list of free space, respectively

46 46 expanded by J. Goetz An Array with Three Lists (Including the Free List)

47 47 expanded by J. Goetz // file: ArrayLL.cpp p.366-7 // This file contains the code for the array-of-records implementation // in the text. Incomplete definitions of ListType and NodeType and // one stub routine have been included to confirm that the syntax of // this code is correct. The student must write a complete definition // to use this code. typedef int ItemType; struct NodeType; struct MemoryType { int free; // index of the first free node NodeType* nodes; // a true pointer to the dynamically // allocated array of nodes }; // An incomplete definition of ListType sufficient to compile the code // in the array linked list implementation class ListType { public: ListType(); ListType(int); ~ListType(); // Other member function prototypes go here. void MakeEmpty(); bool IsFull() const; int LengthIs() const; void RetrieveItem(ItemType& item, bool& found); void InsertItem(ItemType item); void DeleteItem(ItemType item); void ResetList(); void GetNextItem(ItemType& item); private: int listData; int currentPos; int length; int maxItems; // new MemoryType storage; // new }; // Prototypes of auxiliary functions. void GetNode(int& nodeIndex, MemoryType& storage); // Returns the index of a free node in nodeIndex. void FreeNode(int nodeIndex, MemoryType& storage); // Returns nodeIndex to storage. void InitializeMemory(int maxItems, MemoryType&); // Initializes all memory to the free list. // Define end-of-list symbol. const int NUL = -1; struct NodeType { int info; int next; }; // Definitions of the auxillary functions: void InitializeMemory(int maxItems, MemoryType& storage) { for (int index = 1; index < maxItems; index++) storage.nodes[index-1].next = index; storage.nodes[maxItems-1].next = NUL; storage.free = 0; }

48 48 expanded by J. Goetz void GetNode(int& nodeIndex, MemoryType& storage) { nodeIndex = storage.free; // take first free one storage.free = storage.nodes[storage.free].next; // update free } void FreeNode(int nodeIndex, MemoryType& storage) { // take the nodeIndex received as a parameter and link it into the list of free nodes storage.nodes[nodeIndex].next = storage.free; storage.free = nodeIndex; // put as a first one }

49 49 expanded by J. Goetz //The class constructors for class ListType must allocate the storage //for the array of records and call InitializeMemory. For the default //constructor, we arbitrarily choose an array size of 500. ListType::ListType(int max) { length = 0; maxItems = max; storage.nodes = new NodeType[max]; // pointer to the dynamically allocated array of nodes InitializeMemory(maxItems, storage); listData = NUL; // index of an array } ListType::ListType() { length = 0; maxItems = 500; storage.nodes = new NodeType[500]; InitializeMemory(500, storage); listData = NUL; } ListType::~ListType() { delete [] storage.nodes; } void InsertItem(ItemType item) { // STUB: REPLACE WITH USEFUL CODE. }

50 50 expanded by J. Goetz Composition (containment) Composition (or containment) means that an internal data member of one class is defined to be an object of another class type. A FAMILIAR EXAMPLE...

51 51 expanded by J. Goetz Private data value ComparedTo Print Initialize class ItemType ItemType Class Interface Diagram

52 52 expanded by J. Goetz Sorted list contains an array of ItemType SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos GetNextItem

53 53 expanded by J. Goetz Inheritance Inheritance is a means by which one class acquires the properties--both data and operations--of another class. When this occurs, the class being inherited from is called the Base Class. The class that inherits is called the Derived Class. AN EXAMPLE...

54 54 expanded by J. Goetz Inheritance A derived class generally is longer but represents a smaller set of more specific objects than its base class (superclass) An object of derived class can be treated as an object of its corresponding base class, but the reverse is not true, so Derived object “is a” base class object through inheritance Reusability can be achieved via inheritance

55 55 expanded by J. Goetz Recall Definition of Queue Logical (or ADT) level: A queue is an ordered group of homogeneous items (elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front). A queue is a FIFO “first in, first out” structure.

56 expanded by J. Goetz Queue ADT Operations MakeEmpty -- Sets queue to an empty state. IsEmpty -- Determines whether the queue is currently empty. IsFull -- Determines whether the queue is currently full. Enqueue (ItemType newItem) -- Adds newItem to the rear of the queue. Dequeue (ItemType& item) -- Removes the item at the front of the queue and returns it in item. 56

57 57 expanded by J. Goetz Class interface diagram for class QueType QueType ~QueType Enqueue Dequeue. Private Data: Front Rear ‘C’‘Z’ ‘T’

58 58 expanded by J. Goetz // DYNAMICALLY LINKED IMPLEMENTATION OF QUEUE #include "ItemType.h" // for ItemType template class QueType { public: QueType( ); // CONSTRUCTOR ~QueType( ) ;// DESTRUCTOR bool IsEmpty( ) const; bool IsFull( ) const; void Enqueue( ItemType item ); void Dequeue( ItemType& item ); void MakeEmpty( ); private: NodeType * Front; NodeType * Rear; }; 58

59 59 expanded by J. Goetz 59 // DERIVED CLASS CountedQue FROM BASE CLASS QueType template class CountedQue : public QueType // “:” CountedQue is derived // from QueType { public: CountedQue( ); void Enqueue( ItemType newItem ); //redefining the inherited function void Dequeue( ItemType& item ); //redefining the inherited function int LengthIs( ) const; // new // Returns number of items on the counted queue. private: int length; // new }; SAYS ALL PUBLIC MEMBERS OF QueType CAN BE INVOKED FOR OBJECTS OF TYPE CountedQue

60 60 expanded by J. Goetz Class interface diagram for class CountedQue QueType ~QueType Enqueue Dequeue. Private Data: Front Rear ‘C’‘Z’ ‘T’ CountedQue LengthIs Enqueue Dequeue. Private Data: length 3

61 61 expanded by J. Goetz 61 // Member function definitions for class CountedQue // Constructor initializer template CountedQue ::CountedQue( ) : QueType ( ) //”:” – it causes the invocation of the base-class constructor { length = 0 ; } template int CountedQue ::LengthIs( ) const { return length ; }

62 62 expanded by J. Goetz template void CountedQue ::Enqueue( ItemType newItem ) // Adds newItem to the rear of the queue. // Increments length. { length++; QueType ::Enqueue( newItem ); } template void CountedQue ::Dequeue(ItemType& item ) // Removes item from the rear of the queue. // Decrements length. { length--; QueType ::Dequeue( item ); } 62

63 63 expanded by J. Goetz // File: VirDemo.cpp// This program demonstrates the use of Polymorhism with virtual functions. #include // specification class One { public: virtual void Print(); // virtual function !!! }; // end of One class Two : public One { public: void Print(); // virtual because Print is virtual in One – in all derived classes as well }; // end of One // Function definitions: void One::Print(){ using namespace std; cout << "Print member function of class One" << endl;} void Two::Print(){ using namespace std; cout << "Print member function of class Two" << endl;} void PrintTest(One* ); // prototype int main() { using namespace std; One* onePtr; onePtr = new One; // points to the base class cout << "Result of passing an object of class One: " ; PrintTest(onePtr); onePtr = new Two; // points to the derived class using the base class pointer cout << "Result of passing an object of class Two: "; PrintTest(onePtr); return 0; } void PrintTest(One* ptr) { ptr->Print();} // formal parameter must be a base class type, because // an object of derived class can be treated as an object of its corresponding base class, so it can be passed also.


Download ppt "1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures."

Similar presentations


Ads by Google