cpp:vector_overloading
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cpp:vector_overloading [2017/05/04 12:38] – [Μοναδιαίοι τελεστές (unary operators)] gthanos | cpp:vector_overloading [Unknown date] (current) – external edit (Unknown date) 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Παράδειγμα | + | ====== Παράδειγμα |
Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση //Vector// η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους. | Ας υποθέσουμε ότι έχουμε την παρακάτω κλάση //Vector// η οποία υλοποιεί ένα μονοδιάστατο πίνακα από ακεραίους. | ||
- | <code cpp Vector.cpp> | + | <code cpp Vector.hpp> |
#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;// return Vector' |
- | int & | + | int & |
- | int find(int a); | + | int find(int a) const; |
+ | // if not element not found | ||
+ | |||
+ | void print() const; // print vector values to standard output | ||
}; | }; | ||
- | Vector:: | + | #endif |
+ | </ | ||
+ | |||
+ | <code cpp Vector.cpp> | ||
+ | #include " | ||
+ | Vector:: | ||
size = length; | size = length; | ||
array = new (nothrow) int[size]; | array = new (nothrow) int[size]; | ||
Line 58: | Line 69: | ||
} | } | ||
- | unsigned | + | int Vector:: |
return size; | return size; | ||
} | } | ||
- | int & | + | int & |
if(pos> | if(pos> | ||
cerr << " | cerr << " | ||
Line 70: | Line 81: | ||
} | } | ||
- | int Vector:: | + | int Vector:: |
for(int i=0; i<size; i++) | for(int i=0; i<size; i++) | ||
if(array[i] == a) | if(array[i] == a) | ||
Line 77: | Line 88: | ||
} | } | ||
+ | void Vector:: | ||
+ | for(int i=0; i<size; i++) { | ||
+ | cout << array[i]; | ||
+ | if(i==size-1) | ||
+ | cout << endl; | ||
+ | else | ||
+ | cout << ", "; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | <code cpp VectorUsage.cpp> | ||
+ | #include " | ||
int main() { | int main() { | ||
Vector v(5); | Vector v(5); | ||
v.valueAt(0) = 2; v.valueAt(1) = 3; | v.valueAt(0) = 2; v.valueAt(1) = 3; | ||
- | v.valueAt(2) = 4; v.valueAt(3) = 5; v.valueAt(4) = 6; | + | v.valueAt(2) = 4; v.valueAt(3) = 5; v.valueAt(4) = 6; |
+ | cout << "value 5 at position: " << v.find(5) << endl; | ||
+ | cout << "value 10 at position: " << v.find(10) << endl; | ||
+ | v.print(); | ||
} | } | ||
</ | </ | ||
- | |||
- | Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ως εξής: | ||
- | |||
- | ===== Μοναδιαίοι τελεστές (unary operators) ===== | ||
- | |||
- | ^ Τελεστής | ||
- | | %%+%% | Πριν | ||
- | | %%-%% | Πριν | ||
- | | %%!%% | Πριν | ||
- | | %%++%% | ||
- | | %%--%% | ||
- | | %%++%% | ||
- | | %%--%% | ||
- | |||
- | <WRAP center round tip 60%> | ||
- | Προσέξτε τη διαφορετική συμπεριφορά ανάμεσα στους μοναδιαίους τελεστές αύξησης και μείωσης κατά ένα (%%++, --%%) όταν αυτοί εφαρμόζονται πριν και μετά το αντικείμενο. | ||
- | </ | ||
- | |||
+ | Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής: | ||
+ | * [[cpp: | ||
+ | * [[cpp: | ||
+ | * [[cpp: | ||
cpp/vector_overloading.1493901488.txt.gz · Last modified: 2017/05/04 11:38 (external edit)