2 dimensional vector represented as a class

Hello,

I have to create a class to represent a 2 dimensional vector. I need to include certain member functions such as a function to find magnitude of the vector, and one to find the dot product of that vector with another vector, and several others too. That's all fine. A stipulation of the problem is that I must include a constructor which can take cartesian form of the vector and a constructor which can take polar form of vector. Since this involves overloading the constructor the best solution I have come up with is to create the object with either doubles or floats so that the compiler can choose the correct constructor. This seems like a really bad idea. Is there a way I can get the compiler to choose the correct constructor without doing it using the precision? Here is a sample of my header file, there are many more member functions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Vector{		
	public:		
		Vector(double x, double y, double v, double w){
			myArray[0]=x;
			myArray[1]=y;
			additionalArray[0]=v;
			additionalArray[1]=w;
		};
		
		Vector(float rad1, float ang1, float rad2, float ang2){
			myArray[0]=rad1*cos(ang1);
			myArray[1]=rad1*sin(ang1);
			additionalArray[0]=rad2*cos(ang2);
			additionalArray[1]=rad2*sin(ang2);
		}
		
		void multiScalar(){
			double multiArray[2];
			multiArray[0]=5*myArray[0];
			multiArray[1]=5*myArray[1];
			cout<<"When multiplied by a scalar, vector becomes    "<<multiArray[0]<<"i +"<<" "<<multiArray[1]<<"j"<<endl;
		}


        private:
		double myArray[2];
		double additionalArray[2];
};


in my .cpp file the object is created with either four doubles or four floats depending on which constructor I want to implement. There must be a better way.

p.s additionalArray is created for use in member function which require calculations with a second 2d vector.
By "2D vector" you mean something like: http://qt-project.org/doc/qt-5/qvector2d.html

Rather than constructor, you could have a helper function:
Vector fromPolar( double radius, double angle );

If you absolutely need a constructor, then a dummy parameter may be necessary:
Vector::Vector( double radius, double angle, void * )
The user would pass null:
Vector foo( 7, 42, nullptr );
Topic archived. No new replies allowed.