cpp:interfaces
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| cpp:interfaces [2017/04/25 13:04] – gthanos | cpp:interfaces [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Interfaces ====== | ====== Interfaces ====== | ||
| - | Η C++ δεν διαθέτει // | + | Η 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 int[capacity]; | ||
| + | } | ||
| + | |||
| + | int ArrayStack:: | ||
| + | return stackSize; | ||
| + | } | ||
| + | |||
| + | void ArrayStack:: | ||
| + | if(stackSize==capacity) { | ||
| + | int *_array = new 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 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; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Το Makefile που μεταγλωττίζει τον παραπάνω κώδικα είναι το εξής: | ||
| + | |||
| + | <code makefile Makefile> | ||
| + | |||
| + | StackUsage: ArrayStack.o LinkedStack.o | ||
| + | g++ -Wall -g -std=c++11 StackUsage.cpp ArrayStack.o LinkedStack.o -o StackUsage | ||
| + | |||
| + | ArrayStack.o: | ||
| + | g++ -Wall -g -std=c++11 ArrayStack.cpp -c | ||
| + | |||
| + | LinkedStack.o: | ||
| + | g++ -Wall -g -std=c++11 LinkedStack.cpp -c | ||
| + | |||
| + | clean: | ||
| + | rm *.o StackUsage | ||
| + | </ | ||
| + | |||
| + | Από τον παραπάνω κώδικα παρατηρούμε ότι η abstract κλάση //Stack// παρέχει μία σειρά από //virtual// μεθόδους οι οποίες υλοποιούνται από τις κλάσεις // | ||
| + | |||
| + | <WRAP center round tip 80%> | ||
| + | Τα // | ||
| + | </ | ||
cpp/interfaces.1493125482.txt.gz · Last modified: 2017/04/25 12:04 (external edit)
