User Tools

Site Tools


cpp:vector_overloading_binary_operators2

Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης

Σε αυτή την ενότητα, θα δούμε παραδείγματα υπερφόρτωσης, τα οποία μεταβάλλουν τον αριστερό τελεστέο και μπορούν να υλοποιηθού μόνο ως μέλη της κλάσης.

Τελεστής Αριστερός τελεστέος Δεξιός τελεστέος Περιγραφή (>σε όλες τις συναρτήσεις υπερφόρτωσης επιστρέφεται μία αναφορά στον αριστερό τελεστέο)
= Vector Vector Ο αριστερός τελεστέος γίνεται ίσος με τον δεξιό τελεστέο. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
[ ] Vector int Επιστρέφει μία αναφορά στο i-στο στοιχείο του Vector, i η θέση που προσδιορίζεται από τον δεξιό τελεστή.
+= Vector Vector Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
+= Vector int Σε κάθε στοιχείο του αριστερού τελεστέου προστίθεται ο δεξιός τελεστέος
-= Vector Vector Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται το αντίστοιχο στοιχείο του δεξιού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
-= Vector int Σε κάθε στοιχείο του αριστερού τελεστέου αφαιρείται ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
*= Vector Vector Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με το αντίστοιχο στοιχείο του δεξιού τελεστέου και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
*= Vector int Κάθε στοιχείο του αριστερού τελεστέου πολλαπλασιάζεται με τον δεξιό τελεστέο και το αποτέλεσμα αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
/= Vector int Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το αποτέλεσμα της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
%= Vector int Κάθε στοιχείο του αριστερού τελεστέου διαιρείται με τον δεξιό τελεστέο και το υπόλοιπο της ακέραιας διαίρεσης αποθηκεύεται στο στοιχείο του αριστερού τελεστέου. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
<<= Vector int Τα στοιχεία του αριστερού τελεστέου ολισθένουν αριστερά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
>>= Vector int Τα στοιχεία του αριστερού τελεστέου ολισθένουν δεξιά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. Επιστρέφεται μία αναφορά στο τρέχον αντικείμενο.
Vector.hpp
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
#ifndef _VECTOR_HPP_
#define _VECTOR_HPP_
 
class Vector {
  int *array;
  int size;
 
public:
  Vector(int length=0);
  Vector(const Vector& v);
  Vector(const Vector *v);
  ~Vector();
  int length() const;           // return Vector's length.
  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 
                              // 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 */
  Vector operator+(const Vector& v) const;
  Vector operator-(const Vector& v) const;
  Vector operator*(Vector& v) const;
 
  /* 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-=(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);
  int &operator[](int a) const;
 
};
 
#endif
Vector.cpp
#include "Vector.hpp"
 
Vector::Vector(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;
}
 
int Vector::length() const { 
  return size; 
}
 
int &Vector::valueAt(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 << ", ";
  }
}
 
Vector& 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);
  return *this;
}
 
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;
}
 
Vector& Vector::operator+=(const Vector& v) {
  if(v.length() > size) {
    Vector n(v);
    n = n + *this;
    *this = n;
  }
  else {
    *this = *this + v;
  }
  return *this;
}
 
Vector& Vector::operator+=(int a) {
  for(int i=0; i<size; i++)
    array[i] += a;
  return *this;
}
 
Vector& Vector::operator-=(const Vector& v) {
  if(v.length() > size) {
    Vector n(v);
    n = n - *this;
    *this = n;
  }
  else {
    *this = *this - v;
  }
  return *this;
}
 
Vector& Vector::operator-=(int a) {
  for(int i=0; i<size; i++)
    array[i] -= a;
  return *this;
}
 
Vector& Vector::operator*=(const Vector& v) {
  if( v.size != size )
    return *this;
 
  for(int i=0; i<size; i++)
    array[i] *= v.array[i];  
  return *this;
}
 
Vector& Vector::operator*=(int a) {
  for(int i=0; i<size; i++)
    array[i] *= a;
  return *this;
}
 
Vector& Vector::operator/=(int a) {
  for(int i=0; i<size; i++)
    array[i] /= a;
  return *this;
}
 
Vector& Vector::operator%=(int a) {
  for(int i=0; i<size; i++)
    array[i] %= a;
  return *this;
}
 
Vector& Vector::operator<<=(int a) {
  for(int i=0; i<size; i++)
    array[i] <<= a;
  return *this;
}
 
Vector& Vector::operator>>=(int a) {
  for(int i=0; i<size; i++)
    array[i] >>= a;
  return *this;
}
 
int &Vector::operator[](int pos) const {
  return array[pos];
}
VectorUsage.cpp
#include "Vector.hpp"
 
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 , g;
  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.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:  ");
 
  cout << "v[2]:  " << v[2] << endl;
  v[2] = 100;
  v.print(msg="[v[2] = 100] v:  ");  
}
cpp/vector_overloading_binary_operators2.txt · Last modified: 2021/05/24 06:45 (external edit)