User Tools

Site Tools


cpp:vector_overloading_binary_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_binary_operators [2019/05/16 07:53] gthanoscpp:vector_overloading_binary_operators [Unknown date] (current) – external edit (Unknown date) 127.0.0.1
Line 1: Line 1:
 ===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ===== ===== Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις =====
  
-Παρακάτω θα δούμε την υπερφόρτωση τελεστών που δεν μεταβάλλουν τον αριστερό τελεστέο. Οι τελεστές περιγράφονται στον παρακάτω πίνακα.+Ο παρακάτω πίνακας περιγράφει τη λειτουργία της υπερφόρτωσης των τελεστών για το παράδειγμα της κλάσης //Vector// που ακολουθεί. Σε αυτή την ενότητα, θα δούμε παραδείγματα υπερφόρτωσης, τα οποία δεν μεταβάλλουν τον αριστερό τελεστέο. 
  
 ^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  ^ Περιγραφή  ^  Φιλική μέθοδος  ^ ^  Τελεστής  ^  Αριστερός τελεστέος  ^  Δεξιός τελεστέος  ^ Περιγραφή  ^  Φιλική μέθοδος  ^
-|  %%+%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το άθροισμα των στοιχείων των δύο τελεστεών τύπου //Vector// στην αντίστοιχη θέση  |  και τα δύο (η φιλική συνάρτηση σε σχόλια)  |+|  %%+%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το άθροισμα των στοιχείων των δύο τελεστέων τύπου //Vector// στην αντίστοιχη θέση  |  και τα δύο (η φιλική συνάρτηση σε σχόλια)  |
 |  %%-%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από τη διαφορά των στοιχείων των δύο τελεστεών τύπου //Vector// στην αντίστοιχη θέση  |  Όχι  | |  %%-%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από τη διαφορά των στοιχείων των δύο τελεστεών τύπου //Vector// στην αντίστοιχη θέση  |  Όχι  |
 |  %%*%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το γινόμενο των στοιχείων των δύο τελεστεών τύπου //Vector// στην αντίστοιχη θέση  |  Όχι  | |  %%*%%  |  //Vector//  |  //Vector//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το γινόμενο των στοιχείων των δύο τελεστεών τύπου //Vector// στην αντίστοιχη θέση  |  Όχι  |
Line 14: Line 14:
 |  %%/%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από την ακέραια διαίρεση του αντίστοιχου στοιχείου του //Vector// με τον ακέραιο |  Όχι  | |  %%/%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από την ακέραια διαίρεση του αντίστοιχου στοιχείου του //Vector// με τον ακέραιο |  Όχι  |
 |  %  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το υπόλοιπο της ακέραιας διαίρεσης του αντίστοιχου στοιχείου του //Vector// με τον ακέραιο |  Όχι  | |  %  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// που κάθε στοιχείο του προκύπτει από το υπόλοιπο της ακέραιας διαίρεσης του αντίστοιχου στοιχείου του //Vector// με τον ακέραιο |  Όχι  |
-|  %%<<%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// στο οποίο έχει προστεθεί ο δεξιός τελεστέος. Το νέο αντικείμενο έχει μέγεθος κατά ένα μεγαλύτερο του αριστερού τελεστέου. |  και τα δύο (η φιλική συνάρτηση σε σχόλια) +|  %%<<%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector//το οποίο προκύπτει από τον αριστερό τελεστέο εάν ολισθήσουμε όλα τα στοιχεία του αριστερά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. |  και τα δύο (η φιλική συνάρτηση σε σχόλια) 
-|  %%>>%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector// στο οποίο έχει διαγραφεί ο δεξιός τελεστέος, εφόσον αυτός υπάρχει. Το νέο αντικείμενο έχει μέγεθος κατά ένα μικρότερο του αριστερού τελεστέου, εάν βρεθεί ο δεξιός τελεστέος μέσα στο //Vector//. Εάν δεν βρεθεί επιστρέφεται ένα αντικείμενο που περιέχει τα στοιχεία του αριστερού τελεστέου. |  και τα δύο (η φιλική συνάρτηση σε σχόλια) +|  %%>>%%  |  //Vector//  |  //int//  | Επιστρέφει ένα νέο αντικείμενο τύπου //Vector//το οποίο προκύπτει από τον αριστερό τελεστέο εάν ολισθήσουμε όλα τα στοιχεία του δεξιά κατά τόσα bits όσα προσδιορίζει ο δεξιός τελεστέος. |  και τα δύο (η φιλική συνάρτηση σε σχόλια) 
-|  %%==%%  |  //int//  |  //Vector//  | Επιστρέφει //true// εάν για κάθε (0<=i<=size) στο στοιχείο του αριστερού τελεστέου στην θέση i, είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά //false// |  Όχι +|  %%==%%  |  //int//  |  //Vector//  | Επιστρέφει //true// εάν για κάθε (0<=i<=size) το στοιχείο του αριστερού τελεστέου στην θέση i, είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά //false// |  Όχι 
-|  %%!=%%  |  //int//  |  //Vector//  | Επιστρέφει //true// εάν για κάθε (0<=i<=size) στο στοιχείο του αριστερού τελεστέου στην θέση i, δεν είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά //false// |  Όχι  |+|  %%!=%%  |  //int//  |  //Vector//  | Επιστρέφει //true// εάν για κάθε (0<=i<=size) το στοιχείο του αριστερού τελεστέου στην θέση i, ΔΕΝ είναι ίσο με το στοιχείο του δεξιού τελεστέου στην ίδια θέση, διαφορετικά //false// |  Όχι  |
  
 <WRAP center round info 80%> <WRAP center round info 80%>
