write the definition in c++ for a class named vector2d that stores information about a two-dimensional vector. the class should have functions to get and set the x and y components, where x and y are integers
next, overload the * operator so that it returns the dot product of two vectors

(Ax+Bx)+(Ay+By)

so far I have this

# include <iostream>
# include <vector>


class Vector2D
{
    
    public Vector2D() { _x = _y = 0; }

    
    public Vector2D(float x, float y) { _x = x; _y = y; }

    
    public Vector2D(Vector2D other) { _x = other._x; _y = other._y; }

    
    private float _x, _y;
	void set(const _x, const _y ) {X=x; Y=y;  }
		void set(const vector2d & p) { X=p.X; Y=p.Y; }
		getLength() const { return sqrt(X*X + Y*Y ); }
		getLength() const { return X*X + Y*Y ; }
}
vector2d operator*(const vector2d& other) const { return vector2d(X * other.X, Y * other.Y);	}
		vector2d& operator*=(const vector2d& other)	{ X*=other.X; Y*=other.Y;  return *this; }
	

	dotProduct(const vector3d<T>& other) const
		{
			return X*other.X + Y*other.Y + Z*other.Z;
		}

You need to say what the problem is. It's not going to compile, but please state that and give the location and the error messages and a detailed question. For starters, this is C++, not Java, so you need a colon after public and private and you don't have to put it before everything, only when it changes from public to private or vice-versa. There are some other problems, but this will be a start.

class Vector2D
{
    
    public:
        Vector2D() { _x = _y = 0; }
        Vector2D(float x, float y) { _x = x; _y = y;     
        Vector2D(Vector2D other) { _x = other._x; _y = other._y; }
    
    private:
        float _x, _y;
	void set(const _x, const _y ) {X=x; Y=y;  }
	void set(const vector2d & p) { X=p.X; Y=p.Y; }
	getLength() const { return sqrt(X*X + Y*Y ); }
	getLength() const { return X*X + Y*Y ; }
}

The first thing I noticed is that you aren't following the instructions.

"...where x and y are integers"

You define them as float.

There are several small errors that I could point out but I would rather just suggest that you slow down and take things one step at a time and follow the instructions very closely.

It appears to me you've quickly typed out sloppy code that you hope one of us will correct for you.

write the definition in c++ for a class named vector2d that stores information about a two-dimensional vector. the class should have functions to get and set the x and y components, where x and y are integers
next, overload the * operator so that it returns the dot product of two vectors

(Ax+Bx)+(Ay+By)
I modified my level best but how can i add main for this

# include <iostream>
# include <vector>
# include<cmath>


class Vector2D
{
 public:
  Vector2D(void);
  Vector2D(double nx, double ny);
  Vector2D(bool v);
  void normalize(void);
  double magnitude(void);
  void full_dump(void) const;
  bool valid;
  double x,y;
 
};

double dot(Vector2D v, Vector2D w);

 Vector2D operator+(Vector2D v, Vector2D w);

 Vector2D operator*(Vector2D v, Vector2D w);
 Vector2D operator*(Vector2D v, double w);
 Vector2D operator*(double w, Vector2D v);


inline Vector2D::Vector2D(void)
{
 x=y=0;
 valid=true;
}

inline Vector2D::Vector2D(double nx, double ny)
{
 x=nx;
 y=ny;
 valid=true;
}

inline Vector2D::Vector2D(bool v)
{
 x=y=0;
 valid=v; 
}


inline Vector2D operator+(Vector2D v, Vector2D w)
{
 return Vector2D(
  v.x + w.x,
  v.y + w.y
 );
}



inline Vector2D operator*(Vector2D v, Vector2D w)
{
 return Vector2D(
  v.x * w.x,
  v.y * w.y
 );
}

 inline Vector2D operator*(Vector2D v, double w)
 {
  return Vector2D(
   v.x * w,
   v.y * w
  );
 }
 
 inline Vector2D operator*(double w, Vector2D v)
 {
  return v*w;
 }

inline Vector2D operator/(Vector2D v, Vector2D w)
{
 return Vector2D (
  v.x / w.x,
  v.y / w.y
 );
}

 inline Vector2D operator/(Vector2D v, double w)
 {
  return Vector2D (
   v.x / w,
   v.y / w
  );
 }
 
 inline Vector2D operator/(double w, Vector2D v)
 {
  return Vector2D (
   w / v.x,
   w / v.y
  );
 }



inline void Vector2D::normalize(void)
{
 double mag=magnitude();
 x/=mag;
 y/=mag;
}


inline double Vector2D::magnitude(void)
{
 return sqrt(x*x+y*y);
}



inline Vector2D cross(Vector2D v, Vector2D w)
{
 
 return Vector2D(0,0);
}



inline double dot(Vector2D v, Vector2D w)
{
 Vector2D x=w*v;
 return x.x + x.y;
}
Member Avatar for jencas
getLength() const { return sqrt(X*X + Y*Y ); }
	getLength() const { return X*X + Y*Y ; }

No return type and defined twice....

>>how can i add main for this

First, develop a protocol to display a Vector2D object. You could write a class method or you could overload the << operator for the Vector2D class. In my example below I assume an overloaded << operator has been developed.

Second, you might consider making most of the non member functions you have declared friend methods and move them inside the class declaration.

Third, organize the code you have written into two files. All the declarations should go in a header file, which I assumed would be named Vector2D.h, and all the definitions go into a cpp file, which I would assume would be named Vector2D.cpp.

Then write a program where you include the header file, declare some Vector2D objects and call the methods/operators on them to see if they work. In the future, I would suggest you get the driver program up and working so you can test each method as you write it, and not wait until you write the whole class before testing it.

#include <iostream>
#include "Vector2D"

int main()
{
   Vector2D foo(4.0, 7.0);
   Vector2D bar(5.0, 1.0);
   Vector2D foobar = foo + bar;
   cout << foobar;
   cin.get();
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.