Scope of Variable in Friend Operator - c++

For one of our class assignments we had to implement a polynomial class. It stores the coefficients of a polynomial and outputs the answer.
In my class definition, I have a friend operator that outputs the function:
friend std::ostream& operator << (std::ostream& out, Polynomial& p);
and my implementation is as follows:
std::ostream& operator << (std::ostream& out, Polynomial& p) {
double ans = 0;
for(int i = 0; i <= p.order; ++i)
ans += (double)p.coefficents[i] * pow(p.x, i);
out << ans;
return out;
}
My main function (written by my instructor) is:
int main ()
{
Polynomial p1 (0.5,3); // Invokes two argument constructor for p1
p1.inputCoefficients(); // Set the coefficient of polynomial p1
cout << "Polynomial p1 evaluates to " << p1 << endl;
Polynomial p2(p1), p3; // Copy constructor for p2 and default constructor for p3
cout << "Polynomial p2 evaluates to " << p2 << endl;
cout << "Polynomial p3 evaluates to " << p3 << endl;
p3 = p2; // Copy assignment operator
return 0;
}
My question is:
When I run my program with this line of code:
double ans = 0;
my output is this:
Polynomial p1 evaluates to 1.375
Polynomial p2 evaluates to 1.375
Polynomial p3 evaluates to 0
Polynomial destroyed!
Polynomial destroyed!
Polynomial destroyed!
Which is the correct output
but if I change that line to this:
double ans;
Unexpected things start to happen:
Polynomial p1 evaluates to 1.375
Polynomial p2 evaluates to 1.375
Polynomial p3 evaluates to 1.375
Polynomial destroyed!
Polynomial destroyed!
Polynomial destroyed!
Why is p3 evaluated to 1.375? p3 was created using the default constructor so wouldn't it output 0?
As soon as the polynomial is outputted wouldn't the scope of ans die? Even if it didn't, or ans kept the value from the last time it ran, then wouldn't p2 be doubled (be 2.75) because the << operator ran twice?
Please feel free to get technical and offer advice, I'm curious and want to know the insides of what's actually going on.
Here is the entirety of my code (for reference):
/*
Using dynamic arrays, implement a polynomial class. In mathematics, polynomial is a function of the form f(x) = a0*x^0 + a1*x^1 + a2*x^2 + a3*x^3 + ....n terms. Here, a0, a1, a2 etc. are the coefficients of the polynomial and n is the order of the polynomial.
The private variables include the value of x (a real number), the order of the polynomial (an integer) and the dynamic array that stores the coefficients (real numbers).
The public methods include
a. default constructor that sets the value of x to zero and the order to 0,
b. a two argument constructor that takes as arguments the value of x and the order of the polynomial. The values of the coefficients are set to zero.
c. inputCoefficients(): prompts the user to input the value of the coefficients of the polynomial. For this homework, skip the user input. Instead assign the coefficent values equal to the index of the position in the array. For example, a0 = 0, a1 = 1, a2 = 2 and so on depending on the order of the particular polynomial object
c. a copy constructor
d. << operator overloaded that returns the value of the polynomial (obtained by evaluating the polynomial).
e. == overloaded (copy assignment operator)
f. destructor. Deallocates dynamic arrays and prints a message "Polynomial destroyed! "
Below is the testing program -
*/
#include <iostream>
using std::cin;
using std::endl;
using std::cout;
#include <cmath>
class Polynomial {
public:
Polynomial() {
x = 0,
order = 0;
coefficents = new double[order + 1];
}
Polynomial(const double x, const int order) {
this->x = x;
this->order = order;
this->coefficents = new double[order + 1];
for(int i = 0; i <= order; ++i)
this->coefficents[i] = 0;
}
Polynomial(const Polynomial& p) {
this->x = p.x;
this->order = p.order;
this->coefficents = new double[this->order];
for(int i = 0; i <= this->order; ++i)
this->coefficents[i] = p.coefficents[i];
}
~Polynomial() {
std::cout << "Polynomial destroyed! " << std::endl;
delete[] coefficents;
}
void inputCoefficients() {
/*
for(auto& num: coefficents) {
std::cout << "Enter the next coefficent:: ";
std::cin >> num;
}
std::cout << "Thank you" << std::endl;
*/
for(int i = 0; i <= order; ++i) {
coefficents[i] = i;
}
}
Polynomial& operator = (const Polynomial& p) {
this->x = p.x;
this->order = p.order;
delete[] this->coefficents;
this->coefficents = new double[order + 1];
for(int i = 0; i <= this->order; ++i)
this->coefficents[i] = p.coefficents[i];
return *this;
}
friend std::ostream& operator << (std::ostream& out, Polynomial& p);
friend bool operator == (const Polynomial& p1, const Polynomial& p2);
private:
double x;
double* coefficents;
int order;
};
std::ostream& operator << (std::ostream& out, Polynomial& p) {
double ans;
for(int i = 0; i <= p.order; ++i)
ans += (double)p.coefficents[i] * pow(p.x, i);
out << ans;
return out;
}
bool operator == (const Polynomial& p1, const Polynomial& p2) {
if((p1.x != p2.x) && (p1.order != p2.order))
return false;
for(int i = 0; i < p1.order; ++i) {
if(p1.coefficents[i] != p2.coefficents[i])
return false;
}
return true;
}
int main ()
{
Polynomial p1 (0.5,3); // Invokes two argument constructor for p1
p1.inputCoefficients(); // Set the coefficient of polynomial p1
cout << "Polynomial p1 evaluates to " << p1 << endl;
Polynomial p2(p1), p3; // Copy constructor for p2 and default constructor for p3
cout << "Polynomial p2 evaluates to " << p2 << endl;
cout << "Polynomial p3 evaluates to " << p3 << endl;
p3 = p2; // Copy assignment operator
return 0;
}

