User Tools

Site Tools


cpp:vector_overloading_binary_operators2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:vector_overloading_binary_operators2 [2017/05/05 15:03]
gthanos
cpp:vector_overloading_binary_operators2 [2019/05/20 08:30]
gthanos
Line 1: Line 1:
 ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ===== ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης =====
  
-Παρακάτω θα δούμε την υπερφόρτωση τελεστών που μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης. Οι τελεστές περιγράφονται στον παρακάτω πίνακα+Σε αυτή την ενότητα, θα δούμε παραδείγματα υπερφόρτωσης, τα οποία μεταβάλλουν τον αριστερό τελεστέο και μπορούν να υλοποιηθού __μόνο ως μέλη της κλάσης__
- +  
-^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  ^ Περιγραφή +^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  Περιγραφή (//>σε όλες τις συναρτήσεις υπερφόρτωσης επιστρέφεται μία αναφορά στον αριστερό τελεστέο// ^ 
-|  %%+=%%  |  //Vector//  |  //Vector// Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου  +|  %%=%%  |  //Vector//  |  //Vector// Ο αριστερός τελεστέος γίνεται ίσος με τον δεξιό τελεστέο. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%-=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου  |+|  %%[ ]%%  |  //Vector//  |  //int//  | Επιστρέφει μία αναφορά στο **i**-στο στοιχείο του //Vector//, **i** η θέση που προσδιορίζεται από τον δεξιό τελεστή. 
 +|  %%+=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.  |
 |  %%+=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος | |  %%+=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος |
-|  %%-=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος | +|  %%-=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. 
-|  %%*=%%  |  //Vector//  |  //Vector//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. | +|  %%-=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. 
-|  %%*=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. | +|  %%*=%%  |  //Vector//  |  //Vector//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%/=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. | +|  %%*=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.| 
-|  %%<<=%%  |  //Vector//  |  //int// Στον αριστερό τελεστέο προστίθεται ο ακέραιος (δεξιός τελεστέος), αυξάνοντας τη χωρητικότητα του κατά 1. | +|  %%/=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%>>=%%  |  //Vector//  |  //int// Από τον αριστερό τελεστέο αφαιρείται ο ακέραιος (δεξιός τελεστέος), μειώνοντας τη χωρητικότητα του κατά 1, εφόσον αυτός υπάρχει. Εάν δεν υπάρχει ο αριστερός τελεστέος παραμένει αμετάβλητος. |+|  %% %=%%  |  //Vector//  |  //int// Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το υπόλοιπο της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
 +|  %%<<=%%  |  //Vector//  |  //int// Τα στοιχεία του αριστερού τελεστέου ολισθένουν αριστερά κατά το μέγεθος του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
 +|  %%>>=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν δεξιά κατά το μέγεθος του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.|
  
 <code cpp Vector.cpp> <code cpp Vector.cpp>
Line 22: Line 25:
 class Vector { class Vector {
   int *array;   int *array;
-  unsigned int size;+  int size;
      
 public: public:
-  Vector(unsigned int length=0); +  Vector(int length=0); 
-  Vector(const Vector &v);+  Vector(const Vector& v);
   Vector(const Vector *v);   Vector(const Vector *v);
   ~Vector();   ~Vector();
-  unsigned int length() const;           // return Vector's length. +  int length() const;           // return Vector's length. 
-  int &valueAt(unsigned int pos) const;  // return a reference to element at position pos+  int &valueAt(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    int find(int a) const;      // check if a exists in Vector. Return it position >0 or -1 
                               // if not element not found                               // if not element not found
Line 36: Line 39:
   void print() const;           // print vector values to standard output   void print() const;           // print vector values to standard output
   void print(string &msg) 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 */   /* 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-(const Vector& v) const; 
-  Vector operator-(const Vector &v) const; +  Vector operator*(Vector& v) const;
-  Vector operator*(Vector &v) const;+
      
   /* binary operators that modify left operand */   /* binary operators that modify left operand */
-  void operator+=(const Vector &v); +  Vector& operator=(const Vector& v); 
-  void operator+=(int a); +  Vector& operator+=(const Vector& v); 
-  void operator-=(const Vector &v); +  Vector& operator+=(int a); 
-  void operator-=(int a); +  Vector& operator-=(const Vector& v); 
-  void operator*=(const Vector &v); +  Vector& operator-=(int a); 
-  void operator*=(int a); +  Vector& operator*=(const Vector& v); 
-  void operator/=(int a); +  Vector& operator*=(int a); 
-  void operator%=(int a); +  Vector& operator/=(int a); 
-  void operator<<=(int a); +  Vector& operator%=(int a); 
-  void operator>>=(int a);+  Vector& operator<<=(int a); 
 +  Vector& operator>>=(int a);
   int &operator[](int a) const;   int &operator[](int a) const;
      
 }; };
  
-Vector::Vector(unsigned int length) {+Vector::Vector(int length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 69: Line 72:
 } }
  
-Vector::Vector(const Vector &v) {+Vector::Vector(const Vector& v) {
   size = v.length();   size = v.length();
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 95: Line 98:
 } }
  
-unsigned int Vector::length() const { +int Vector::length() const { 
   return size;    return size; 
 } }
  
-int &Vector::valueAt(unsigned int pos) const {+int &Vector::valueAt(int pos) const {
   if(pos>=length()) {   if(pos>=length()) {
      cerr << "Invalid access position!\n";      cerr << "Invalid access position!\n";
Line 135: Line 138:
 } }
  
-void Vector::operator=(const Vector &v) {+Vector& Vector::operator=(const Vector& v) {
   if(array!=NULL)   if(array!=NULL)
     delete [] array;     delete [] array;
Line 145: Line 148:
   }   }
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
-    array[i] = v.valueAt(i);  +    array[i] = v.valueAt(i); 
 +  return *this;
 } }
  
-Vector Vector::operator+(const Vector &v) const {+Vector Vector::operator+(const Vector& v) const {
   int length;   int length;
   if (size > v.length())   if (size > v.length())
Line 166: Line 170:
 } }
  
-Vector Vector::operator-(const Vector &v) const {+Vector Vector::operator-(const Vector& v) const {
   int length;   int length;
   if (size > v.length())   if (size > v.length())
Line 184: Line 188:
 } }
  
-void Vector::operator+=(const Vector &v) {+Vector& Vector::operator+=(const Vector& v) {
   if(v.length() > size) {   if(v.length() > size) {
     Vector n(v);     Vector n(v);
Line 193: Line 197:
     *this = *this + v;     *this = *this + v;
   }   }
 +  return *this;
 } }
  
-void Vector::operator+=(int a) {+Vector& Vector::operator+=(int a) {
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] += a;     array[i] += a;
 +  return *this;
 } }
  
