[C++] przeciążanie operatora dodawania
Ostatnio zmodyfikowano 2013-09-11 23:53
zielonyy Temat założony przez niniejszego użytkownika |
» 2013-09-11 23:53:29 dziekuje za poswiecony czas, na koncu wklejam poprawny kod programu.
PS Powinienem zamknac temat ?
#include <iostream> #include <string.h>
using namespace std;
class vektor { int x, y, z; public: vektor( int wartoscX, int wartoscY, int wartoscZ ) : x( wartoscX ) , y( wartoscY ) , z( wartoscZ ) { } friend ostream & operator <<( ostream & out, vektor & punkt ); friend istream & operator >>( istream & out, vektor & punkt ); vektor operator +( const vektor & liczba ) const; int returnwartoscX() { return x; } int returnwartoscY() { return y; } int returnwartoscZ() { return z; } };
vektor vektor::operator +( const vektor & liczba ) const { return vektor( x + liczba.x, y + liczba.y, z + liczba.z ); }
ostream & operator <<( ostream & out, vektor & punkt ) { out << "(" << punkt.x << "," << punkt.y << "," << punkt.z << ")"; return out; }
istream & operator >>( istream & in, vektor & punkt ) { in >> punkt.x; in >> punkt.y; in >> punkt.z; return in; }
int main() { vektor punkt1( 10, 6, 6 ), punkt2( 10, 20, 30 ); vektor punkt3 = punkt2 + punkt1; cout << punkt1 << endl; cout << punkt2 << endl; cout << punkt3 << endl; return 0; }
|
|
1 « 2 » |