cpp:vector_overloading_binary_operators2
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
cpp:vector_overloading_binary_operators2 [2017/05/05 12:57] – created gthanos | cpp:vector_overloading_binary_operators2 [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ===== Υπερφόρτωση δυαδικών τελεστών που δεν μεταβάλλουν τον αριστερό τελεστέο | + | ===== Υπερφόρτωση δυαδικών τελεστών |
- | Παρακάτω θα δούμε την υπερφόρτωση τελεστών που δεν μεταβάλλουν τον αριστερό τελεστέο. | + | Σε αυτή την ενότητα, |
+ | |||
+ | ^ Τελεστής | ||
+ | | %%=%% | // | ||
+ | | %%[ ]%% | // | ||
+ | | %%+=%% | ||
+ | | %%+=%% | ||
+ | | %%-=%% | ||
+ | | %%-=%% | ||
+ | | %%*=%% | ||
+ | | %%*=%% | ||
+ | | %%/ | ||
+ | | %% %=%% | // | ||
+ | | %%<< | ||
+ | | %%>> | ||
- | ^ Τελεστής | + | <code cpp Vector.hpp> |
- | | %%+%% | // | + | |
- | | %%-%% | // | + | |
- | | %%+%% | // | + | |
- | | %%+%% | // | + | |
- | | %%-%% | // | + | |
- | | %%*%% | // | + | |
- | | %%*%% | // | + | |
- | | %%/%% | // | + | |
- | | % | // | + | |
- | | %%<< | + | |
- | | %%>> | + | |
- | + | ||
- | <WRAP center round info 80%> | + | |
- | Όταν έχετε μία φιλική μέθοδο και ένα μέλος της κλάσης που υλοποιούν την ίδια λειτουργικότητα υπερφόρτωσης, | + | |
- | </ | + | |
- | + | ||
- | + | ||
- | + | ||
- | <code cpp Vector.cpp> | + | |
#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 | // if not element not found | ||
Line 44: | Line 42: | ||
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); | + | Vector operator*(Vector& |
- | Vector operator-(const Vector &v) const; | + | |
- | Vector operator+(int a) const; | + | |
- | friend Vector operator+(int a, const Vector &v); | + | |
- | Vector operator-(int a) const; | + | |
- | Vector operator*(int a) const; | + | |
- | friend Vector operator*(int a, const Vector &v); | + | |
- | Vector operator/ | + | |
- | Vector operator%(int a) const; | + | |
| | ||
- | Vector operator | + | |
- | | + | |
- | Vector operator | + | Vector& operator+=(const Vector& v); |
- | // | + | Vector& operator+=(int a); |
+ | Vector& operator-=(const Vector& v); | ||
+ | Vector& operator-=(int a); | ||
+ | Vector& operator*=(const Vector& v); | ||
+ | Vector& operator*=(int a); | ||
+ | | ||
+ | | ||
+ | Vector& operator<< | ||
+ | Vector& | ||
+ | int &operator[](int a) const; | ||
| | ||
}; | }; | ||
- | 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 81: | ||
} | } | ||
- | Vector:: | + | Vector:: |
size = v.length(); | size = v.length(); | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 102: | Line 107: | ||
} | } | ||
- | unsigned | + | int Vector:: |
return size; | return size; | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 142: | Line 147: | ||
} | } | ||
- | void Vector:: | + | Vector& |
if(array!=NULL) | if(array!=NULL) | ||
delete [] array; | delete [] array; | ||
Line 152: | Line 157: | ||
} | } | ||
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 173: | Line 179: | ||
} | } | ||
- | /* | + | Vector Vector:: |
- | Vector operator+(const Vector &v1, const Vector v2) { | + | |
- | int length; | + | |
- | if (v1.size > v2.size) | + | |
- | length = v1.length(); | + | |
- | else | + | |
- | length = v2.length(); | + | |
- | Vector n(length); | + | |
- | for(int i=0; i< | + | |
- | int sum = 0; | + | |
- | if(i< | + | |
- | sum += v1.array[i]; | + | |
- | if(i< | + | |
- | sum += v2.array[i]; | + | |
- | n.array[i] = sum; | + | |
- | } | + | |
- | return n; | + | |
- | }*/ | + | |
- | + | ||
- | Vector Vector:: | + | |
int length; | int length; | ||
if (size > v.length()) | if (size > v.length()) | ||
Line 210: | Line 197: | ||
} | } | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | Vector n(size); | + | |
+ | | ||
+ | n = n + *this; | ||
+ | *this = n; | ||
+ | } | ||
+ | else { | ||
+ | *this = *this + v; | ||
+ | } | ||
+ | return *this; | ||
+ | } | ||
+ | |||
+ | Vector& Vector:: | ||
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | | + | array[i] |
- | return | + | return |
} | } | ||
- | Vector operator+(int a, const Vector &v) { | + | Vector& Vector::operator-=(const Vector& v) { |
- | | + | |
- | for(int i=0; i<v.length(); i++) { | + | Vector n(v); |
- | n.array[i] += a; | + | n = n - *this; |
+ | *this = n; | ||
} | } | ||
- | return | + | |
+ | *this = *this - v; | ||
+ | } | ||
+ | | ||
} | } | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | Vector n(size); | + | |
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | | + | array[i] |
- | return | + | return |
} | } | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | | + | |
+ | return *this; | ||
+ | | ||
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | | + | array[i] |
- | return | + | return |
} | } | ||
- | Vector operator*(int a, const Vector &v) { | + | Vector& Vector::operator*=(int a) { |
- | Vector n(v); | + | for(int i=0; i<size; i++) |
- | for(int i=0; i<v.length(); i++) { | + | array[i] *= a; |
- | | + | return |
- | } | + | |
- | return | + | |
} | } | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | Vector n(size); | + | |
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | | + | array[i] |
- | return | + | return |
} | } | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | Vector n(size); | + | |
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
- | | + | array[i] |
- | return | + | return |
} | } | ||
- | /* | ||
- | Vector operator<< | ||
- | Vector n(v); | ||
- | int *array_new = new (nothrow) int [n.size+1]; | ||
- | if(array_new==NULL) { | ||
- | cerr << " | ||
- | exit(-1); | ||
- | } | ||
- | for(int i=0; i< | ||
- | array_new[i] = n.array[i]; | ||
- | array_new[n.size] = a; | ||
- | n.size++; | ||
- | delete [] n.array; | ||
- | n.array = array_new; | ||
- | return n; | ||
- | }*/ | ||
- | Vector Vector:: | + | Vector& Vector:: |
- | Vector n(*this); | + | for(int i=0; i<size; i++) |
- | int *array_new = new (nothrow) int [n.size+1]; | + | array[i] |
- | if(array_new==NULL) { | + | return |
- | cerr << " | + | |
- | exit(-1); | + | |
- | } | + | |
- | for(int i=0; i<n.size; i++) | + | |
- | | + | |
- | array_new[n.size] | + | |
- | n.size++; | + | |
- | delete [] n.array; | + | |
- | n.array = array_new; | + | |
- | return | + | |
} | } | ||
- | /*Vector operator>> | + | Vector& Vector::operator>> |
- | int pos = v.find(a); | + | for(int i=0; i<size; i++) |
- | if(pos< | + | array[i] >>= a; |
- | return v; | + | return |
- | Vector n(v); | + | } |
- | int *array_new = new (nothrow) int [n.size-1]; | + | |
- | if(array_new==NULL) { | + | |
- | cerr << " | + | |
- | exit(-1); | + | |
- | } | + | |
- | for(int i=0; i<n.size; i++) { | + | |
- | | + | |
- | array_new[i] = n.array[i]; | + | |
- | if(i==pos) | + | |
- | continue; | + | |
- | if(i>pos) | + | |
- | array_new[i-1] | + | |
- | } | + | |
- | n.size--; | + | |
- | delete [] n.array; | + | |
- | n.array = array_new; | + | |
- | return | + | |
- | }*/ | + | |
- | Vector | + | int &Vector:: |
- | | + | return array[pos]; |
- | if(pos< | + | |
- | | + | |
- | Vector n(*this); | + | |
- | int *array_new = new (nothrow) int [n.size-1]; | + | |
- | if(array_new==NULL) { | + | |
- | cerr << " | + | |
- | exit(-1); | + | |
- | } | + | |
- | for(int i=0; i< | + | |
- | if(i< | + | |
- | array_new[i] = n.array[i]; | + | |
- | if(i==pos) | + | |
- | continue; | + | |
- | if(i> | + | |
- | array_new[i-1] = n.array[i]; | + | |
- | } | + | |
- | n.size--; | + | |
- | delete [] n.array; | + | |
- | n.array = array_new; | + | |
- | return n; | + | |
} | } | ||
+ | </ | ||
+ | <code cpp VectorUsage.cpp> | ||
+ | #include " | ||
int main() { | int main() { | ||
Line 350: | Line 288: | ||
v.print(msg); | v.print(msg); | ||
| | ||
- | Vector f = v; | + | Vector f , g; |
- | | + | |
- | Vector k; | + | |
- | k = f + v; | + | |
- | | + | |
- | | + | g = f += v; |
- | k.print(msg=" | + | |
- | | + | |
- | | + | f -= v; |
- | | + | |
- | | + | f *= v; |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | |
- | | + | f -= 2; |
- | f = f << 7; | + | f.print(msg=" |
- | f.print(msg=" | + | f *= 2; |
- | f = f << 7; | + | f.print(msg=" |
- | f.print(msg=" | + | f /= 2; |
- | f = f >> 6; | + | f.print(msg=" |
- | f.print(msg=" | + | f %= 2; |
- | f = f >> 6; | + | f.print(msg=" |
- | f.print(msg=" | + | |
| | ||
+ | cout << " | ||
+ | v[2] = 100; | ||
+ | v.print(msg=" | ||
} | } | ||
</ | </ | ||
cpp/vector_overloading_binary_operators2.1493989025.txt.gz · Last modified: 2017/05/05 11:57 (external edit)