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 revisionPrevious revision
Next revision
Previous revision
cpp:vector_overloading_binary_operators2 [2017/05/05 14:49] gthanoscpp:vector_overloading_binary_operators2 [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
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// Τα στοιχεία του αριστερού τελεστέου ολισθένουν αριστερά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο. | 
 +|  %%>>=%%  |  //Vector//  |  //int//  | Τα στοιχεία του αριστερού τελεστέου ολισθένουν δεξιά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.|
  
- +<code cpp Vector.hpp>
-<code cpp Vector.cpp>+
 #include <iostream> #include <iostream>
 #include <string> #include <string>
 #include <cstdlib> #include <cstdlib>
 using namespace std; using namespace std;
 +
 +#ifndef _VECTOR_HPP_
 +#define _VECTOR_HPP_
  
 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 37: Line 42:
   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) {+#endif 
 +</code> 
 + 
 +<code cpp Vector.cpp> 
 +#include "Vector.hpp" 
 + 
 +Vector::Vector(int length) {
   size = length;   size = length;
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 70: Line 81:
 } }
  
-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 96: Line 107:
 } }
  
-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 136: Line 147:
 } }
  
-void Vector::operator=(const Vector &v) {+Vector& Vector::operator=(const Vector& v) {
   if(array!=NULL)   if(array!=NULL)
     delete [] array;     delete [] array;
Line 146: Line 157:
   }   }
   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 167: Line 179:
 } }
  
-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 185: Line 197:
 } }
  
-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 194: Line 206:
     *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 210: Line 224:
     *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;+
 } }
 +</code>
  
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
  
 int main() { int main() {
Line 289: Line 288:
   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 311: Line 313:
   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.1493995780.txt.gz · Last modified: 2017/05/05 13:49 (external edit)