User Tools

Site Tools


cpp:vector_overloading_unary_operators

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cpp:vector_overloading_unary_operators [2017/05/05 08:23] gthanoscpp:vector_overloading_unary_operators [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 ====== Υπερφόρτωση μοναδιαίων τελεστών (unary operators) ====== ====== Υπερφόρτωση μοναδιαίων τελεστών (unary operators) ======
  
-^  Τελεστής  ^  Θέση (πριν ή μετά το αντικείμενο)  ^  Περιγραφή  ^  Μεταβάλει το αντικείμενο<color red>*</color>  | +Ο παρακάτω πίνακας περιγράφει την υπερφόρτωση των τελεστών στο παράδειγμα της κλάσης //Vector// που ακολουθεί. 
-|  %%+%%  |  Πριν  | Επιστρέφει το άθροισμα των στοιχείων του αντικειμένου. |  Όχι + 
-|  %%-%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντεστραμμένο πρόσημο σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι +^  Τελεστής  ^  Θέση (πριν ή μετά το αντικείμενο)  ^  Περιγραφή  ^  Μεταβάλει το αντικείμενο<color red>*</color>  ^ Φιλική συνάρτηση ^ 
-|  %%++%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αυξηθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεταιΤο αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει και αυτό τις τιμές των στοιχείων του κατά ένα.  |  Ναι +|  %%+%%  |  Πριν  | Επιστρέφει το άθροισμα των στοιχείων του αντικειμένου. |  Όχι  | Όχι 
-|  %%--%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν μειωθεί κατά ένα σε σχέση με το αντικείμενο που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει και αυτό τις τιμές των στοιχείων του κατά ένα.  |  Ναι +|  %%-%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντεστραμμένο πρόσημο σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι  | Όχι 
-|  %%++%%  |  Μετά  | Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει αυξήσει τις τιμές των στοιχείων του κατά ένα |  Ναι +|  %%++%%  |  Πριν  | Επιστρέφει μία αναφορά στο τρέχον αντικείμενοτα στοιχεία του οποίου έχουν αυξηθεί κατά ένα. |  Ναι  | Όχι 
-|  %%--%%  |  Μετά  | Επιστρέφει ένα νέο αντικείμενο αντίγραφο του αντικειμένου που εφαρμόζεται. Το αντικείμενο στο οποίο εφαρμόζεται έχει μειώσει τις τιμές των στοιχείων του κατά ένα.  |  Ναι +|  %%--%%  |  Πριν  | Επιστρέφει μία αναφορά στο αντικείμενο που περνιέται ως 1η παράμετρος στη φιλική συνάρτηση (δηλαδή το τρέχον αντικείμενο), τα στοιχεία του οποίου έχουν μειωθεί κατά ένα.  |  Ναι  | Ναι 
-|  %%*%%  |  Πριν  | Επιστρέφει έναν ακέραιο που αποτελεί το γινόμενο των στοιχείων του πίνακα. |  Όχι +|  %%++%%  |  Μετά  | Επιστρέφει ένα νέο αντικείμενο, που είναι αντίγραφο του αντικειμένου στο οποίο εφαρμόζεται ο τελεστής, πριν την πράξη της αύξησης. Το αντικείμενο στο οποίο εφαρμόζεται ο τελεστής αυξάνει τις τιμές των στοιχείων του κατά 1. |  Ναι  | Όχι 
-|  %%!%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντίστροφη σειρά σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι +|  %%--%%  |  Μετά  | Επιστρέφει ένα νέο αντικείμενο, που είναι αντίγραφο του αντικειμένου στο οποίο εφαρμόζεται ο τελεστής, πριν την πράξη της μείωσης. Το αντικείμενο στο οποίο εφαρμόζεται ο τελεστής μειώνει τις τιμές των στοιχείων του κατά 1.  |  Ναι  | Ναι 
-|  %%~%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου αποτελούν το //binary NOT// των στοιχείων του αντικειμένου στο οποίο εφαρμόζεται.  |  Όχι  |+|  %%*%%  |  Πριν  | Επιστρέφει έναν ακέραιο που αποτελεί το γινόμενο των στοιχείων του πίνακα. |  Όχι Όχι 
 +|  %%!%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου έχουν αντίστροφη σειρά σε σχέση με το αντικείμενο που εφαρμόζεται.  |  Όχι  | Ναι 
 +|  %%~%%  |  Πριν  | Επιστρέφει ένα νέο αντικείμενο τα στοιχεία του οποίου αποτελούν το //binary NOT// των στοιχείων του αντικειμένου στο οποίο εφαρμόζεται.  |  Όχι Όχι  |
  
 <color red>*</color> **στο οποίο εφαρμόζεται**. <color red>*</color> **στο οποίο εφαρμόζεται**.
  
-<WRAP center round tip 80%> 
 Προκειμένου να μπορέσουμε να παρακολουθήσουμε την υπερφόρτωση των τελεστών υπερφορτώνουμε τον τελεστή **=**. Η επεξήγηση της υπερφόρτωσης του τελεστή **=** θα δοθεί στη συνέχεια. Προκειμένου να μπορέσουμε να παρακολουθήσουμε την υπερφόρτωση των τελεστών υπερφορτώνουμε τον τελεστή **=**. Η επεξήγηση της υπερφόρτωσης του τελεστή **=** θα δοθεί στη συνέχεια.
-</WRAP> 
  
 <WRAP center round tip 80%> <WRAP center round tip 80%>
Line 33: Line 33:
 </WRAP> </WRAP>
  
-<code cpp Vector.cpp>+ 
 +<WRAP center round tip 80%> 
 +Επίσης, παρατηρήστε ότι οι συναρτήσεις που δεν μεταβάλλουν το αντικείμενο στο οποίο εφαρμόζονται δηλώνονται ως **//const//**. Για παράδειγμα, η συνάρτηση 
 +<code cpp
 +Vector operator~() const; 
 +</code> 
 +επιστρέφει ένα νέο αντικείμενο του οποίου τα στοιχεία είναι το binary NOT του αρχικούΟ αρχικός πίνακας παραμένει αμετάβλητος. 
 +</WRAP> 
 + 
 +<code cpp Vector.hpp>
 #include <iostream> #include <iostream>
 #include <string> #include <string>
 #include <cstdlib> #include <cstdlib>
 using namespace std; using namespace std;
 +
 +#ifndef _VECTOR_HPP_
 +#define _VECTOR_HPP_
  
 class Vector { class Vector {
   int *array;   int *array;
-  unsigned int size;+  int size;
      
 public: public:
Line 48: Line 60:
   Vector(const Vector *v);   Vector(const Vector *v);
   ~Vector();   ~Vector();
-  unsigned int length() const;           // return Vector's length. +  unsigned int length() const;     // return Vector's length. 
-  int &valueAt(unsigned int pos) const;  // return a reference to element at position pos +  int& valueAt(unsigned int pos);  // 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 +  int find(int a) const;           // check if 'a' exists in Vector. Return the position 
-  void print(string &msg) const;  // print vector values to standard output +                                   // If found, return the position of the element. 
-  void operator=(const Vector &v);+                                   // Otherwise, return -1
      
-  int operator+() const;     // returns the sum of all elements +  void print() const;              // print to standard output vector values 
-  Vector operator-() const;  // returns a new Vector with negative values; +  void print(string &msg) const;   // print to standard output msg and vector values  
-  Vector operator++();       // prefix increment +  Vectoroperator=(const Vector &v);
-  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 +  int operator+() const;           // returns the sum of Vector elements 
-  Vector operator--(int );   // postfix decrement+  friend int operator*(const Vector&) ;  // returns the product of Vector elements 
 +   
 +  Vector operator-() const;        // returns a new Vector, containing the negative 
 +                                   // values of the original vector. 
 +  Vector& operator++();            // prefix increment. Returns reference to the existing vector. 
 +  friend Vector& operator--(Vector&);    // prefix decrement. Returns reference to the existing vector. 
 +  Vector operator++(int );         // postfix increment. Returns a new Vector. 
 +  friend Vector operator--(Vector&, int);  // postfix decrement. Returns a new Vector. 
 +   
 +  Vector operator~() const;        // returns the binary NOT for each element in a new Vector 
 +  friend Vector operator!(const Vector &v);  // returns a copy of the Vector with  
 +                                             // elements in inverted sequence.
 }; };
 +
 +#endif
 +</code>
 +
 +<code cpp Vector.cpp>
 +#include "Vector.hpp"
  
 Vector::Vector(unsigned int length) { Vector::Vector(unsigned int length) {
Line 88: Line 111:
   }   }
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
-    array[i] = v.valueAt(i);+    array[i] = v.array[i];
 } }
  
