cpp:vector_overloading_binary_operators2
This is an old revision of the document!
Υπερφόρτωση δυαδικών τελεστών που μεταβάλλουν τον αριστερό τελεστέο
Παρακάτω θα δούμε την υπερφόρτωση τελεστών που μεταβάλλουν τον αριστερό τελεστέο. Οι τελεστές αυτής της κατηγορίας μπορούν να υλοποιηθούν μόνο από μέλη της κλάσης και όχι από φιλικές συναρτήσεις. Οι τελεστές περιγράφονται στον παρακάτω πίνακα.
Τελεστής | Αριστερός τελεστέος | Δεξιός τελεστέος | Περιγραφή |
---|---|---|---|
+= | Vector | Vector | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου |
-= | Vector | Vector | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου |
+= | Vector | int | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος |
-= | Vector | int | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος |
*= | Vector | Vector | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. |
*= | Vector | int | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. |
/= | Vector | int | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. |
<<= | Vector | int | Στον αριστερό τελεστέο προστίθεται ο ακέραιος (δεξιός τελεστέος), αυξάνοντας τη χωρητικότητα του κατά 1. |
>>= | Vector | int | Από τον αριστερό τελεστέο αφαιρείται ο ακέραιος (δεξιός τελεστέος), μειώνοντας τη χωρητικότητα του κατά 1, εφόσον αυτός υπάρχει. Εάν δεν υπάρχει ο αριστερός τελεστέος παραμένει αμετάβλητος. |
- 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 */ void 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 */ void operator+=(const Vector &v); void operator+=(int a); void operator-=(const Vector &v); void operator-=(int a); void operator*=(const Vector &v); void operator*=(int a); void operator/=(int a); void operator%=(int a); void operator<<=(int a); void 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 << ", "; } } void 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); } 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; } void Vector::operator+=(const Vector &v) { if(v.length() > size) { Vector n(v); n = n + *this; *this = n; } else { *this = *this + v; } } void Vector::operator+=(int a) { for(int i=0; i<size; i++) array[i] += a; } void Vector::operator-=(const Vector &v) { if(v.length() > size) { Vector n(v); n = n - *this; *this = n; } else { *this = *this - v; } } void Vector::operator-=(int a) { for(int i=0; i<size; i++) array[i] -= a; } void Vector::operator*=(const Vector &v) { if( v.size != size ) return; for(int i=0; i<size; i++) array[i] *= v.array[i]; } void Vector::operator*=(int a) { for(int i=0; i<size; i++) array[i] *= a; } void Vector::operator/=(int a) { for(int i=0; i<size; i++) array[i] /= a; } void Vector::operator%=(int a) { for(int i=0; i<size; i++) array[i] %= a; } int &Vector::operator[](int pos) const { return array[pos]; } void 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; } void Vector::operator>>=(int a) { int pos = find(a); if(pos<0) return; 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 = v; v.print(msg="Vector f: "); f += v; f.print(msg="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.1493995780.txt.gz · Last modified: 2017/05/05 13:49 (external edit)