This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
cpp:interfaces [2017/04/26 08:13] gthanos |
cpp:interfaces [2021/05/07 08:42] |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Interfaces ====== | ||
| - | |||
| - | Η C++ δεν διαθέτει // | ||
| - | |||
| - | Η //abstract base class Stack// έχει δύο υποκλάσεις την κλάση // | ||
| - | |||
| - | <code cpp Stack.h> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | #ifndef __STACK_H__ | ||
| - | #define __STACK_H__ | ||
| - | |||
| - | class Stack { | ||
| - | public: | ||
| - | virtual int size()=0; | ||
| - | virtual void push(int o)=0; | ||
| - | virtual int pop()=0; | ||
| - | virtual int top()=0; | ||
| - | }; | ||
| - | #endif | ||
| - | </ | ||
| - | |||
| - | <code cpp ArrayStack.h> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | #include " | ||
| - | |||
| - | #ifndef __ARRAYSTACK_H__ | ||
| - | #define __ARRAYSTACK_H__ | ||
| - | |||
| - | class ArrayStack : public Stack { | ||
| - | int *array; | ||
| - | int stackSize, capacity; | ||
| - | public: | ||
| - | ArrayStack(int capacity=16); | ||
| - | int size(); | ||
| - | void push(int o); | ||
| - | int pop(); | ||
| - | int top(); | ||
| - | }; | ||
| - | #endif | ||
| - | </ | ||
| - | |||
| - | <code cpp ArrayStack.cpp> | ||
| - | #include " | ||
| - | |||
| - | ArrayStack:: | ||
| - | capacity = cap; | ||
| - | stackSize = 0; | ||
| - | array = new (nothrow) int[capacity]; | ||
| - | } | ||
| - | |||
| - | int ArrayStack:: | ||
| - | return stackSize; | ||
| - | } | ||
| - | |||
| - | void ArrayStack:: | ||
| - | if(stackSize==capacity) { | ||
| - | int *_array = new (nothrow) int[2*capacity]; | ||
| - | for(int i=0; i< | ||
| - | _array[i] = array[i]; | ||
| - | delete[] array; | ||
| - | array = _array; | ||
| - | } | ||
| - | array[stackSize++] = o; | ||
| - | } | ||
| - | |||
| - | int ArrayStack:: | ||
| - | return array[--stackSize]; | ||
| - | } | ||
| - | |||
| - | int ArrayStack:: | ||
| - | return array[stackSize]; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code cpp LinkedStack.h> | ||
| - | #include < | ||
| - | using namespace std; | ||
| - | |||
| - | #include " | ||
| - | |||
| - | #ifndef __LINKEDSTACK_H__ | ||
| - | #define __LINKEDSTACK_H__ | ||
| - | |||
| - | class LinkedNode { | ||
| - | int element; | ||
| - | LinkedNode *next; | ||
| - | public: | ||
| - | LinkedNode(LinkedNode *nxt, int e); | ||
| - | LinkedNode(int e); | ||
| - | int getElement(); | ||
| - | LinkedNode* getNext(); | ||
| - | void setElement(int e); | ||
| - | void setNext(LinkedNode *nxt); | ||
| - | }; | ||
| - | |||
| - | class LinkedStack : public Stack { | ||
| - | int stackSize; | ||
| - | LinkedNode *head; | ||
| - | public: | ||
| - | LinkedStack(); | ||
| - | int size(); | ||
| - | void push(int o); | ||
| - | int pop(); | ||
| - | int top(); | ||
| - | }; | ||
| - | #endif | ||
| - | </ | ||
| - | |||
| - | <code cpp LinkedStack.cpp> | ||
| - | |||
| - | #include " | ||
| - | |||
| - | /* LinkedNode function | ||
| - | * */ | ||
| - | |||
| - | LinkedNode:: | ||
| - | next = nxt; | ||
| - | element = e; | ||
| - | } | ||
| - | |||
| - | LinkedNode:: | ||
| - | int LinkedNode:: | ||
| - | LinkedNode* LinkedNode:: | ||
| - | void LinkedNode:: | ||
| - | void LinkedNode:: | ||
| - | |||
| - | /* LinkedStack function | ||
| - | * */ | ||
| - | |||
| - | LinkedStack:: | ||
| - | stackSize = 0; | ||
| - | head = NULL; | ||
| - | } | ||
| - | |||
| - | int LinkedStack:: | ||
| - | |||
| - | void LinkedStack:: | ||
| - | LinkedNode *node = new (nothrow) LinkedNode(head, | ||
| - | head = node; | ||
| - | stackSize++; | ||
| - | } | ||
| - | |||
| - | int LinkedStack:: | ||
| - | if(size()==0) | ||
| - | return -1; | ||
| - | LinkedNode *node = head; | ||
| - | head = head-> | ||
| - | int ptr = node-> | ||
| - | delete node; | ||
| - | stackSize--; | ||
| - | return ptr; | ||
| - | } | ||
| - | |||
| - | int LinkedStack:: | ||
| - | return head-> | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | <code cpp StackUsage.cpp> | ||
| - | #include " | ||
| - | #include " | ||
| - | |||
| - | #define ARRAY_SIZE 10 | ||
| - | |||
| - | void invertArray(int array[], int arraySize, Stack &stack) { | ||
| - | for(int i=0; i< | ||
| - | stack.push(array[i]); | ||
| - | | ||
| - | for(int i=0; i< | ||
| - | array[i] = stack.pop(); | ||
| - | } | ||
| - | |||
| - | int main() { | ||
| - | | ||
| - | // | ||
| - | LinkedStack st; | ||
| - | int array[] = { 100, 99, 98, 97, 96, 95, 94, 93, 92, 91 }; | ||
| - | | ||
| - | for(int e:array) | ||
| - | cout << e << endl; | ||
| - | cout << endl; | ||
| - | | ||
| - | invertArray(array, | ||
| - | | ||
| - | for(int e:array) | ||
| - | cout << e << endl; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | Οι παραπάνω κλάσεις μεταγλωττίζονται ως εξής: | ||
| - | < | ||
| - | g++ -g -std=c++11 -o stack StackUsage.cpp ArrayStack.cpp LinkedStack.cpp | ||
| - | </ | ||
| - | |||
| - | Από τον παραπάνω κώδικα παρατηρούμε ότι η κλάση abstract κλάση //Stack// παρέχει μία σειρά από //virtual// μεθόδους οι οποίες υλοποιούνται στις // | ||
| - | |||
| - | <WRAP center round tip 80%> | ||
| - | Τα // | ||
| - | </ | ||