Your << function contains the line:
ans += (double)p.coefficents[i] * pow(p.x, i);
If you don't initialize ans to 0, then the initial value of ans will be indeterminate, and then you're adding each term to this. So you get a random result.
In your case, ans is apparently holding on to its value from the previous call. And since p3 is an empty polynomial, the loop never adds anything to it, so you print the previous result.

Related

Why object's destructor was called after assignment operation is done

I have written this class and driver function:
#include <cstdlib>
#include <iostream>
using namespace std;
class Weight {
int grams;
int kilograms;
public:
Weight(int kg, int g) : kilograms(kg), grams(g) {
}
~Weight() {
cout << "Destructor " << this->kilograms << " " << this->grams << "\n";
}
friend Weight operator+(const Weight& a, const Weight& b);
Weight operator+(const Weight& w) const {
cout << "member operator+\n";
int newKg = this->kilograms + w.kilograms;
int newG = this->grams + w.grams;
if (newG >= 1000) {
newG -= 1000;
newKg += 1;
}
return Weight(newKg, newG);
}
Weight operator+(const int addition) const {
cout << "operator+int" << endl;
int newKg = this->kilograms;
int newG = this->grams + addition;
if (newG >= 1000) {
newG -= 1000;
newKg += 1;
}
return Weight(newKg, newG);
}
Weight operator+(const double addition) const {
cout << "operator+double" << endl;
int newKg = this->kilograms + ((int) addition);
int newG = this->grams + (1000 * (addition - ((int) addition)));
if (newG >= 1000) {
newG -= 1000;
newKg += 1;
}
return Weight(newKg, newG);
}
Weight& operator=(const Weight & w) {
cout << "Assignment operator\n";
this->grams = w.grams;
this->kilograms = w.kilograms;
return *this;
}
void print() {
cout << "Weight is: " << this->kilograms << " Kilograms and " << this->grams <<
" Grams\n";
}
};
Weight operator+(const Weight& a, const Weight& b) {
cout << "Friend plus\n";
int newKg = a.kilograms + b.kilograms;
int newG = a.grams + b.grams;
if (newG >= 1000) {
newG -= 1000;
newKg += 1;
}
return Weight(newKg, newG);
}
int main(int argc, char** argv) {
Weight m(90, 900);
m = m + 1.1;
m = m + m;
m.print();
return 0;
}
And here is the output:
operator+double
Assignment operator
Destructor 92 0
Friend plus
Assignment operator
Destructor 184 0
Weight is: 184 Kilograms and 0 Grams
Destructor 184 0
Why the destructor was called twice after the two assignment operators were called? (i.e., third and sixth lines in the output).
I know these are probably for the temporary variable being used for the addition, but what is the rule or C++ specification for this?
Thanks.
Because indeed the operator+ creates a temporary object which is the result of the operation, and is discarded after the assignment.
Consider your own signature for the operator+: Weight operator+(const double addition) const. This returns, by value, a Weight object instance. This isn't a reference, nor a pointer. It's a de-facto new object that is created to hold the result of m + 1.1, without updating the value of m first (unlike operator+=). This is even more evident from a look in your own code: return Weight(newKg, newG); - a new object is created right here, and it needs to be destroyed.
This temporary value is then assigned into m, and the temporary object is then destroyed as it moves out of scope.
As a side note, what you see here is also an optimization (a standard optimization called "return value optimization") as the explicit behavior for this situation would've been the construction of the temporary value in the return statement inside the stack frame of the operator+, followed by a copy-construction of the value in the stack frame of the calling function main and then the destruction of the object in the operator+ stack frame. Then the value in main would've gone into the assignment operator, and then destroyed too. Your compiler just optimized this code to construct the return value directly on the stack frame of the calling function saving an extra temporary object.

