cpp:vector_overloading_binary_operators
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:vector_overloading_binary_operators [2017/05/05 13:09] – gthanos | cpp:vector_overloading_binary_operators [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Υπερφόρτωση δυαδικών τελεστών που | + | ===== Υπερφόρτωση δυαδικών τελεστών που |
- | Παρακάτω | + | Ο παρακάτω |
^ Τελεστής | ^ Τελεστής | ||
- | | %%+%% | // | + | | %%+%% | // |
- | | %%-%% | // | + | | %%-%% | // |
- | | %%+%% | // | + | | %%*%% | // |
+ | | %%+%% | // | ||
| %%+%% | // | | %%+%% | // | ||
| %%-%% | // | | %%-%% | // | ||
Line 13: | 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+(int a) const; | + | |
- | friend Vector operator+(int a, const Vector &v); | + | friend |
- | Vector operator-(int a) const; | + | 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; |
- | friend Vector operator*(int a, const Vector &v); | + | Vector operator*(int a) const; |
- | Vector operator/ | + | friend Vector operator*(int a, const Vector& v); // Returns a new vector. Each element of vector equals the product: |
- | Vector operator%(int a) const; | + | Vector operator/ |
+ | 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 74: | Line 90: | ||
} | } | ||
- | Vector:: | + | Vector:: |
size = v.length(); | size = v.length(); | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 100: | Line 116: | ||
} | } | ||
- | unsigned | + | int Vector:: |
return size; | return size; | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 131: | 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 150: | 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 172: | 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 190: | Line 201: | ||
}*/ | }*/ | ||
- | Vector Vector:: | + | Vector Vector:: |
int length; | int length; | ||
if (size > v.length()) | if (size > v.length()) | ||
Line 208: | 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] | + | length = size; |
+ | else | ||
+ | length = v.size; | ||
+ | Vector n(length); | ||
+ | for(int i=0; i<length; i++) | ||
+ | n.array[i] = array[i] | ||
return n; | return n; | ||
} | } | ||
- | Vector operator+(int a, const Vector &v) { | + | Vector operator+(const Vector& v, int a) { |
+ | Vector n(v.size); | ||
+ | for(int i=0; i< | ||
+ | n.array[i] = v.array[i] + a; | ||
+ | return n; | ||
+ | } | ||
+ | |||
+ | Vector operator+(int a, const Vector& v) { | ||
Vector n(v); | Vector n(v); | ||
for(int i=0; i< | for(int i=0; i< | ||
Line 237: | 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 258: | 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 277: | 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 350: | 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.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.1493989746.txt.gz · Last modified: 2017/05/05 12:09 (external edit)