cpp:vector_overloading_binary_operators2
This is an old revision of the document!
Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης
Παρακάτω θα δούμε την υπερφόρτωση τελεστών που μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης. Οι τελεστές περιγράφονται στον παρακάτω πίνακα.
Τελεστής | Αριστερός τελεστέος | Δεξιός τελεστέος | Περιγραφή |
---|---|---|---|
= | Vector | Vector | Ο αριστερός τελεστέος ισούται με τον δεξιό τελεστέο. Επιστρέφεται μία αναφορά στον αριστερό τελεστέο. |
+= | Vector | Vector | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου |
-= | Vector | Vector | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου |
+= | Vector | int | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος |
-= | Vector | int | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος |
*= | Vector | Vector | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. |
*= | Vector | int | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. |
/= | Vector | int | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. |
%= | Vector | int | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το υπόλοιπο της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. |
<<= | Vector | int | Στον αριστερό τελεστέο προστίθεται ο ακέραιος (δεξιός τελεστέος), αυξάνοντας τη χωρητικότητα του κατά 1. |
>>= | Vector | int | Από τον αριστερό τελεστέο αφαιρείται ο ακέραιος (δεξιός τελεστέος), μειώνοντας τη χωρητικότητα του κατά 1, εφόσον αυτός υπάρχει. Εάν δεν υπάρχει ο αριστερός τελεστέος παραμένει αμετάβλητος. |
[ ] | Vector | int | Επιστρέφει μία αναφορά στο i-στο στοιχείο του Vector, i η θέση που προσδιορίζεται από τον δεξιό τελεστή. |
- Vector.cpp
#include <iostream> #include <string> #include <cstdlib> using namespace std; class Vector { int *array; unsigned int size; public: Vector(unsigned int length=0); Vector(const Vector &v); Vector(const Vector *v); ~Vector(); unsigned int length() const; // return Vector's length. int &valueAt(unsigned int pos) const; // return a reference to element at position pos int find(int a) const; // check if a exists in Vector. Return it position >0 or -1 // if not element not found void print() const; // print vector values to standard output void print(string &msg) const; // print vector values to standard output /* binary operators that don't modify left operand */ Vector &operator=(const Vector &v); Vector operator+(const Vector &v) const; Vector operator-(const Vector &v) const; Vector operator*(Vector &v) const; /* binary operators that modify left operand */ Vector & operator+=(const Vector &v); Vector & operator+=(int a); Vector & operator-=(const Vector &v); Vector & operator-=(int a); Vector & operator*=(const Vector &v); Vector & operator*=(int a); Vector & operator/=(int a); Vector & operator%=(int a); Vector & operator<<=(int a); Vector & operator>>=(int a); int &operator[](int a) const; }; Vector::Vector(unsigned int length) { size = length; array = new (nothrow) int[size]; if(array==NULL) { cerr << "Memory allocation failure!" << endl; exit(-1); } for(int i=0; i<size; i++) array[i] = 0; } Vector::Vector(const Vector &v) { size = v.length(); array = new (nothrow) int[size]; if(array==NULL) { cerr << "Memory allocation failure!" << endl; exit(-1); } for(int i=0; i<size; i++) array[i] = v.valueAt(i); } Vector::Vector(const Vector *v) { size = v->length(); array = new (nothrow) int[size]; if(array==NULL) { cerr << "Memory allocation failure!" << endl; exit(-1); } for(int i=0; i<size; i++) array[i] = v->valueAt(i); } Vector::~Vector() { delete [] array; } unsigned int Vector::length() const { return size; } int &Vector::valueAt(unsigned int pos) const { if(pos>=length()) { cerr << "Invalid access position!\n"; return array[size-1]; } return array[pos]; } int Vector::find(int a) const { for(int i=0; i<size; i++) if(array[i] == a) return i; return -1; } void Vector::print() const { for(int i=0; i<size; i++) { cout << array[i]; if(i==size-1) cout << endl; else cout << ", "; } } void Vector::print(string &msg) const { cout << msg; for(int i=0; i<size; i++) { cout << array[i]; if(i==size-1) cout << endl; else cout << ", "; } } Vector &Vector::operator=(const Vector &v) { if(array!=NULL) delete [] array; size = v.length(); array = new (nothrow) int[size]; if(array==NULL) { cerr << "Memory allocation failure!" << endl; exit(-1); } for(int i=0; i<size; i++) array[i] = v.valueAt(i); return *this; } Vector Vector::operator+(const Vector &v) const { int length; if (size > v.length()) length = size; else length = v.length(); Vector n(length); for(int i=0; i<length; i++) { int sum = 0; if(i<size) sum += array[i]; if(i<v.length()) sum += v.array[i]; n.array[i] = sum; } return n; } Vector Vector::operator-(const Vector &v) const { int length; if (size > v.length()) length = size; else length = v.length(); Vector n(length); for(int i=0; i<length; i++) { int sum = 0; if(i<size) sum += array[i]; if(i<v.length()) sum -= v.array[i]; n.array[i] = sum; } return n; } Vector & Vector::operator+=(const Vector &v) { if(v.length() > size) { Vector n(v); n = n + *this; *this = n; } else { *this = *this + v; } return *this; } Vector & Vector::operator+=(int a) { for(int i=0; i<size; i++) array[i] += a; return *this; } Vector & Vector::operator-=(const Vector &v) { if(v.length() > size) { Vector n(v); n = n - *this; *this = n; } else { *this = *this - v; } return *this; } Vector & Vector::operator-=(int a) { for(int i=0; i<size; i++) array[i] -= a; return *this; } Vector & Vector::operator*=(const Vector &v) { if( v.size != size ) return *this; for(int i=0; i<size; i++) array[i] *= v.array[i]; return *this; } Vector & Vector::operator*=(int a) { for(int i=0; i<size; i++) array[i] *= a; return *this; } Vector & Vector::operator/=(int a) { for(int i=0; i<size; i++) array[i] /= a; return *this; } Vector & Vector::operator%=(int a) { for(int i=0; i<size; i++) array[i] %= a; return *this; } int &Vector::operator[](int pos) const { return array[pos]; } Vector & Vector::operator<<=(int a) { int *array_new = new (nothrow) int [size+1]; if(array_new==NULL) { cerr << "Memory allocation failure in operator<< !" << endl; exit(-1); } for(int i=0; i<size; i++) array_new[i] = array[i]; array_new[size] = a; size++; delete [] array; array = array_new; } Vector & Vector::operator>>=(int a) { int pos = find(a); if(pos<0) return *this; int *array_new = new (nothrow) int [size-1]; if(array_new==NULL) { cerr << "Memory allocation failure in operator<< !" << endl; exit(-1); } for(int i=0; i<size; i++) { if(i<pos) array_new[i] = array[i]; if(i==pos) continue; if(i>pos) array_new[i-1] = array[i]; } size--; delete [] array; array = array_new; } 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; string msg = "Vector v: "; v.print(msg); Vector f , g; f = g = v; f.print(msg="Vector f: "); g.print(msg="Vector g: "); g = f += v; g.print(msg="g = f += v: "); f -= v; f.print(msg="f -= v: "); f *= v; f.print(msg="f *= v: "); f = v; f += 2; f.print(msg="f += 2: "); f -= 2; f.print(msg="f -= 2: "); f *= 2; f.print(msg="f *= 2: "); f /= 2; f.print(msg="f /= 2: "); f %= 2; f.print(msg="f %= 2: "); f = v; int k = f[2]; cout << "k : " << k << endl; f[2] = 100; f.print(msg="f[2] = 100: "); }
cpp/vector_overloading_binary_operators2.1494578437.txt.gz · Last modified: 2017/05/12 07:40 (external edit)