Line 99: Line 122:
   }   }
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
-    array[i] = v->valueAt(i);+    array[i] = v->array[i];
 } }
  
Line 110: Line 133:
 } }
  
-int &Vector::valueAt(unsigned int pos) const {+int& Vector::valueAt(unsigned int pos) {
   if(pos>=length()) {   if(pos>=length()) {
-     cerr << "Invalid access position!\n";+     cerr << "Invalid position!\n";
      return array[size-1];      return array[size-1];
   }   }
Line 119: Line 142:
  
 int Vector::find(int a) const { int Vector::find(int a) const {
-  for(int i=0; i<size; i++)+  for(int i=0; i<size; i++) {
     if(array[i] == a)     if(array[i] == a)
       return i;       return i;
-    return -1;+  } 
 +  return -1;
 } }
  
Line 137: Line 161:
 void Vector::print(string &msg) const { void Vector::print(string &msg) const {
   cout << msg;   cout << msg;
-  for(int i=0; i<size; i+++  print();
-    cout << array[i]; +
-    if(i==size-1) +
-      cout << endl; +
-    else +
-      cout << ", "; +
-  }+
 } }
  
-void Vector::operator=(const Vector &v) {+Vector &Vector::operator=(const Vector &v) {
   if(array!=NULL)   if(array!=NULL)
     delete [] array;     delete [] array;
Line 156: Line 174:
   }   }
   for(int i=0; i<size; i++)   for(int i=0; i<size; i++)
-    array[i] = v.valueAt(i);  +    array[i] = v.array[i]; 
 +  return *this;
 } }
  
