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 [2019/05/16 08:33]
gthanos
cpp:vector_overloading_binary_operators2 [2021/05/07 14:37]
gthanos
Line 1: Line 1:
 ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ===== ===== Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης =====
  
-Παρακάτω θα δούμε την υπερφόρτωση τελεστών που μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης. Οι τελεστές περιγράφονται στον παρακάτω πίνακα.+Σε αυτή την ενότητα, θα δούμε παραδείγματα υπερφόρτωσης, τα οποία μεταβάλλουν τον αριστερό τελεστέο και μπορούν να υλοποιηθού __μόνο ως μέλη της κλάσης__.
    
 ^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  ^  Περιγραφή (//>σε όλες τις συναρτήσεις υπερφόρτωσης επιστρέφεται μία αναφορά στον αριστερό τελεστέο// ^ ^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  ^  Περιγραφή (//>σε όλες τις συναρτήσεις υπερφόρτωσης επιστρέφεται μία αναφορά στον αριστερό τελεστέο// ^
-|  %%=%%  |  //Vector//  |  //Vector//  | Ο αριστερός τελεστέος ισούται με τον δεξιό τελεστέο. |+|  %%=%%  |  //Vector//  |  //Vector//  | Ο αριστερός τελεστέος γίνεται ίσος με τον δεξιό τελεστέο. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. |
 |  %%[ ]%%  |  //Vector//  |  //int//  | Επιστρέφει μία αναφορά στο **i**-στο στοιχείο του //Vector//, **i** η θέση που προσδιορίζεται από τον δεξιό τελεστή. | |  %%[ ]%%  |  //Vector//  |  //int//  | Επιστρέφει μία αναφορά στο **i**-στο στοιχείο του //Vector//, **i** η θέση που προσδιορίζεται από τον δεξιό τελεστή. |
-|  %%+=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου  |+|  %%+=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.  |
 |  %%+=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος | |  %%+=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος |
-|  %%-=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου +|  %%-=%%  |  //Vector//  |  //Vector//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.  | 
-|  %%-=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος | +|  %%-=%%  |  //Vector//  |  //int//  | Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. 
-|  %%*=%%  |  //Vector//  |  //Vector//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. | +|  %%*=%%  |  //Vector//  |  //Vector//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%*=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστεροού τελεστέου. | +|  %%*=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.| 
-|  %%/=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. | +|  %%/=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %% %=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το υπόλοιπο της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. | +|  %% %=%%  |  //Vector//  |  //int//  | Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το υπόλοιπο της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%<<=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν αριστερά κατά το μέγεθος του δεξιού τελεστέου. | +|  %%<<=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν αριστερά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
-|  %%>>=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν δεξιά κατά το μέγεθος του δεξιού τελεστέου. |+|  %%>>=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν δεξιά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.|
  
 <code cpp Vector.cpp> <code cpp Vector.cpp>
Line 25: 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 39: 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 */
-  Vector &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 */
-  Vector & operator+=(const Vector &v); +  Vector& operator=(const Vector& v); 
-  Vector & operator+=(int a); +  Vector& operator+=(const Vector& v); 
-  Vector & operator-=(const Vector &v); +  Vector& operator+=(int a); 
-  Vector & operator-=(int a); +  Vector& operator-=(const Vector& v); 
-  Vector & operator*=(const Vector &v); +  Vector& operator-=(int a); 
-  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); +  Vector& operator%=(int a); 
-  Vector & 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 72: 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 98: 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 138: Line 138:
 } }
  
-Vector &Vector::operator=(const Vector &v) {+Vector& Vector::operator=(const Vector& v) {
   if(array!=NULL)   if(array!=NULL)
     delete [] array;     delete [] array;
Line 152: Line 152:
 } }
  
-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 170: 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 188: Line 188:
 } }
  
-Vector & 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 200: Line 200:
 } }
  
-Vector & 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;
Line 206: Line 206:
 } }
  
-Vector & 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 218: Line 218:
 } }
  
-Vector & 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;
Line 224: Line 224:
 } }
  
-Vector & Vector::operator*=(const Vector &v) {+Vector& Vector::operator*=(const Vector& v) {
   if( v.size != size )   if( v.size != size )
     return *this;     return *this;
Line 233: Line 233:
 } }
  
-Vector & 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;
Line 239: Line 239:
 } }
  
-Vector & 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;
Line 245: Line 245:
 } }
  
-Vector & 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;
Line 251: Line 251:
 } }
  
-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;
 } }
  
-Vector & 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;+
 } }
  
-Vector & Vector::operator>>=(int a) { +int &Vector::operator[](int posconst 
-  int pos = find(a); +  return array[pos];
-  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() { int main() {
Line 325: 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)