2D vector ignores assigned values?

I'm working on a simple class representing polynomials. I store coefficients and equivalent powers of x in 2D vector. The way of assigning certain terms must be as shown in the test file. It equires some operator overloading which i provided. The problem is that it assigns proper coefficients besides last one, additionally every terms has power of x of the first assigned value.
Desired output:
Polynomial p1: 2x^3 + 3.6x + 7x^0
Polynomial p2: 3x^1 + 6x^2 + 1x^4
Actual output:
Polynomial p1: 5x^3 + 2x^3 + 3.6x^3
Polynomial p2: 5x^1 + 3x^1 + 6x^1
Have you got any idea what is the case? I
Polynomials.cpp
double & Polynomials::operator[]( int power_of_x ){
this->coeff_and_power.push_back(5.0); // dummy variable 5.0 just to alloc 'cell' for coefficient
this->coeff_and_power.push_back( (double) power_of_x ); //assigning power of x
this->polynomial_terms.push_back( coeff_and_power ); // polynomial_terms vector contains collection of 2 elements vectors( coefficient and power ) indicating certain term of a function
return (coeff_and_power[0]); //coefficient to be assigned in main(returning by reference) function
}
std::ostream& operator<<(std::ostream& out, const Polynomials & toWrite){
for(unsigned int i = 0; i < toWrite.polynomial_terms.size(); i++){
if(i){
out<<" + ";
}
out<<toWrite.polynomial_terms[i][0]<<"x^"<<toWrite.polynomial_terms[i][1];
}
return out;
}
test.cpp
#include <iostream>
#include "Polynomials.h"
using namespace std;
int main(void) {
Polynomials p1;
p1[3] = 2; p1[1] = 3.6; p1[0] = 7;
Polynomials p2;
p2[1] = 3; p2[2] = 6; p2[4] = 1;
cout << "Polynomial p1: " << p1 << endl;
cout << "Polynomial p2: " << p2 << endl;
}
In your operator[] overload you push_back(), but unconditionally.
return coeff_and_power[0];
return front for assignment
And btw this
this->polynomial_terms.push_back( coeff_and_power )
pushes a copy, not a reference, given polynomial_terms has behavior like e.g. std:vector.

C++ Polynomial addition

