A megoldás tanulmányozása során figyeljünk az alábbi kérdésekre!
- Az I/O operátorok túlterhelése (mindenhol referencia van. a .h fájlban kell std::, mert ott nem használunk usingot)
- A konstans és a nem konstans [] operátorok (mindegyik kell, egyik ír is, másik csak olvas és konstans objektumra is használható)
- A konstans függvények használatára (amikor csak lehet, konstansnak deklaráljuk a függvényt)
A header file:
#ifndef TWO_DIM_VECTOR_H
#define TWO_DIM_VECTOR_H // A többszörös beépítés elkerülése
#include<iostream>
class TwoDimVector;
std::ostream& operator << (std::ostream&, const TwoDimVector&);
std::istream& operator >> (std::istream&, TwoDimVector&);
class TwoDimVector
{
friend std::ostream& operator << (std::ostream&, const TwoDimVector&);
friend std::istream& operator >> (std::istream&, TwoDimVector&);
public:
double x,y;
// Elegánsabb default értékkel megoldani, mint 3 konstruktorral
TwoDimVector(double x=0, double y=0){this->x=x; this->y=y;};
// Operator+
TwoDimVector operator+(TwoDimVector theOther)const;
// Operator-
TwoDimVector operator-(TwoDimVector theOther)const;
// Operator*
double operator*(TwoDimVector theOther)const;
// Operator[]: értékbeállítás
double& operator[](unsigned int index);
// Operator[]: értéklekérdezés konstans objektumból
double operator[](unsigned int index)const;
};
#endif /* TWO_DIM_VECTOR_H */
Az implementáció:
#include "TwoDimVector.h"
using namespace std;
std::ostream& operator << (std::ostream& os, const TwoDimVector& tdv)
{
os << '(' << tdv.x << '|' << tdv.y << ')';
return os;
}
std::istream& operator >> (std::istream& is, TwoDimVector& tdv)
{
char c;
double x, y;
is >> c;
if(c!='(')
{
is.setstate(ios::failbit);
return is;
}
is >> x;
is >> c;
if(c!='|')
{
is.setstate(ios::failbit);
return is;
}
is >> y;
is >> c;
if(c!=')')
{
is.setstate(ios::failbit);
return is;
}
if( is.good())
{
tdv.x = x;
tdv.y = y;
}
return is;
}
// Operator+
TwoDimVector TwoDimVector::operator+(TwoDimVector theOther)const
{
TwoDimVector newTdw(x+theOther.x, y+theOther.y);
return newTdw;
}
// Operator-
TwoDimVector TwoDimVector::operator-(TwoDimVector theOther)const
{
TwoDimVector newTdw(x-theOther.x, y-theOther.y);
return newTdw;
}
// Operator*
double TwoDimVector::operator*(TwoDimVector theOther)const
{
return x*theOther.x + y*theOther.y;
}
// Operator[]: itt írhatjuk is az értéket.
// Pl. TwoDim Vector v; v[0]=3.2;
double& TwoDimVector::operator[](unsigned int index)
{
if (index == 0) return x;
else return y;
// Egynél nagyobb index esetén kivételt is dobhatunk
}
// Operator[]: ezzel csak olvashatjuk az értéket.
// Pl. const TwoDim Vector v; double x = v[0];
// Ha nem egyszerű típus, akkor konstans referenciával térnénk vissza.
// Mivel beépített, ezért egyszerűen érték szerint térünk vissza
double TwoDimVector::operator[](unsigned int index)const
{
if (index == 0) return x;
else return y;
// Egynél nagyobb index esetén kivételt is dobhatunk
}
A tesztelőprogram:
#include "TwoDimVector.h"
#include <iostream>
using namespace std;
int main()
{
// Létrehozzuk az objektumokat
TwoDimVector tdv1;
TwoDimVector tdv2(4);
TwoDimVector tdv3(2,3);
TwoDimVector tdv4(4,5);
// Kiiratjuk a tagváltozókat
cout << tdv1 << endl;
cout << tdv2 << endl;
cout << tdv3 << endl;
cout << tdv4 << endl;
// Beolvasás tesztelése
cout << "Enter a vector in a form of (x|y): ";
cin >> tdv1;
if(cin.good())
{
cout << tdv1 << endl;
}
else
{
cerr << "Error reading vector. Use format (x|y)" << endl;
}
// A nem konstans [] operátor tesztelése
tdv2[0] = 42;
tdv2[1] = 43;
cout << tdv2 << endl;
// A konstans [] operátor tesztelése
const TwoDimVector tdvc = tdv2;
cout << "x: " << tdvc[0] << " y:" << tdvc[1] << endl;
TwoDimVector tdv5 = tdv3+tdv4;
TwoDimVector tdv6 = tdv3-tdv4;
cout << "Operator+ : " << tdv5 << endl;
cout << "Operator- : " << tdv6 << endl;
cout << "Operator* : " << tdv3*tdv4 << endl;
}