Line 23: Line 23:
 </WRAP> </WRAP>
  
-<code cpp Vector.cpp>+<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:
-  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
      
-  void print() const;           // print vector values to standard output +  void print() const;                                // print vector values to standard output 
-  void print(string &msg) 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 */   /* binary operators that don't modify left operand */
-  Vector &operator=(const Vector &v); +  Vector& operator=(const Vector& v);                // assignment operator 
-  Vector operator+(const Vector &v) const; +  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); +  //friend Vector operator+(const Vector& v1, const Vectorv2);  // same as above 
-  Vector operator-(const Vector &v) const; +  Vector operator-(const Vector& v) const;           // Subtracts v from *this and returns the subtraction in a new vector. 
-  Vector operator*(Vector &v) const; +  Vector operator*(const Vector& v) const;           // Returns a new vector that is the product of v and *this. 
-  Vector operator+(int a) const+  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); +  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; +  Vector operator-(int a) const;                     // Returns a new vector. Each element of vector equals the subtraction:  v[i] - a 
-  Vector operator*(int a) const; +  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); +  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; +  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;+  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; +  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; +  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; +
-  //friend Vector operator<<(const Vector &v, int a); +
-  Vector operator >> (int a) const; +
-  //friend Vector operator>>(const Vector &v, int a);+
      
 +  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  
 }; };
  
-Vector::Vector(unsigned int length) {+#endif 
 +</code> 
 + 
 +<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 81: Line 90:
 } }
  
-Vector::Vector(const Vector &v) {+Vector::Vector(const Vector& v) {
   size = v.length();   size = v.length();
   array = new (nothrow) int[size];   array = new (nothrow) int[size];
Line 107: Line 116:
 } }
  
-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 138: Line 147:
 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 << ", "; +
-  }+
 } }
  
-Vector &Vector::operator=(const Vector &v) {+Vector& Vector::operator=(const Vector& v) {
   if(array!=NULL)   if(array!=NULL)
     delete [] array;     delete [] array;
Line 161: Line 164:
 } }
  
-Vector Vector::operator+(const Vector &v) const {+Vector Vector::operator+(const Vector& v) const {
   int length;   int length;
   if (size > v.length())   if (size > v.length())
Line 180: Line 183:
  
 /* /*
-Vector operator+(const Vector &v1, const Vector v2) {+Vector operator+(const Vector& v1, const Vector v2) {
   int length;   int length;
   if (v1.size > v2.size)   if (v1.size > v2.size)
Line 198: Line 201:
 }*/ }*/
  
-Vector Vector::operator-(const Vector &v) const {+Vector Vector::operator-(const Vector& v) const {
   int length;   int length;
   if (size > v.length())   if (size > v.length())
Line 216: Line 219:
 } }
  
-Vector Vector::operator*(Vector &v) const { +Vector Vector::operator*(const Vector& v) const { 
-  Vector n(size); +  int length; 
-  for(int i=0; i<size; i++)     +  if (size < v.size) 
-      n.array[i] = array[i] * v.array[i];+    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;   return n;
 } }
  
-Vector Vector::operator+(int a) const +Vector operator+(const Vector& v, int a) { 
-  Vector n(size); +  Vector n(v.size); 
-  for(int i=0; i<size; i++) +  for(int i=0; i<v.size; i++) 
-    n.array[i] = array[i] + a;+    n.array[i] = v.array[i] + a;
   return n;   return n;
 } }
  
