User Tools

Site Tools


cpp:vector_overloading

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
cpp:vector_overloading [2017/05/04 12:21] – created gthanoscpp: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 <iostream> #include <iostream>
 #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; +  int length() const;// return Vector's length. 
-  int &valueAt(unsigned int pos) const; +  int &valueAt(int pos) const;  // return a reference to element at position pos 
-  int find(int a);+  int find(int a) const;      // check if a exists in Vector. Return it position >0 or -1  
 +                              // if not element not found 
 +   
 +  void print() const       // print vector values to standard output
 }; };
  
-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 58: Line 69:
 } }
  
-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 70: Line 81:
 } }
  
-int Vector::find(int a) {+int Vector::find(int a) const {
   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::print() const {
 +  for(int i=0; i<size; i++) {
 +    cout << array[i];
 +    if(i==size-1)
 +      cout << endl;
 +    else
 +      cout << ", ";
 +  }
 +}
 +</code>
 +
 +<code cpp VectorUsage.cpp>
 +#include "Vector.hpp"
 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();
 } }
 </code> </code>
  
-Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε + 
 +Για την παραπάνω κλάση κλάση //Vector// θέλουμε να υπερφορτώσουμε τους τελεστές ανά κατηγορία ως εξής: 
 +  * [[cpp:vector_overloading_unary_operators| Υπερφόρτωση μοναδιαίων τελεστών ]] 
 +  * [[cpp:vector_overloading_binary_operators| Υπερφόρτωση δυαδικών τελεστών που μπορούν να υλοποιηθούν ως μέλη της κλάσης ή ως φιλικές συναρτήσεις ]] 
 +  * [[cpp:vector_overloading_binary_operators2| Υπερφόρτωση δυαδικών τελεστών μπορούν να υλοποιηθούν μόνο ως μέλη της κλάσης ]] 
  
cpp/vector_overloading.1493900511.txt.gz · Last modified: 2017/05/04 11:21 (external edit)