User Tools

Site Tools


cpp:vector_overloading_binary_operators

This is an old revision of the document!


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

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

Τελεστής Αριστερός τελεστέος Δεξιός τελεστέος Περιγραφή Φιλική μέθοδος
+ 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 στο οποίο έχει προστεθεί ο δεξιός τελεστέος. Το νέο αντικείμενο έχει μέγεθος κατά ένα μεγαλύτερο του αριστερού τελεστέου. και τα δύο (η φιλική συνάρτηση σε σχόλια)
>> Vector int Επιστρέφει ένα νέο αντικείμενο τύπου Vector στο οποίο έχει διαγραφεί ο δεξιός τελεστέος, εφόσον αυτός υπάρχει. Το νέο αντικείμενο έχει μέγεθος κατά ένα μικρότερο του αριστερού τελεστέου, εάν βρεθεί ο δεξιός τελεστέος μέσα στο Vector. Εάν δεν βρεθεί επιστρέφεται ένα αντικείμενο που περιέχει τα στοιχεία του αριστερού τελεστέου. και τα δύο (η φιλική συνάρτηση σε σχόλια)
== int Vector Επιστρέφει true εάν ο αριστερός τελεστέος είναι ίσος με το δεξιό τελεστέο, διαφορετικά false Όχι
== int Vector Επιστρέφει true εάν ο αριστερός τελεστέος δεν είναι ίσος με το δεξιό τελεστέο, διαφορετικά false Όχι

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

Vector.cpp
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
 
class Vector {
  int *array;
  unsigned int size;
 
public:
  Vector(unsigned int length=0);
  Vector(const Vector &v);
  Vector(const Vector *v);
  ~Vector();
  unsigned int length() const;           // return Vector's length.
  int &valueAt(unsigned 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 */
  void operator=(const Vector &v);
  Vector operator+(const Vector &v) const;
  //friend Vector operator+(const Vector &v1, const Vector v2);
  Vector operator-(const Vector &v) const;
  Vector operator*(Vector &v) const;
  Vector operator+(int a) const;
  friend Vector operator+(int a, const Vector &v);
  Vector operator-(int a) const;
  Vector operator*(int a) const;
  friend Vector operator*(int a, const Vector &v);
  Vector operator/(int a) const;
  Vector operator%(int a) const;
 
  bool operator==(const Vector &v) const;
  bool operator!=(const Vector &v) const;
 
  Vector operator << (int a) const;
  //friend Vector operator<<(const Vector &v, int a);
  Vector operator >> (int a) const;
  //friend Vector operator>>(const Vector &v, int a);
 
};
 
Vector::Vector(unsigned 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;
}
 
unsigned int Vector::length() const { 
  return size; 
}
 
int &Vector::valueAt(unsigned 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 << ", ";
  }
}
 
void 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);  
}
 
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*(Vector &v) const {
  Vector n(size);
  for(int i=0; i<size; i++)    
      n.array[i] = array[i] * v.array[i];
  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;
}
 
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);
  int *array_new = new (nothrow) int [n.size+1];
  if(array_new==NULL) {
    cerr << "Memory allocation failure in operator<< !" << endl;
    exit(-1);
  }
  for(int i=0; i<n.size; i++)
    array_new[i] = n.array[i];
  array_new[n.size] = a;
  n.size++;
  delete [] n.array;
  n.array = array_new;
  return n;
}*/
 
Vector Vector::operator << (int a) const {
  Vector n(*this);
  int *array_new = new (nothrow) int [n.size+1];
  if(array_new==NULL) {
    cerr << "Memory allocation failure in operator<< !" << endl;
    exit(-1);
  }
  for(int i=0; i<n.size; i++)
    array_new[i] = n.array[i];
  array_new[n.size] = a;
  n.size++;
  delete [] n.array;
  n.array = array_new;
  return n;
}
 
/*Vector operator>>(const Vector &v, int a) {
  int pos = v.find(a);
  if(pos<0)
    return v;
  Vector n(v);
  int *array_new = new (nothrow) int [n.size-1];
  if(array_new==NULL) {
    cerr << "Memory allocation failure in operator<< !" << endl;
    exit(-1);
  }
  for(int i=0; i<n.size; i++) {
    if(i<pos)
      array_new[i] = n.array[i];
    if(i==pos)
      continue;
    if(i>pos)
      array_new[i-1] = n.array[i];
  }  
  n.size--;
  delete [] n.array;
  n.array = array_new;
  return n;
}*/
 
Vector Vector::operator >> (int a) const {
  int pos = find(a);
  if(pos<0)
    return *this;
  Vector n(*this);
  int *array_new = new (nothrow) int [n.size-1];
  if(array_new==NULL) {
    cerr << "Memory allocation failure in operator<< !" << endl;
    exit(-1);
  }
  for(int i=0; i<n.size; i++) {
    if(i<pos)
      array_new[i] = n.array[i];
    if(i==pos)
      continue;
    if(i>pos)
      array_new[i-1] = n.array[i];
  }  
  n.size--;
  delete [] n.array;
  n.array = array_new;
  return n;
}
 
 
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="f + v:  ");
  k = f - v;
  k.print(msg="f - v:  ");
  k = f * v;
  k.print(msg="f * v:  ");
  k = f + 2;
  k.print(msg="f + 2:  ");
  k = f - 2;
  k.print(msg="f - 2:  ");
  k = f * 2;
  k.print(msg="f * 2:  ");
  k = f / 2;
  k.print(msg="f / 2:  ");
  k = f % 2;
  k.print(msg="f % 2:  ");
  f = f << 7;
  f.print(msg="f = f << 7:  ");
  f = f << 7;
  f.print(msg="f = f << 7:  ");
  f = f >> 6;
  f.print(msg="f = f >> 6:  ");
  f = f >> 6;
  f.print(msg="f = f >> 6:  ");
  if( f == v )
    cout << "f == v " << endl;
  else
    cout << "f != v " << endl;
}
cpp/vector_overloading_binary_operators.1493996629.txt.gz · Last modified: 2017/05/05 14:03 (external edit)