-void Vector::operator-=(const Vector &v) {+Vector& Vector::operator-=(const Vector& v) {
   if(v.length() > size) {   if(v.length() > size) {
     Vector n(v);     Vector n(v);
Line 209: Line 215:
     *this = *this - v;     *this = *this - v;
   }   }
 +  return *this;
 } }
  
-void Vector::operator-=(int a) {+Vector& Vector::operator-=(int a) {
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] -= a;     array[i] -= a;
 +  return *this;
 } }
  
-void Vector::operator*=(const Vector &v) {+Vector& Vector::operator*=(const Vector& v) {
   if( v.size != size )   if( v.size != size )
-    return;+    return *this;
      
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] *= v.array[i];       array[i] *= v.array[i];  
 +  return *this;
 } }
  
-void Vector::operator*=(int a) {+Vector& Vector::operator*=(int a) {
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] *= a;     array[i] *= a;
 +  return *this;
 } }
  
-void Vector::operator/=(int a) {+Vector& Vector::operator/=(int a) {
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] /= a;     array[i] /= a;
 +  return *this;
 } }
  
-void Vector::operator%=(int a) {+Vector& Vector::operator%=(int a) {
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
     array[i] %= a;     array[i] %= a;
 +  return *this;
 } }
  
-int &Vector::operator[](int posconst +Vector& Vector::operator<<=(int a) { 
-  return array[pos];+  for(int i=0; i<size; i++) 
 +    array[i<<= a; 
 +  return *this;
 } }
  
-void Vector::operator<<=(int a) { +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++)   for(int i=0; i<size; i++)
-    array_new[i] = array[i]+    array[i] >>= a; 
-  array_new[size] = a; +  return *this;
-  size++; +
-  delete [] array; +
-  array = array_new;+
 } }
  
-void Vector::operator>>=(int a) { +int &Vector::operator[](int posconst 
-  int pos = find(a); +  return array[pos];
-  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() { int main() {
Line 288: Line 275:
   v.print(msg);   v.print(msg);
      
-  Vector f = v; +  Vector f , g; 
-  v.print(msg="Vector f:  ");+  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 -= v;
   f.print(msg="f -= v:  ");   f.print(msg="f -= v:  ");
Line 310: Line 300:
   f.print(msg="f %= 2:  ");   f.print(msg="f %= 2:  ");
      
-  f = v; +  cout << "v[2]:  " << v[2] << endl; 
-  int k = f[2]; +  v[2] = 100; 
-  cout << ":  " << << endl; +  v.print(msg="[v[2] = 100] v:  ");  
-  f[2] = 100; +
-  f.print(msg="f[2] = 100:  ");  +
 } }
 </code> </code>
  
  
cpp/vector_overloading_binary_operators2.txt · Last modified: 2021/05/24 06:45 (external edit)