User Tools

Site Tools


cpp:vector_overloading_binary_operators

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

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

Τελεστής Αριστερός τελεστέος Δεξιός τελεστέος Περιγραφή Φιλική μέθοδος
+ Vector Vector Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το άθροισμα των στοιχείων των δύο τελεστέων τύπου Vector στην αντίστοιχη θέση και τα δύο (η φιλική συνάρτηση σε σχόλια)
- Vector Vector Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από τη διαφορά των στοιχείων των δύο τελεστεών τύπου Vector στην αντίστοιχη θέση Όχι
* Vector Vector Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το γινόμενο των στοιχείων των δύο τελεστεών τύπου Vector στην αντίστοιχη θέση Όχι
+ Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το άθροισμα του αντίστοιχου στοιχείου του Vector με τον ακέραιο Ναι
+ int Vector Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το άθροισμα του αντίστοιχου στοιχείου του Vector με τον ακέραιο Ναι
- Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από τη διαφορά του αντίστοιχου στοιχείου του Vector με τον ακέραιο Όχι
* Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το γινόμενο του αντίστοιχου στοιχείου του Vector με τον ακέραιο Όχι
* int Vector Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το γινόμενο του αντίστοιχου στοιχείου του Vector με τον ακέραιο Ναι
/ Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από την ακέραια διαίρεση του αντίστοιχου στοιχείου του Vector με τον ακέραιο Όχι
% Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector που κάθε στοιχείο του προκύπτει από το υπόλοιπο της ακέραιας διαίρεσης του αντίστοιχου στοιχείου του Vector με τον ακέραιο Όχι
<< Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector, το οποίο προκύπτει από τον αριστερό τελεστέο εάν ολισθήσουμε όλα τα στοιχεία του αριστερά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. και τα δύο (η φιλική συνάρτηση σε σχόλια)
>> Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector, το οποίο προκύπτει από τον αριστερό τελεστέο εάν ολισθήσουμε όλα τα στοιχεία του δεξιά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. και τα δύο (η φιλική συνάρτηση σε σχόλια)
== int Vector Επιστρέφει true εάν για κάθε (0⇐i⇐size) το στοιχείο του αριστερού τελεστέου στην θέση i, είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά false Όχι
!= int Vector Επιστρέφει true εάν για κάθε (0⇐i⇐size) το στοιχείο του αριστερού τελεστέου στην θέση i, ΔΕΝ είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά false Όχι

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

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);                // assignment operator
  Vector operator+(const Vector& v) const;           // Adds v to *this and returns the sum in a new vector.
  //friend Vector operator+(const Vector& v1, const Vector& v2);  // same as above
  Vector operator-(const Vector& v) const;           // Subtracts v from *this and returns the subtraction in a new vector.
  Vector operator*(const Vector& v) const;           // Returns a new vector that is the product of v and *this.
  friend Vector operator+(const Vector& v, int a);   // Returns a new vector. Each element of vector equals the sum of v[i] + a
  friend Vector operator+(int a, const Vector& v);   // Returns a new vector. Each element of vector equals the sum of v[i] + a
  Vector operator-(int a) const;                     // Returns a new vector. Each element of vector equals the subtraction:  v[i] - a
  Vector operator*(int a) const;                     // Returns a new vector. Each element of vector equals the product:  v[i] * a
  friend Vector operator*(int a, const Vector& v);   // Returns a new vector. Each element of vector equals the product:  v[i] * a
  Vector operator/(int a) const;                     // Returns a new vector. Each element of vector equals the division (int):  v[i] / a
  Vector operator%(int a) const;                     // Returns a new vector. Each element of vector equals the modulo:  v[i] % a
 
  bool operator==(const Vector& v) const;            // Returns true if each element of the left operand equals each element of right operand.
  bool operator!=(const Vector& v) const;            // Returns true if each element of the left operand is NOT equal with each element 
                                                     // of right operand.
 
  Vector operator << (int a) const;                  // Returns a new vector. Each element of vector is left shifted by a positions.
  //friend Vector operator<<(const Vector& v, int a);// Same as above
  Vector operator >> (int a) const;                  // Returns a new vector. Each element of vector is right shifted by a positions.
  //friend Vector operator>>(const Vector& v, int a);// Same as above  
};
 