I am trying to add two polynomials in C++ and I'm lost as to where to even start. So the user can enter values for a polynomial, they do not need to be in order or anything.
For example it can be:
Poly 1: 2x^5 + 5x^2 - 2x + 9
Poly 2: x^2 + 0
I have coefficient and exponent stored in the class (objects) private fields. So would I look at the first exponent in Poly 1, search Poly 2 for same exponent first and if found add them? Then go on to the second term?
Code as requested: (NOTE: The implementation is currently wrong and I need help on how to work through this problem.)
#include <cmath>
#include <iostream>
using namespace std;
class polynomial
{
public:
polynomial();
polynomial(int);
polynomial(int exponent[], int coefficient[], int);
~polynomial();
polynomial &operator = (const polynomial &obj);
int evaluate(double uservalue);
polynomial operator+(const polynomial &obj) const;
void operator-(const polynomial &obj) const;
void operator*(const polynomial &obj) const;
friend istream & operator>> (istream & in, polynomial &obj);
friend ostream & operator<< (ostream & out, const polynomial &obj);
friend void growone(polynomial &obj);
private:
int *coefficient;
int *exponent;
int size;
};
And the implementation
polynomial polynomial::operator+(const polynomial &obj) const
{
bool matchFound = false;
polynomial tmp;
if (size >= obj.size) {
for (int i = 0; i < size; i++) {
if (i >= tmp.size) {
growone(tmp);
}
for(int y = 0; i < obj.size; i++) {
if (exponent[i] == obj.exponent[y]) {
tmp.coefficient[i] = (coefficient[i]+obj.coefficient[y]);
tmp.exponent[i] = exponent[i];
tmp.size++;
matchFound = true;
}
}
if (matchFound == false) {
tmp.coefficient[i] = coefficient[i];
tmp.exponent[i] = exponent[i];
tmp.size++;
}
matchFound = false;
}
} else {
}
return tmp;
}
You're not really using the power of C++ to its fullest there. A good rule is that, if you are using non-smart pointers for anything, you should step back and have a rethink.
It's no accident that a large number of questions about C on Stack Overflow have to do about problems with pointers :-)
The other point I would add is that it may be worth actually wasting a small amount of memory to greatly simplify your code. By that, I mean don't try to create sparse arrays to hold your terms (coefficient-exponent pairs). Instead, allow each expression to hold every term up to its maximum, with the coefficients for the unused ones simply set to zero. Unless you have an expression like 4x99999999999999 + 3, the amount of extra memory taken may be worth it.
To that end, I'd propose using a std::vector for just the coefficients starting at an exponent of zero. The exponents are actually decided by the position within the vector. So the expression 4x9 - 17x3 + 3 would be stored as the vector:
{3, 0, 0, -17, 0, 0, 0, 0, 0, 4}
0 <------- exponent -------> 9
That actually makes the task of adding and subtracting polynomials incredibly easy since the coefficients are all nicely lined up.
So, with that in mind, let's introduce a cut-down class for showing how it's done:
#include <iostream>
#include <vector>
using std::cout;
using std::vector;
using std::ostream;
class Polynomial {
public:
Polynomial();
Polynomial(size_t expon[], int coeff[], size_t sz);
~Polynomial();
Polynomial &operator=(const Polynomial &poly);
Polynomial operator+(const Polynomial &poly) const;
friend ostream &operator<<(ostream &os, const Polynomial &poly);
private:
std::vector<int> m_coeff;
};
The default constructor (and the destructor) are very simple, we just ensure that an initialised polynomial always has at least one term:
Polynomial::Polynomial() { m_coeff.push_back(0); }
Polynomial::~Polynomial() {}
The constructor-from-array is a little more complex because we want to, as early as possible, turn the user's "anything goes" format into something we can use easily:
Polynomial::Polynomial(size_t expon[], int coeff[], size_t sz) {
// Work out largest exponent and size vector accordingly.
auto maxExpon = 0;
for (size_t i = 0; i < sz; ++i) {
if (expon[i] > maxExpon) {
maxExpon = expon[i];
}
}
m_coeff.resize(maxExpon + 1, 0);
// Fill in coefficients.
for (size_t i = 0; i < sz; ++i) {
m_coeff[expon[i]] = coeff[i];
}
}
Now you'll see why we made the decision to not use sparse arrays and to place the zero exponent at the start of the vector. Bothe operator= and operator+ are simple because they already know where all terms are:
Polynomial &Polynomial::operator=(const Polynomial &poly) {
if (this != &poly) {
m_coeff.clear();
for (int coeff: poly.m_coeff) {
m_coeff.push_back(coeff);
}
}
return *this;
}
Polynomial Polynomial::operator+(const Polynomial &poly) const {
// Create sum with required size.
size_t thisSize = this->m_coeff.size();
size_t polySize = poly.m_coeff.size();
Polynomial sum;
if (thisSize > polySize) {
sum.m_coeff.resize(thisSize, 0);
} else {
sum.m_coeff.resize(polySize, 0);
}
// Do the actual sum (ignoring terms beyond each limit).
for (size_t idx = 0; idx < sum.m_coeff.size(); ++idx) {
if (idx < thisSize) sum.m_coeff[idx] += this->m_coeff[idx];
if (idx < polySize) sum.m_coeff[idx] += poly.m_coeff[idx];
}
return sum;
}
Now we just need to complete this with an output function and a small test mainline:
ostream &operator<< (ostream &os, const Polynomial &poly) {
bool firstTerm = true;
if (poly.m_coeff.size() == 1 && poly.m_coeff[0] == 0) {
os << "0";
return os;
}
for (size_t idx = poly.m_coeff.size(); idx > 0; --idx) {
if (poly.m_coeff[idx - 1] != 0) {
if (firstTerm) {
os << poly.m_coeff[idx - 1];
} else if (poly.m_coeff[idx - 1] == 1) {
os << " + ";
if (idx == 1) {
os << poly.m_coeff[idx - 1];
}
} else if (poly.m_coeff[idx - 1] == -1) {
os << " - ";
} else if (poly.m_coeff[idx - 1] < 0) {
os << " - " << -poly.m_coeff[idx - 1];
} else {
os << " + " << poly.m_coeff[idx - 1];
}
if (idx > 1) {
os << "x";
if (idx > 2) {
os << "^" << idx - 1;
}
}
firstTerm = false;
}
}
return os;
}
int main() {
int c1[] = {1, 2, 3, 4, 5};
size_t e1[] = {3, 1, 4, 0, 9};
Polynomial p1(e1, c1, (size_t)5);
cout << "Polynomial 1 is " << p1 << " (p1)\n";
int c2[] = {6, 7, 8};
size_t e2[] = {3, 7, 9};
Polynomial p2(e2, c2, (size_t)3);
cout << "Polynomial 2 is " << p2 << " (p2)\n";
Polynomial p3 = p1 + p2;
cout << "Polynomial 3 is " << p3 << " (p3 = p1 = p2);
}
And the output, which I've slightly reformatted to show like terms, shows it in action:
Polynomial 1 is 5x^9 + 3x^4 + x^3 + 2x + 4
Polynomial 2 is 8x^9 + 7x^7 + 6x^3
===================================
Polynomial 3 is 13x^9 + 7x^7 + 3x^4 + 7x^3 + 2x + 4

