User Tools

Site Tools


cpp:vector_overloading

This is an old revision of the document!


Παράδειγμα υπερφόρτωσης τελεστών

Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση Vector η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους.

Vector.cpp
#include <iostream>
#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
};
 
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 << ", ";
  }
}
 
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; 
}

Για την παραπάνω κλάση κλάση Vector θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής:

Μοναδιαίοι τελεστές (unary operators)

Τελεστής Θέση (πριν ή μετά το αντικείμενο) Περιγραφή Μεταβάλει το αντικείμενο*
+ Πριν Επιστρέφει το άθροισμα των στοιχείων του αντικειμένου. Όχι
- Πριν Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντεστραμμένο πρόσημο σε σχέση με το αντικείμενο που εφαρμόζεται. Όχι
++ Πριν Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αυξηθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει και αυτό τις τιμές των στοιχείων του κατά ένα. Ναι
-- Πριν Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν μειωθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει και αυτό τις τιμές των στοιχείων του κατά ένα. Ναι
++ Μετά Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει τις τιμές των στοιχείων του κατά ένα. Ναι
-- Μετά Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει τις τιμές των στοιχείων του κατά ένα. Ναι
* Πριν Επιστρέφει έναν ακέραιο που αποτελεί το γινόμενο των στοιχείων του πίνακα. Όχι
! Πριν Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντίστροφη σειρά σε σχέση με το αντικείμενο που εφαρμόζεται. Όχι
~ Πριν Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου αποτελούν το binary NOT των στοιχείων του αντικειμένου στο οποίο εφαρμόζεται. Όχι

* στο οποίο εφαρμόζεται.

Προκειμένου να μπορέσουμε να παρακολουθήσουμε την υπερφόρτωση των τελεστών υπερφορτώνουμε τον τελεστή =. Η επεξήγηση της υπερφόρτωσης του τελεστή = θα δοθεί στη συνέχεια.

Προσέξτε τη διαφορετική συμπεριφορά ανάμεσα στους μοναδιαίους τελεστές αύξησης και μείωσης κατά ένα (++, --) όταν αυτοί εφαρμόζονται πριν και μετά το αντικείμενο.

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
  void operator=(const Vector &v);
 
  int operator+() const;     // returns the sum of all elements
  Vector operator-() const;  // returns a new Vector with negative values;
  Vector operator++();       // prefix increment
  Vector operator--();       // prefix decrement
  int operator*() const;     // returns the product of Vector elements
  Vector operator~() const;  // returns the binary NOT for each element in a new Vector
  Vector operator!() const;  // returns the factorial of each element in a new Vector
 
  Vector operator++(int );   // postfix increment
  Vector operator--(int );   // postfix decrement
};
 
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);  
}
 
int Vector::operator+() const{
  int sum=0.0;
  for(int i=0; i<size; i++) {
    sum += array[i];
  }
  return sum;
}
 
Vector Vector::operator-() const{
  Vector v(size);
  for(int i=0; i<size; i++) {
    v.valueAt(i) = -array[i];
  }
  return v;
}
 
//prefix increment
Vector Vector::operator++() {
  for(int i=0; i<size; i++) {
    array[i]++;
  }
  Vector v(this);
  return v;
}
//prefix decrement
Vector Vector::operator--() {  
  for(int i=0; i<size; i++) {
    array[i]--;
  }
  Vector v(this);
  return v;
}
 
int Vector::operator*() const{
  int product = 1;
  for(int i=0; i<size; i++)
    product *= array[i];
  return product;
}
 
Vector Vector::operator~() const{
  Vector v(size);
  for(int i=0; i<size; i++)
    v.valueAt(i) = ~array[i];
  return v;
}
 
Vector Vector::operator!() const{
  Vector v(size);
  for(int i=0; i<size; i++)
    v.valueAt(i) = array[size-1-i];
  return v;
}
 
//postfix increment
Vector Vector::operator++(int a) {
  Vector v(this);
  for(int i=0; i<size; i++) {
    array[i]++;
  }  
  return v;
}
 
//postfix decrement
Vector Vector::operator--(int a) {
  Vector v(this);
  for(int i=0; i<size; i++) {
    array[i]--;
  }  
  return v;
}
 
 
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 = "Initial Vector: ";
 
  v.print(msg);
  int sum = +v;
  cout << "Sum of elements: " << sum << endl;
 
  Vector f = -v;
  f.print(msg="Negative values: ");
  int product = *v;
  cout << "Product of elements: " << product << endl;
 
  f = !v;
  f.print(msg = "Inverted sequence: ");
 
  f = ~v;
  f.print(msg = "Binary inverted: ");
 
  msg = "-------------\nPrefix increment";
  cout << msg << endl;
  f = ++v;
  v.print(msg = "Initial Vector: ");
  f.print(msg = "Assigned Vector: ");
 
  msg = "-------------\nPostfix decrement";
  cout << msg << endl;
  f = v--;
  v.print(msg = "Initial Vector: ");
  f.print(msg = "Assigned Vector: ");
}

Από τα παραπάνω παρατηρούμε τα εξής:

  • Οι συναρτήσεις που δεν μεταβάλλουν το αντικείμενο στο οποίο εφαρμόζονται δηλώνονται ως const.
  • Το prototype της συνάρτησης υπερφόρτωσης στην περίπτωση του prefix και postfix increment/decrement τελεστή είναι διαφορετική.
Στην περίπτωση δήλωσης του τελεστή πριν το αντικείμενο η δήλωση είναι 
Vector operator++();
Vector operator--();
Στην περίπτωση δήλωσης του τελεστή μετά το αντικείμενο η δήλωση είναι 
Vector operator++(int a);
Vector operator--(int a);
  • Σε όλες τις περιπτώσεις επιστρέφεται ένα νέο αντικείμενο ανεξάρτητα εάν μεταβλήθηκε το αντικείμενο πάνω στο οποίο εφαρμόζεται ο τελεστής ή όχι.
cpp/vector_overloading.1493905136.txt.gz · Last modified: 2017/05/04 12:38 (external edit)