Line 176: Line 195:
  
 //prefix increment //prefix increment
-Vector Vector::operator++() {+VectorVector::operator++() {
   for(int i=0; i<size; i++) {   for(int i=0; i<size; i++) {
     array[i]++;     array[i]++;
   }   }
-  Vector v(this); +  return *this;
-  return v;+
 } }
 +
 //prefix decrement //prefix decrement
-Vector Vector::operator--() {   +Vectoroperator--(Vector& v) {   
-  for(int i=0; i<size; i++) { +  for(int i=0; i<v.size; i++) { 
-    array[i]--;+    v.array[i]--;
   }   }
-  Vector v(this); 
   return v;   return v;
 } }
  
-int Vector::operator*(const{+int operator*(const Vector& v){
   int product = 1;   int product = 1;
-  for(int i=0; i<size; i++) +  for(int i=0; i<v.size; i++) 
-    product *= array[i];+    product *= v.array[i];
   return product;   return product;
 } }
Line 206: Line 224:
 } }
  
-Vector Vector::operator!(const{ +Vector operator!(const Vector &v) 
-  Vector v(size)+  Vector n = v; 
-  for(int i=0; i<size; i++) +  for(int i=0; i<v.size; i++) 
-    v.valueAt(i= array[size-1-i]; +    n.array[iv.array[v.size-1-i]; 
-  return v;+  return n;
 } }
  
Line 223: Line 241:
  
 //postfix decrement //postfix decrement
-Vector Vector::operator--(int a) { +Vector operator--(Vector& v, int a) { 
-  Vector v(this); +  Vector w(v); 
-  for(int i=0; i<size; i++) { +  for(int i=0; i<v.size; i++) { 
-    array[i]--;+    v.array[i]--;
   }     }  
-  return v;+  return w;
 } }
  
 +</code>
 +
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
  
 int main() { int main() {
Line 239: Line 261:
   string msg = "Initial Vector: ";   string msg = "Initial Vector: ";
      
-  v.print(msg); +  v.print(msg);   
-  int sum = +v; +  cout << "+v: " << +v << endl;
-  cout << "Sum of elements: " << sum << endl;+
      
   Vector f = -v;   Vector f = -v;
-  f.print(msg="Negative values: "); +  f.print(msg="f = -v: "); cout << endl
-  int product = *v; +   
-  cout << "Product of elements: " << product << endl;+  cout << "*v: " << *v << endl;
      
   f = !v;   f = !v;
-  f.print(msg = "Inverted sequence: ");+  f.print(msg = "f = !v "); cout << endl;
      
 +  cout << "~2: "<< ~2 << " | ~3: "<< ~3 << " | ~4: "<< ~4 << endl;
   f = ~v;   f = ~v;
-  f.print(msg = "Binary inverted: ");+  f.print(msg = "f = ~v "); cout << endl;
      
-  msg = "-------------\nPrefix increment"; +  f = ++v;   
-  cout << msg << endl+  f.print(msg = "f = ++v: ")  
-  f = ++v; +  v.print(msg = "v:       ")
-  v.print(msg = "Initial Vector: "); +  f = --v;   
-  f.print(msg = "Assigned Vector: ");+  f.print(msg = "f = --v: ");   
 +  v.print(msg = "v      ")
 +  cout << endl;
      
-  msg = "-------------\nPostfix decrement"; +  f = v++; 
-  cout << msg << endl;+  f.print(msg = "f = v++: ")
 +  v.print(msg = "v:       ");
   f = v--;   f = v--;
-  v.print(msg = "Initial Vector: "); +  f.print(msg = "f = v--: "); 
-  f.print(msg = "Assigned Vector: ");+  v.print(msg = "v      ");
 } }
 </code> </code>
  
-<WRAP center round tip 80%> +
-Από τα παραπάνω παρατηρούμε τα εξής: +
-  * Οι συναρτήσεις που δεν μεταβάλλουν το αντικείμενο στο οποίο εφαρμόζονται δηλώνονται ως **//const//**. +
-  * Σε όλες τις περιπτώσεις επιστρέφεται ένα νέο αντικείμενο ανεξάρτητα εάν μεταβλήθηκε το αντικείμενο πάνω στο οποίο εφαρμόζεται ο τελεστής ή όχι. +
-</WRAP>+
  
cpp/vector_overloading_unary_operators.1493972606.txt.gz · Last modified: 2017/05/05 07:23 (external edit)