C++ designing a class for big integer addition and subtraction using linked list, template and stack

Current I have implemented only the addition part.
In the main function, my program can actually print out n2 n3 respectively,
but it can't print out the next "n2+n3" case as it gets run-time error.
After I set the breakpoints, I found that when bigInteger& n is passing into case "n2+n3",
the follow statement didnt work and thus the content of n is not modified.
linkedListIterator r = n.digits.begin();
Below are three pieces of codes of the program.
There's another linked list header file which defines the uses of node, iterator (p, q) and some member function like insert() and length().
Thanks anyways for your help.
class bigInteger
{
private:
int sign; // set 0 for positive, 1 for negative
linkedListType<int> digits; // internal linked-list for storing digits in reverse order
public:
bigInteger(); // default constructor
bigInteger(const bigInteger& other); // copy constructor
// Overload constructor
// Use an numerical string to construct this bigInteger
// For negative number, the first char in the string is '-'
// e.g. "-12345"
bigInteger(const string& number);
// overload the assignment operator
const bigInteger& operator= (const bigInteger& other);
// Return a new bigInteger that is equal to *this + other
// The contents of this and other should not be modified
bigInteger& operator+ (bigInteger& other);
// Return a new bigInteger that is equal to *this - other
// The contents of this and other should not be modified
bigInteger& operator- (bigInteger& other);
// Print a big integer
// Since the digits are stored in reverse order in the internal
// list, you should print the list reversely
// Print "undefined" if the digits list is empty
friend ostream& operator<<(ostream& os, bigInteger& n);
};
The second one is about member function implementation.
bigInteger& bigInteger::operator+ ( bigInteger& other )
{
bigInteger resultBI; //saving the answer
bigInteger forSwap; //not used
bool explicitSign = 0; //..
stack<int> resultStack; // stack saving the answer, later converting to Type BigInteger
int result; //local var for addition
int carry = 0; //..
linkedListIterator<int> p = digits.begin(); //iterator marking the first node
linkedListIterator<int> q = other.digits.begin(); //..
if ( this->digits.length() >= other.digits.length() )
{
while ( q != NULL )
{
result = ( *p + *q + carry ) % 10; // '*' acts like dereference
carry = ( *p + *q + carry ) / 10;
++p; // "++' acts like increment to the link
++q;
resultStack.push(result);
}
while ( p != NULL ) //remaining carry
{
result = ( *p + carry ) % 10;
carry = ( *p + carry ) / 10;
++p;
resultStack.push(result);
}
}
if ( this->digits.length() < other.digits.length() )
{
while ( p != NULL )
{
result = ( *p + *q + carry ) % 10;
carry = ( *p + *q + carry ) / 10;
++p;
++q;
resultStack.push(result);
}
while ( q != NULL )
{
result = ( *q + carry ) % 10;
carry = ( *q + carry ) / 10;
++q;
resultStack.push(result);
}
}
if ( carry != 0 ) //push and remaining carry
{
resultStack.push(carry);
}
while ( !resultStack.empty() ) //convert the stack to Type bigInteger
{
resultBI.digits.insert ( resultStack.top() );
resultStack.pop();
}
if ( explicitSign == 1 ) // not used
resultBI.sign = 1;
return resultBI;
}
The last part is the main function.
int main()
{
//-------- Test Case 1
bigInteger n1;
bigInteger n2("987654321");
bigInteger n3("123456789");
cout << "n1 = ";
cout << n1 << endl; // undefined
cout << "n2 = ";
cout << n2 << endl; // 987654321
cout << "n3 = ";
cout << n3 << endl; // 123456789
//-------- Test Case 2
cout << "n2 + n3 = ";
cout << n2 + n3 << endl; // 1111111110 //run-time error
return 0;
}
The problem is that you return the result by reference to a local variable that will disapear as soon as you return.
Define your operator as returning by value and it should work much better:
bigInteger operator+ (bigInteger& other); // return by value.
Additional information:
Here is an article with guidelines explaining the issue pretty well (section "Returning Objects by Value").
Here an article about operator overloading which explains by family of operators the approach to take and pittfalls to avoid.

