Creating a class for 3D vectors?! Methods in public for display and test.

I want to define a class Vector, representing a 3D vector with the following methods:

display - to display with <<cout>> an object Vector;
e.g. 1.0 3.0 5.0

compare - to test the equality of the current object Vector, with another similar object (i.e. of type Vector) passed in parameter.

Then complete the class Vector, in order to:

1)add 2 vectors
Vector addition(Vector other) const;
which would be used like this:
Vector a, b, c;
(affecting a and b)
c = a.addition(b);

2)substraction
3)the opposite of a vector:
4)Scalar multiplication:
Vector mult(double) const;
used like this -
Vector a, b;
double x;

(affectation of a and x)
b = a.mult(x);

5)Scalar product
6)Cross product

I am completely lost. It is the first stage of the exercise, so we aren't expected to use operators or even constructors in this case(though i know a little bit about them). So its supposed to be simple? but i just can't seem to even get the hang of the first exercise?!
I did

class Vector3D{

public:
void display(double x, double y, double z);
void compare();

private:
double x, y, z;

};

int main(){

Vector3D vect1, vect2;
cout << display() << endl;
return 0;
}

I am soooo lost can someone please help! D:
thanks!!
First off, you need to have something define your functions. Also, think about your arguments. You have member variables representing the points in the vector, so you don't need to pass them into your function.

1
2
3
4
5
6
7
8
9
10
11
12
class Vector3D
{
private:
   double x, y, z;
public:
   void Display() const;
};

void Vector3D::Display() const
{
   std::cout << x << ", " << y << ", " << z << std::endl;
}


This method belongs to the Vector3D class, so we need a call on a Vector3D object when we use it.

1
2
3
4
5
int main(int argc, const char* argv[])
{
   Vector3D myVector;
   myVector.Display();
}


For what it's worth, if you know a little about constructors then I'd use one. Just so your points aren't junk values from the get-go.

1
2
3
4
5
6
7
8
class Vector3D
{
private:
   double x, y, z;
public:
   Vector3D() : x(0.0), y(0.0), z(0.0){}
   void Display() const;
};


Hope this helps.
OMG thankyou so much! i will try and use this as a start off. you really explained it well :D
I prefer to overload operator << for outputting that way you can do std::cout << myVector << std::endl;.
http://en.cppreference.com/w/cpp/language/operators --about a third of the way down.
Last edited on
Topic archived. No new replies allowed.