Vector Addition

Posted
Comments 0

Part 2 of C++ tutorial – a 3D vector & transform library

To avoid laboriously adding a vectors via addition of their elements, one can improve the facility of the vector class by adding arithmetic operator overloads for addition and subtraction.

My preferred approach is to implement additional functionality in protected const helper functions, that can be used directly by all other methods, whether const or non-const, public or friend.

Thus the first thing to do is to create a helper function that calculates the addition of a supplied vector to this object and returns the resulting vector.

Thus: Vector addition(const Vector& v) const { return Vector(x+v.x,y+v.y,z+v.z); }

A modifying helper method that adds the supplied vector to this object, setting this object with the result, can be implemented using a simple assignment function, thus:

void assign(const Vector& v) { x=v.x; y=v.y; z=v.z; }
void add(const Vector& v) { assign(addition(v)); }

The overloaded operators can then be written succinctly in terms of helpers, thus:

Vector& operator+=(const Vector& v) { add(v); return *this; }
friend Vector operator+(const Vector& u,const Vector& v) { return Vector(u.addition(v)); }

Subtraction is simply a matter of changing the sign.

The complete code is below:

#include <iostream>	// For console output functionality

using namespace std;	// Avoids having to use std:: scoping prefix

class Vector	// A vector class with addition and subtraction functionality
{
public:
	double x,y,z;	// Representation

protected:
	// Non-modifying functions
	Vector addition(const Vector& v) const { return Vector(x+v.x,y+v.y,z+v.z); }
	Vector subtraction(const Vector& v) const { return Vector(x-v.x,y-v.y,z-v.z); }

	// Modifying methods
	void assign(const Vector& v) { x=v.x; y=v.y; z=v.z; }
	void add(const Vector& v) { assign(addition(v)); }
	void subtract(const Vector& v) { assign(subtraction(v)); }

public:
	Vector(double a,double b,double c):x(a),y(b),z(c) { }

	Vector& operator+=(const Vector& v) { add(v); return *this; }
	Vector& operator-=(const Vector& v) { subtract(v); return *this; }

	friend Vector operator+(const Vector& u,const Vector& v) { return Vector(u.addition(v)); }
	friend Vector operator-(const Vector& u,const Vector& v) { return Vector(u.subtraction(v)); }

	friend ostream& operator<<(ostream& os,const Vector& v)
	{ return os<<'('<<v.x<<','<<v.y<<','<<v.z<<')'; }	// Output, e.g. (1,2,3)
};

int main()	// The program
{	Vector v(1,2,3);	// A test vector

    cout << "v="<<v<<"\n";	// Output it

	Vector u(-1,-3,-5);

	cout<<"u="<<u<<"\n";

	Vector s=Vector(u.x+v.x,u.y+v.y,u.z+v.z);	// Sum of two vectors

	cout<<"u+v="<<s<<"\n";

	cout<<"u+v="<<u+v<<"\n";
	cout<<"u-v="<<u-v<<"\n";

}

Program output is:

v=(1,2,3)
u=(-1,-3,-5)
u+v=(0,-1,-2)
u+v=(0,-1,-2)
u-v=(-2,-5,-8)

Author

Comments

There are currently no comments on this article.

Comment

Enter your comment below. Fields marked * are required. You must preview your comment before submitting it.