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 [2019/05/16 08:33] 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//  |  //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.hpp>
 #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 39: 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 */
-  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) {+#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 72: 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 98: 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 138: Line 147:
 } }
  
-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 161:
 } }
  
-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 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 188: Line 197:
 } }
  
-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 209:
 } }
  
-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 215:
 } }
  
-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 227:
 } }
  
-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 233:
 } }
  
-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 242:
 } }
  
-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 248:
 } }
  
-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 254:
 } }
  
-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 260:
 } }
  
-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;+
 } }
 +</code>
  
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
  
 int main() { int main() {
Line 325: 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.1557995601.txt.gz · Last modified: 2019/05/16 07:33 (external edit)