








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of abstract classes and multiple inheritance in C++. An abstract class is a base class that cannot be used to create objects but defines a common interface for a set of related classes. an example of an Item class that inherits from both Exam and Ordered classes, demonstrating how abstract classes and multiple inheritance can be used to define a wrapper class with more functions than the original class without duplicating existing ones.
Typology: Study notes
1 / 14
This page cannot be seen from the preview
Don't miss anything!
2 // Ordered base class -- An abstract class 3 // A. Fischer June 8, 1998 file: ordered.hpp 4 // 5 #ifndef ORDERED_H 6 #define ORDERED_H 7 #include <limits.h> 8 #include <iostream.h> 9 // ------------------------------------------------------------------------ 10 class Ordered { 11 public: 12 virtual ~Ordered(){} 13 virtual KeyType key() const =0; 14 virtual bool operator < (const KeyType&) const =0; 15 virtual bool operator == (const KeyType&) const =0; 16 }; 17 #endif
16.3 Multiple Inheritance
16.3.1 Item: The Data Class
16.4 Linear Containers You Can Search
16.4.1 PQueue: a Sorted Linear Container
45 // Priority queues: derived from Container-<--Linear-<--PQueue 46 // A. Fischer June 9, 2001 file: pqueue.hpp 47 // ------------------------------------------------------------------------ 48 #ifndef PQUEUE_H 49 #define PQUEUE_H 50 #include "linear.hpp" 51 52 class PQueue : public Linear { 53 public: // ----------------------------------------------------- 54 PQueue(){} 55 ~PQueue(){} 56 void focus(){ reset(); } // Priority queue deletion is at the head. 57 58 // ------------------------ Insert new Cell in ascending sorted order. 59 void PQueue::insert( Cell* cp ) { 60 for (reset(); !end(); ++this) { // locate insertion spot. 61 if ( !(this < cp) )break; 62 } 63 Linear::insert( cp ); // do the insertion. 64 } 65 }; 66 #endif
16.4.2 List: An Unordered Container
68 // Unsorted list: derived from Container-<--Linear-<--List 69 // A. Fischer June 9, 2001 file: list.hpp 70 // ------------------------------------------------------------------------ 71 #ifndef LIST_H 72 #define LIST_H 73 #include "linear.hpp" 74 #include "item.hpp" 75 76 // ------------------------------------------------------------------------ 77 class List : public Linear { 78 public: 79 void insert( Cell* cp ) { reset(); Linear::insert(cp); } 80 81 // -------------------------------------------------------------------- 82 void focus(){ 83 KeyType k; 84 cout <<"\n What key would you like to remove? "; 85 cin >> k; 86 for (reset(); !end(); ++this) if (this == k) break; 87 } 88 }; 89 #endif
Item
1
Container
+^
: void : void
insert( Cell* ) focus()
Stack
Queue
Exam
Ordered
: void : void
PQueue() ~PQueue() insert(Cell*) focus()
PQueue
+^ List
Cell
: void : bool : void : void : void : Cell* : void : bool : bool : bool : void : Item* : Item* : ostream&
ostream& operator<<(ostream&, Linear&)
head : Cell* here : Cell* prior: Cell*
Linear() ~Linear() reset() end() operator++() insert( Cell* ) focus() remove setPrior( Cell* ) operator< ( Cell) operator< (KeyType) operator==(KeyType) put( Exam ) pop() peek() print( ostream& )
Linear (abstract)
=
V
1
Deleting Cell 0x0x33630... Dequeue one item from P and push onto L. Deleting Cell 0x0x336b0... Pushing 33 onto P.
Peek at P: Leo: 37
The list contains: <[ Cell 0x0x336b0 [Dan: 44 , 0x33650] Cell 0x0x33650 [Max: 18 , 0x33610] Cell 0x0x33610 [Ned: 29 , 0x0] ]>
The priority queue contains: <[ Cell 0x0x33630 [Leo: 37 , 0x376f0] Cell 0x0x376f0 [Cil: 33 , 0x33670] Cell 0x0x33670 [Bea: 22 , 0x33690] Cell 0x0x33690 [Ali: 11 , 0x0] ]>
Normal termination.
Deleting Cell 0x0x335f0...Deleting Item 44 Deleting Score Dan... Deleting Cell 0x0x336b0...Deleting Item 18 Deleting Score Max... Deleting Cell 0x0x33650...Deleting Item 29 Deleting Score Ned... Deleting Cell 0x0x33610... Deleting Cell 0x0x335e0...Deleting Item 37 Deleting Score Leo... Deleting Cell 0x0x33630...Deleting Item 33 Deleting Score Cil... Deleting Cell 0x0x376f0...Deleting Item 22 Deleting Score Bea... Deleting Cell 0x0x33670...Deleting Item 11 Deleting Score Ali... Deleting Cell 0x0x33690... multiple has exited with status 0.
16.5 C++ Has Four Kinds of Casts
16.5.1 Static Casts
16.5.2 Reinterpret Casts
140 #include <iostream.h> 141 using namespace std; 142 // ------------------------------------------------------------ File: "assign.cpp" 143 int main( void ) 144 { 145 int my_int = 987654321; 146 int* p_int = & my_int; 147 float* p_float = (float*) p_int; 148 float answer = *p_float + 1.0; 149 cerr <<answer <<"\n"; 150 }
fp= (float)p_int // ordinary C syntax. fp= reinterpret_cast<float>(ip); // explicit C++ syntax.
16.5.3 Const Casts
16.6.1 Virtual Inheritance
151 // ------------------------------------------------- File: "donut.hpp" 152 #ifndef DONUT_H 153 #define DONUT_H 154 #include
Dumping aa A::x = 11. Dumping cc A::x = 11. C::a = 30 Dumping the B part of dd A::x = 11. B::x = 22.2 B::a = 20
donut.hpp: In method ‘void D::dump()’: donut.hpp:37: cannot convert a pointer of type ‘D’ to a pointer of type ‘A’ donut.hpp:37: because ‘A’ is an ambiguous base class
Dumping br A::x = 11. B::x = 22.2 B::a = 20
Dumping ap A::x = 11.
Error in function ‘int main()’ of donut.cpp: donut.cpp:20: dynamic_cast from ‘C’ to private base class ‘A’ A* ap = dynamic_cast<A*>(&cc);
donut.cpp:24: cannot dynamic_cast ‘ap’ (of type ‘class A *’) to type ‘class C *’
Dumping dp after up and down casts, D->B->A->D. A::x = 11. A::x = 11. B::x = 22.2 B::a = 20 A::x = 11. C::a = 30 D::f = 44.
Dumping cp after up and down casts, D->B->A->C. A::x = 11. A::x = 11. B::x = 22.2 B::a = 20 A::x = 11. C::a = 30 D::f = 44.
donut.cpp:33: warning: dynamic_cast of ‘class A aa’ to ‘class C *’ can never succeed
Dumping after down casts, A->C. No exception was thrown. Bus error