-Vector operator+(int a, const Vector &v) {+Vector operator+(int a, const Vector& v) {
   Vector n(v);   Vector n(v);
   for(int i=0; i<v.length(); i++) {   for(int i=0; i<v.length(); i++) {
Line 252: Line 260:
 } }
  
-Vector operator*(int a, const Vector &v) {+Vector operator*(int a, const Vector& v) {
   Vector n(v);   Vector n(v);
   for(int i=0; i<v.length(); i++) {   for(int i=0; i<v.length(); i++) {
Line 274: Line 282:
 } }
  
-bool Vector::operator==(const Vector &v) const {+bool Vector::operator==(const Vector& v) const {
   if( size != v.size )   if( size != v.size )
     return false;     return false;
Line 283: Line 291:
 } }
  
-bool Vector::operator!=(const Vector &v) const {+bool Vector::operator!=(const Vector& v) const {
   return !(*this == v);   return !(*this == v);
 } }
  
 /* /*
-Vector operator<<(const Vector &v, int a){+Vector operator<<(const Vector& v, int a){
   Vector n(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++)   for(int i=0; i<n.size; i++)
-    array_new[i] = n.array[i]+    n.array[i] <<= a;
-  array_new[n.size] = a+
-  n.size++; +
-  delete [] n.array; +
-  n.array = array_new;+
   return n;   return n;
 }*/ }*/
Line 306: Line 305:
 Vector Vector::operator << (int a) const { Vector Vector::operator << (int a) const {
   Vector n(*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++)   for(int i=0; i<n.size; i++)
-    array_new[i] = n.array[i]+    n.array[i] <<= a;
-  array_new[n.size] = a+
-  n.size++; +
-  delete [] n.array; +
-  n.array = array_new;+
   return n;   return n;
 } }
  
-/*Vector operator>>(const Vector &v, int a) { +/*Vector operator>>(const Vector& v, int a) {
-  int pos = v.find(a); +
-  if(pos<0) +
-    return v;+
   Vector n(v);   Vector n(v);
-  int *array_new = new (nothrow) int [n.size-1]; +  for(int i=0; i<n.size; i++) 
-  if(array_new==NULL) { +    n.array[i] >>a;
-    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;   return n;
 }*/ }*/
  
 Vector Vector::operator >> (int a) const { Vector Vector::operator >> (int a) const {
-  int pos = find(a); 
-  if(pos<0) 
-    return *this; 
   Vector n(*this);   Vector n(*this);
-  int *array_new = new (nothrow) int [n.size-1]; +  for(int i=0; i<n.size; i++) 
-  if(array_new==NULL) { +    n.array[i] >>a;
-    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;   return n;
 } }
 +</code>
  
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
  
 int main() { int main() {
Line 387: Line 346:
   Vector k;   Vector k;
   k = f + v;   k = f + v;
-  k.print(msg="f + v:  ");+  k.print(msg="k = f + v:  ");
   k = f - v;   k = f - v;
-  k.print(msg="f - v:  ");+  k.print(msg="k = f - v:  ");
   k = f * v;   k = f * v;
-  k.print(msg="f * v:  ");+  k.print(msg="k = f * v:  ");
   k = f + 2;   k = f + 2;
-  k.print(msg="f + 2:  ");+  k.print(msg="k = f + 2:  "); 
 +  k = 2 + f; 
 +  k.print(msg="k = 2 + f:  ");
   k = f - 2;   k = f - 2;
-  k.print(msg="f - 2:  ");+  k.print(msg="k = f - 2:  ");
   k = f * 2;   k = f * 2;
-  k.print(msg="f * 2:  ");+  k.print(msg="k = f * 2:  ");
   k = f / 2;   k = f / 2;
-  k.print(msg="f / 2:  ");+  k.print(msg="k = f / 2:  ");
   k = f % 2;   k = f % 2;
-  k.print(msg="f % 2:  "); +  k.print(msg="k = f % 2:  "); 
-  f = f << 7+  f = f << 2
-  f.print(msg="f = f << 7 "); +  f.print(msg="f = f << 2: "); 
-  f = f << 7; +  f = f >> 2
-  f.print(msg="f = f << 7:  "); +  f.print(msg="f = f >> 2: ");
-  f = f >> 6+
-  f.print(msg="f = f >> 6 "); +
-  f = f >> 6; +
-  f.print(msg="f = f >> 6:  ");+
   if( f == v )   if( f == v )
     cout << "f == v " << endl;     cout << "f == v " << endl;
cpp/vector_overloading_binary_operators.1557993202.txt.gz · Last modified: 2019/05/16 06:53 (external edit)