cpp:vector_overloading

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revision Both sides next revision
cpp:vector_overloading [2017/05/04 13:38]
gthanos [Μοναδιαίοι τελεστές (unary operators)]
cpp:vector_overloading [2021/05/24 07:29]
gthanos
Line 2: Line 2:
  
 Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση //Vector// η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους. Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση //Vector// η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους.
-<code cpp Vector.cpp>+<code cpp Vector.hpp>
 #include <iostream> #include <iostream>
 #include <cstdlib> #include <cstdlib>
Line 9: Line 9:
 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 23: Line 23:
   void print() const;        // print vector values to standard output   void print() const;        // print vector values to standard output
 }; };
 +</code>
  
-Vector::Vector(unsigned int length) {+<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 61: Line 64:
 } }
  
-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 89: Line 92:
   }   }
 } }
 +</code>
  
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
 int main() { int main() {
   Vector v(5);   Vector v(5);
Line 99: Line 105:
  
 Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής: Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής:
- +  [[cpp:vector_overloading_unary_operatorsΥπερφόρτωση μοναδιαίων τελεστών ]] 
-===== Μοναδιαίοι τελεστές (unary operators) ===== +  * [[cpp:vector_overloading_binary_operatorsΥπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ]] 
- +  * [[cpp:vector_overloading_binary_operators2| Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ]]
-^  Τελεστής  ^  Θέση (πριν ή μετά το αντικείμενο)  ^  Περιγραφή  ^  Μεταβάλει το αντικείμενο<color red>*</color>  | +
-|  %%+%%  |  Πριν  | Επιστρέφει το άθροισμα των στοιχείων του αντικειμένου. |  Όχι +
-|  %%-%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντεστραμμένο πρόσημο σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι +
-|  %%++%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αυξηθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει και αυτό τις τιμές των στοιχείων του κατά ένα.  |  Ναι +
-|  %%--%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν μειωθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει και αυτό τις τιμές των στοιχείων του κατά ένα.  |  Ναι  | +
-|  %%++%%   Μετά  | Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει τις τιμές των στοιχείων του κατά ένα.  |  Ναι  | +
-|  %%--%%  |  Μετά  | Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει τις τιμές των στοιχείων του κατά ένα.  |  Ναι +
-|  %%*%%  |  Πριν  | Επιστρέφει έναν ακέραιο που αποτελεί το γινόμενο των στοιχείων του πίνακα. |  Όχι +
-|  %%!%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντίστροφη σειρά σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι +
-|  %%~%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου αποτελούν το //binary NOT// των στοιχείων του αντικειμένου στο οποίο εφαρμόζεται.  |  Όχι +
- +
-<color red>*</color> **στο οποίο εφαρμόζεται**. +
- +
-<WRAP center round tip 80%> +
-Προκειμένου να μπορέσουμε να παρακολουθήσουμε την υπερφόρτωση των τελεστών υπερφορτώνουμε τον τελεστή %%=%%. Η επεξήγηση της υπερφόρτωσης του τελεστή = θα δοθεί στη συνέχεια. +
-</WRAP> +
- +
-<WRAP center round tip 80%> +
-Προσέξτε τη διαφορετική συμπεριφορά ανάμεσα στους μοναδιαίους τελεστές αύξησης και μείωσης κατά ένα (%%++, --%%) όταν αυτοί εφαρμόζονται πριν και μετά το αντικείμενο. +
-</WRAP> +
- +
-<code cpp 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: "); +
-+
-</code> +
- +
-Από τα παραπάνω παρατηρούμε τα εξής+
- +
-  * Οι συναρτήσεις που δεν μεταβάλλουν το αντικείμενο στο οποίο εφαρμόζονται δηλώνονται ως **//const//**. +
-  * Το //prototype// της συνάρτησης υπερφόρτωσης στην περίπτωση του prefix και postfix increment/decrement τελεστή είναι διαφορετική.  +
-<code cpp> +
-Στην περίπτωση δήλωσης του τελεστή πριν το αντικείμενο η δήλωση είναι  +
-Vector operator++(); +
-Vector operator--(); +
-Στην περίπτωση δήλωσης του τελεστή μετά το αντικείμενο η δήλωση είναι  +
-Vector operator++(int a); +
-Vector operator--(int a); +
-</code> +
-  * Σε όλες τις περιπτώσεις επιστρέφεται ένα νέο αντικείμενο ανεξάρτητα εάν μεταβλήθηκε το αντικείμενο πάνω στο οποίο εφαρμόζεται ο τελεστής ή όχι. +
  
  
cpp/vector_overloading.txt · Last modified: 2021/05/24 06:35 (external edit)