This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | Next revision Both sides next revision | ||
|
cpp:vector_overloading [2021/05/24 06:35] |
cpp:vector_overloading [2021/05/24 07:29] gthanos |
||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Παράδειγμα υπερφόρτωσης τελεστών ====== | ||
| + | |||
| + | Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση //Vector// η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους. | ||
| + | <code cpp Vector.hpp> | ||
| + | #include < | ||
| + | #include < | ||
| + | using namespace std; | ||
| + | |||
| + | class Vector { | ||
| + | int *array; | ||
| + | int size; | ||
| + | | ||
| + | public: | ||
| + | Vector(int length=0); | ||
| + | Vector(const Vector &v); | ||
| + | Vector(const Vector *v); | ||
| + | ~Vector(); | ||
| + | int length() const;// return Vector' | ||
| + | int & | ||
| + | int find(int a) const; | ||
| + | // if not element not found | ||
| + | | ||
| + | void print() const; | ||
| + | }; | ||
| + | </ | ||
| + | |||
| + | <code cpp Vector.cpp> | ||
| + | #include " | ||
| + | Vector:: | ||
| + | size = length; | ||
| + | array = new (nothrow) int[size]; | ||
| + | if(array==NULL) { | ||
| + | cerr << " | ||
| + | exit(-1); | ||
| + | } | ||
| + | for(int i=0; i<size; i++) | ||
| + | array[i] = 0; | ||
| + | } | ||
| + | |||
| + | Vector:: | ||
| + | size = v.length(); | ||
| + | array = new (nothrow) int[size]; | ||
| + | if(array==NULL) { | ||
| + | cerr << " | ||
| + | exit(-1); | ||
| + | } | ||
| + | for(int i=0; i<size; i++) | ||
| + | array[i] = v.valueAt(i); | ||
| + | } | ||
| + | |||
| + | Vector:: | ||
| + | size = v-> | ||
| + | array = new (nothrow) int[size]; | ||
| + | if(array==NULL) { | ||
| + | cerr << " | ||
| + | exit(-1); | ||
| + | } | ||
| + | for(int i=0; i<size; i++) | ||
| + | array[i] = v-> | ||
| + | } | ||
| + | |||
| + | Vector:: | ||
| + | delete [] array; | ||
| + | } | ||
| + | |||
| + | int Vector:: | ||
| + | return size; | ||
| + | } | ||
| + | |||
| + | int & | ||
| + | if(pos> | ||
| + | cerr << " | ||
| + | | ||
| + | } | ||
| + | return array[pos]; | ||
| + | } | ||
| + | |||
| + | int Vector:: | ||
| + | for(int i=0; i<size; i++) | ||
| + | if(array[i] == a) | ||
| + | return i; | ||
| + | return -1; | ||
| + | } | ||
| + | |||
| + | void Vector:: | ||
| + | for(int i=0; i<size; i++) { | ||
| + | cout << array[i]; | ||
| + | if(i==size-1) | ||
| + | cout << endl; | ||
| + | else | ||
| + | cout << ", "; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | <code cpp VectorUsage.cpp> | ||
| + | #include " | ||
| + | int main() { | ||
| + | Vector v(5); | ||
| + | v.valueAt(0) = 2; v.valueAt(1) = 3; | ||
| + | v.valueAt(2) = 4; v.valueAt(3) = 5; v.valueAt(4) = 6; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής: | ||
| + | * [[cpp: | ||
| + | * [[cpp: | ||
| + | * [[cpp: | ||
| + | |||