#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;
  print();
}
 
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 operator+(const Vector& v1, const Vector v2) {
  int length;
  if (v1.size > v2.size)
    length = v1.length();
  else
    length = v2.length();
  Vector n(length);
  for(int i=0; i<length; i++) {
    int sum = 0;
    if(i<v1.length())
      sum += v1.array[i];
    if(i<v2.length())
      sum += v2.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) const {
  int length;
  if (size < v.size)
    length = size;
  else
    length = v.size;
  Vector n(length);
  for(int i=0; i<length; i++)
    n.array[i] = array[i] * v.array[i];
  return n;
}
 
Vector operator+(const Vector& v, int a) {
  Vector n(v.size);
  for(int i=0; i<v.size; i++)
    n.array[i] = v.array[i] + a;
  return n;
}
 
Vector operator+(int a, const Vector& v) {
  Vector n(v);
  for(int i=0; i<v.length(); i++) {
    n.array[i] += a;
  }
  return n;
}
 
Vector Vector::operator-(int a) const {
  Vector n(size);
  for(int i=0; i<size; i++)
    n.array[i] = array[i] - a;
  return n;
}
 
Vector Vector::operator*(int a) const {
  Vector n(size);
  for(int i=0; i<size; i++)
    n.array[i] = array[i] * a;
  return n;
}
 
Vector operator*(int a, const Vector& v) {
  Vector n(v);
  for(int i=0; i<v.length(); i++) {
    n.array[i] *= a;
  }
  return n;
}
 
Vector Vector::operator/(int a) const {
  Vector n(size);
  for(int i=0; i<size; i++)
    n.array[i] = array[i] / a;
  return n;
}
 
Vector Vector::operator%(int a) const {
  Vector n(size);
  for(int i=0; i<size; i++)
    n.array[i] = array[i] % a;
  return n;
}
 
bool Vector::operator==(const Vector& v) const {
  if( size != v.size )
    return false;
  for(int i=0; i<size; i++)
    if( array[i] != v.array[i] )
      return false;
  return true;
}
 
bool Vector::operator!=(const Vector& v) const {
  return !(*this == v);
}
 
/*
Vector operator<<(const Vector& v, int a){
  Vector n(v);
  for(int i=0; i<n.size; i++)
    n.array[i] <<= a;
  return n;
}*/
 
Vector Vector::operator << (int a) const {
  Vector n(*this);
  for(int i=0; i<n.size; i++)
    n.array[i] <<= a;
  return n;
}
 
/*Vector operator>>(const Vector& v, int a) {
  Vector n(v);
  for(int i=0; i<n.size; i++)
    n.array[i] >>= a;
  return n;
}*/
 
Vector Vector::operator >> (int a) const {
  Vector n(*this);
  for(int i=0; i<n.size; i++)
    n.array[i] >>= a;
  return n;
}
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 = v;
  v.print(msg="Vector f:  ");
  if( f == v )
    cout << "f == v " << endl;
  else
    cout << "f != v " << endl;
 
 
  Vector k;
  k = f + v;
  k.print(msg="k = f + v:  ");
  k = f - v;
  k.print(msg="k = f - v:  ");
  k = f * v;
  k.print(msg="k = f * v:  ");
  k = f + 2;
  k.print(msg="k = f + 2:  ");
  k = 2 + f;
  k.print(msg="k = 2 + f:  ");
  k = f - 2;
  k.print(msg="k = f - 2:  ");
  k = f * 2;
  k.print(msg="k = f * 2:  ");
  k = f / 2;
  k.print(msg="k = f / 2:  ");
  k = f % 2;
  k.print(msg="k = f % 2:  ");
  f = f << 2;
  f.print(msg="f = f << 2: ");
  f = f >> 2;
  f.print(msg="f = f >> 2: ");
  if( f == v )
    cout << "f == v " << endl;
  else
    cout << "f != v " << endl;
}
cpp/vector_overloading_binary_operators.txt · Last modified: 2021/05/24 06:42 (external edit)