C++ overloading + operator not working

Hello guys look to my code I'm trying to make a program which asks you to enter the first value by grams And the second value is kilograms and then convert kilograms to grams by an overloaded + operator but it doesn't work why
#include <iostream>
using namespace std;
class ADD{
private:
int Fval;
int Sval;
public:
ADD(){
cout << "WELCOME TO OUR PROGRAM"<<endl<<"PLEASE ENTER THE FIRST VALUE BY GRAMS :";
cin >> Fval;
cout << "PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :"; cin >> Sval;
}
ADD operator+(ADD& add){
add.Sval *= 1000;
return add;
}
int plus(){
return Fval+Sval;
}
};
int main(){
ADD a1;
cout << "THE TOTAL VALUE = " << a1.plus() << " GRAMS";
}
No Effect look to the output
WELCOME TO OUR PROGRAM
PLEASE ENTER THE FIRST VALUE BY GRAMS :2
PLEASE ENTER THE SECOND VALUE BY KILOGRAMS :3
THE TOTAL VALUE = 5 GRAMS
That means the + operator doesn't multiply 3 by 1000
Why??
That's because you're not calling operator +.
You're calling ADD::plus():
int plus(){
return Fval+Sval;
}
Fval and Sval are integers, which you're adding up. It's as simple as that.
EDIT:
Your code is fishy.
ADD operator+(ADD& add){
add.Sval *= 1000;
return add;
}
Multiplication inside operator +? Really? Also, not that you're modifying the parameter, which you shouldn't. It's not intuitive. If you really must do this:
ADD operator+(const ADD& add){
ADD ret;
ret.Sval = add.Sval * 1000;
return ret;
}
The way operator overloading works is that if you have an object of type X, like so:
class X {
public:
X( int v ) : x( v ) {}
int value();
X operator +( const X& y ) {
return X( value() + y.value() );
}
private:
int y;
};
Now if you declare two objects of type X, say X ex and X wye, you can say
X zed = ex + wye;
and get the right result. If type X had more than just a single int field the effect would be more interesting. For example, you could implement 2D vectors and points, and then operations that add and subtract vectors, to get vectors, add and subtract vectors to/from points to get points, and subtract points to get vectors.
Hopefully this will give you enough of an idea of what's going on to be able to restructure your code to get it to do what you want. I may have a detail of syntax wrong, as I'm typing as I go.
Also, I often find the right thing is to declare a friend operator when I want a binary operator:
friend operator + ( vector2D a, vector2D b );
...
inline operator + ( vector2D a, vector 2D b ) {
return vector2D ( a.x + b.x, a.y + b.y );
}