// Keith Lin & Chris Baldwin - Feb 14 2011, computger applications 2 dimension vector
// the purpose of the program is to overload the + and - operator for 2 dimension calculation
//we also need to calculate the r magnituede and the 0 angle.
#include <iostream>
#include <cmath>
#include <iomanip>
usingnamespace std;
class twoDvector{
private:
double xx;
double yy;
public:
twoDvector (double x = 1, double y = 1) {setData(x,y);}
void setData (double x, double y) {xx = x; yy = y;}
double getx(void) {return xx;}
double gety(void) {return yy;}
twoDvector operator+ (const twoDvector &right);
twoDvector operator- (const twoDvector &right);
operatordouble (void);
operatorfloat (void);
};
twoDvector twoDvector::operator+(const twoDvector &right){
twoDvector temp;
temp.xx = xx + right.xx;
temp.yy = yy+right.yy;
return temp;
}
twoDvector twoDvector::operator-(const twoDvector &right)
{
twoDvector temp;
temp.xx = xx - right.xx;
temp.yy = yy - right.yy;
return temp;
}
twoDvector::operatordouble(void)
{
return sqrt(xx*xx+yy*yy);
}
twoDvector::operatorfloat(void)
{
if (xx != 0)
{
return atan(yy/xx);
}
else
cout << "You can't divide a number by zero." << endl;
}
int main(void)
{
twoDvector vec1;
twoDvector vec2;
twoDvector vec3 = vec1 + vec2;
cout << "This is the R value of vector 3: " << double(vec3) << ".\n\n";
cout << "This is the THETA value of vector 3: " << float(vec3) << ".\n\n";
}
look carefully at this line
1 2 3 4 5
private:
double xx;
double yy;
public:
twoDvector (double x = 1, double y = 1) {setData(x,y);}
those xx yy x and y are declare as double yea? I have a similar example which is exactly the same but instead of double it is "int" and the last line looks like this twoDvector (int x = 1, double y = 1) {setData(int x, int y)
for some reason the "int" went in the "setData" whcih i don't mind but if i do that to double which is twoDvector (double x = 1, double y = 1) {setData(double x,double y)
it will give me an error why?
This will compile fine for me, but it warns me about the possible loss of data when converting from double to int. Conversion from int to double does not typically result in a loss of data,