cpp:vector_overloading_binary_operators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
cpp:vector_overloading_binary_operators [2017/05/05 13:38] – gthanos | cpp:vector_overloading_binary_operators [2021/05/24 07:42] – gthanos | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Υπερφόρτωση δυαδικών τελεστών που | + | ===== Υπερφόρτωση δυαδικών τελεστών που |
- | Παρακάτω | + | Ο παρακάτω |
^ Τελεστής | ^ Τελεστής | ||
- | | %%+%% | // | + | | %%+%% | // |
| %%-%% | // | | %%-%% | // | ||
| %%*%% | // | | %%*%% | // | ||
- | | %%+%% | // | + | | %%+%% | // |
| %%+%% | // | | %%+%% | // | ||
| %%-%% | // | | %%-%% | // | ||
Line 14: | Line 14: | ||
| %%/%% | // | | %%/%% | // | ||
| % | // | | % | // | ||
- | | %%<< | + | | %%<< |
- | | %%>> | + | | %%>> |
+ | | %%==%% | ||
+ | | %%!=%% | ||
<WRAP center round info 80%> | <WRAP center round info 80%> | ||
- | Μία φιλική μέθοδο και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. | + | Μία φιλική μέθοδος και ένα μέλος της κλάσης δεν είναι δυνατόν να υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης. Θα πρέπει να επιλέξετε έναν από τους δύο τρόπους για να υλοποιήσετε τη λειτουργία της υπερφόρτωσης. |
</ | </ | ||
- | <code cpp Vector.cpp> | + | <code cpp Vector.hpp> |
#include < | #include < | ||
#include < | #include < | ||
#include < | #include < | ||
using namespace std; | using namespace std; | ||
+ | |||
+ | #ifndef _VECTOR_HPP_ | ||
+ | #define _VECTOR_HPP_ | ||
class Vector { | class Vector { | ||
int *array; | int *array; | ||
- | | + | int size; |
| | ||
public: | public: | ||
- | Vector(unsigned | + | Vector(int length=0); |
- | Vector(const Vector &v); | + | Vector(const Vector& v); |
Vector(const Vector *v); | Vector(const Vector *v); | ||
~Vector(); | ~Vector(); | ||
- | | + | int length() const; |
- | int & | + | int & |
- | int find(int a) const; | + | int find(int a) const; |
- | // if not element not found | + | |
| | ||
- | void print() const; | + | void print() const; |
- | void print(string &msg) const; | + | void print(string &msg) const; |
/* binary operators that don't modify left operand */ | /* binary operators that don't modify left operand */ | ||
- | | + | |
- | Vector operator+(const Vector &v) const; | + | Vector operator+(const Vector& v) const; |
- | //friend Vector operator+(const Vector &v1, const Vector v2); | + | //friend Vector operator+(const Vector& v1, const Vector& v2); // same as above |
- | Vector operator-(const Vector &v) const; | + | Vector operator-(const Vector& v) const; |
- | Vector operator*(Vector &v) const; | + | Vector operator*(const Vector& v) const; |
- | Vector operator+(int a) const; | + | |
- | 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; |
- | Vector operator*(int a) const; | + | Vector operator*(int a) const; |
- | 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: |
- | Vector operator/ | + | Vector operator/ |
- | Vector operator%(int a) const; | + | Vector operator%(int a) const; |
| | ||
- | | + | |
- | | + | |
- | Vector operator >> (int a) const; | + | |
- | //friend Vector operator>> | + | |
| | ||
+ | Vector operator << (int a) const; | ||
+ | //friend Vector operator<< | ||
+ | Vector operator >> (int a) const; | ||
+ | //friend Vector operator>> | ||
}; | }; | ||
- | Vector:: | + | #endif |
+ | </ | ||
+ | |||
+ | <code cpp Vector.cpp> | ||
+ | #include " | ||
+ | |||
+ | Vector:: | ||
size = length; | size = length; | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 76: | Line 90: | ||
} | } | ||
- | Vector:: | + | Vector:: |
size = v.length(); | size = v.length(); | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 102: | Line 116: | ||
} | } | ||
- | unsigned | + | int Vector:: |
return size; | return size; | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 133: | Line 147: | ||
void Vector:: | void Vector:: | ||
cout << msg; | cout << msg; | ||
- | | + | |
- | cout << array[i]; | + | |
- | if(i==size-1) | + | |
- | cout << endl; | + | |
- | else | + | |
- | cout << ", "; | + | |
- | } | + | |
} | } | ||
- | void Vector:: | + | Vector& |
if(array!=NULL) | if(array!=NULL) | ||
delete [] array; | delete [] array; | ||
Line 152: | Line 160: | ||
} | } | ||
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | array[i] = v.valueAt(i); | + | array[i] = v.valueAt(i); |
+ | return *this; | ||
} | } | ||
- | Vector Vector:: | + | Vector Vector:: |
int length; | int length; | ||
if (size > v.length()) | if (size > v.length()) | ||
Line 174: | 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 192: | Line 201: | ||
}*/ | }*/ | ||
- | Vector Vector:: | + | Vector Vector:: |
int length; | int length; | ||
if (size > v.length()) | if (size > v.length()) | ||
Line 210: | Line 219: | ||
} | } | ||
- | Vector Vector:: | + | Vector Vector:: |
- | 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+(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< | for(int i=0; i< | ||
Line 246: | 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< | for(int i=0; i< | ||
Line 267: | Line 281: | ||
return n; | return n; | ||
} | } | ||
+ | |||
+ | bool Vector:: | ||
+ | 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:: | ||
+ | return !(*this == v); | ||
+ | } | ||
+ | |||
/* | /* | ||
- | Vector operator<< | + | Vector operator<< |
Vector n(v); | Vector n(v); | ||
- | int *array_new = new (nothrow) int [n.size+1]; | ||
- | if(array_new==NULL) { | ||
- | cerr << " | ||
- | exit(-1); | ||
- | } | ||
for(int i=0; i< | for(int i=0; i< | ||
- | | + | n.array[i] |
- | array_new[n.size] | + | |
- | n.size++; | + | |
- | delete [] n.array; | + | |
- | n.array = array_new; | + | |
return n; | return n; | ||
}*/ | }*/ | ||
Line 286: | Line 305: | ||
Vector Vector:: | Vector Vector:: | ||
Vector n(*this); | Vector n(*this); | ||
- | int *array_new = new (nothrow) int [n.size+1]; | ||
- | if(array_new==NULL) { | ||
- | cerr << " | ||
- | exit(-1); | ||
- | } | ||
for(int i=0; i< | for(int i=0; i< | ||
- | | + | n.array[i] |
- | array_new[n.size] | + | |
- | n.size++; | + | |
- | delete [] n.array; | + | |
- | n.array = array_new; | + | |
return n; | return n; | ||
} | } | ||
- | /*Vector operator>> | + | /*Vector operator>> |
- | int pos = v.find(a); | + | |
- | if(pos< | + | |
- | return v; | + | |
Vector n(v); | Vector n(v); | ||
- | | + | for(int i=0; i< |
- | if(array_new==NULL) { | + | n.array[i] >>= a; |
- | cerr << " | + | |
- | exit(-1); | + | |
- | } | + | |
- | | + | |
- | | + | |
- | 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 | + | |
return n; | return n; | ||
}*/ | }*/ | ||
Vector Vector:: | Vector Vector:: | ||
- | int pos = find(a); | ||
- | if(pos< | ||
- | return *this; | ||
Vector n(*this); | Vector n(*this); | ||
- | | + | for(int i=0; i< |
- | if(array_new==NULL) { | + | n.array[i] >>= a; |
- | cerr << " | + | |
- | exit(-1); | + | |
- | } | + | |
- | | + | |
- | | + | |
- | 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 | + | |
return n; | return n; | ||
} | } | ||
+ | </ | ||
+ | <code cpp VectorUsage.cpp> | ||
+ | #include " | ||
int main() { | int main() { | ||
Line 359: | Line 338: | ||
Vector f = v; | Vector f = v; | ||
v.print(msg=" | v.print(msg=" | ||
+ | if( f == v ) | ||
+ | cout << "f == v " << endl; | ||
+ | else | ||
+ | cout << "f != v " << endl; | ||
+ | | ||
+ | | ||
Vector k; | Vector k; | ||
k = f + v; | k = f + v; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f - v; | k = f - v; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f * v; | k = f * v; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f + 2; | k = f + 2; | ||
- | k.print(msg=" | + | k.print(msg=" |
+ | k = 2 + f; | ||
+ | k.print(msg=" | ||
k = f - 2; | k = f - 2; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f * 2; | k = f * 2; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f / 2; | k = f / 2; | ||
- | k.print(msg=" | + | k.print(msg=" |
k = f % 2; | k = f % 2; | ||
- | k.print(msg=" | + | k.print(msg=" |
- | f = f << | + | f = f << |
- | f.print(msg=" | + | f.print(msg=" |
- | f = f << 7; | + | f = f >> 2; |
- | f.print(msg=" | + | f.print(msg=" |
- | f = f >> 6; | + | |
- | | + | cout << |
- | | + | |
- | | + | cout << |
} | } | ||
</ | </ | ||
cpp/vector_overloading_binary_operators.txt · Last modified: 2021/05/24 06:42 (external edit)