Class Test please help?!

Dont know how to do!!!

#include "Vector2D.h"
#include <math.h> // for sqrt() function

// Vector2D constructor
Vector2D::Vector2D(double x, double y)
{
this->x = x;
this->y = y;
}

// Vector2D destructor constructor
Vector2D::~Vector2D(void)
{
}

// Vector2D method to find the size (length) of a vector
double Vector2D::Magnitude() const
{
// Use pythogoras theorom to find lenght/size of vector (h^2 = x^2 + y^2)
return sqrt(x * x + y * y);
}

// Vector2D overloaded opertor '=' (assignment)
Vector2D& Vector2D::operator= ( const Vector2D& v2 )
{
if (this == &v2)
return *this;

x = v2.x;
y = v2.y;

return *this;
}

// Vector2D overloaded opertor '=' (assignment)
Vector2D& Vector2D::operator+= ( const Vector2D& v2 )
{
x += v2.x;
y += v2.y;

return *this;
}

// Vector2D overloaded boolean opertor '==' (comparison)
bool Vector2D::operator== ( const Vector2D& v2 ) const
{
if( (x == v2.x) && (y == v2.y) )
return true;
else
return false;
}

// Vector2D overloaded boolean opertor '<' (boolean comparison)
bool Vector2D::operator< ( const Vector2D& v2 ) const
{
if( this->Magnitude() < v2.Magnitude() )
return true;
else
return false;
}

// Overloaded stream operator ** FIX TO MATCH EXAMPLE OUTPUT **
// Note the '<<' operator is not part of the Vector2D class directly.
// Q. How does this work and why?
std::ostream& operator<< (std::ostream& os, const Vector2D& v)
{
return os << "(" << " *contents of 2D vector object's <x,y> should be here* " << ")";
}




This is the .h file


#pragma once

#include <iostream>

class Vector2D
{
private:
double x;
double y;

public:
Vector2D(double x = 0.0, double y = 0.0 );
~Vector2D(void);

// Useful methods to work on the 2D vector
double Vector2D::Magnitude() const;

// Overloaded operators
Vector2D& Vector2D::operator= ( const Vector2D& v2 ); // assignment operator

// boolean opertors
bool operator== ( const Vector2D& v2 ) const; // equal comparison operator
// bool operator!= ( const Vector2D& v2 ) const; // not-equal comparison operator
bool operator< ( const Vector2D& v2 ) const; // less-than comparison operator
// bool operator> ( const Vector2D& v2 ) const; // greater-than comparison operator
// bool operator<= ( const Vector2D& v2 ) const; // less-than or equal comparison operator
// bool operator>= ( const Vector2D& v2 ) const; // greater-than or equal comparison operator

// compound assignment operators
Vector2D& operator+= ( const Vector2D& v2 ); // e.g., 'v1+=v2'
// Vector2D& operator-= ( const Vector2D& v2 ); // e.g., 'v1-=v2'
// Vector2D& operator*= ( const double scalar); // e.g., 'v1*=5' multiply by a scalar operator
// Vector2D& operator/= ( const double scalar); // e.g., 'v1/=2' divide by a scalar operator


// Output the details of a Vector2D object to the output stream
friend std::ostream& operator<<(std::ostream& os, const Vector2D& t);
};


This is the main!!

//
//

#include <iostream>
#include "Vector2D.h"

using namespace std;

int main()
{
Vector2D a( 12.5, 35.2);
Vector2D b( 5.3, 46.3);
Vector2D c; // default operator

cout << "SIT153 Prac Assessment Week 07" << endl;
cout << "------------------------------" << endl;
cout << "Example vector class operator overloading...." << endl << endl;

cout << "Initial values of vectors (overload '<<' stream operator):" << endl;
cout << " a = " << a << ", b = " << b << ", c = " << c << endl;

cout << endl << "Simple vector methods:" << endl;
cout << " magnitude of a = " << a.Magnitude() << endl;
cout << " magnitude of b = " << b.Magnitude() << endl;

cout << endl << "Comparing vectors (overloading boolean operators '<', '>', etc.):" << endl;
cout << " (a < b) = " << std::boolalpha << (a < b) << endl;
// cout << " (a > b) = " << std::boolalpha << (a > b) << endl;


cout << endl << "Copying vectors (overloading the assignment operator '='):" << endl;
c = a;
cout << " (a == c) = " << std::boolalpha << (a == c) << endl;
//cout << " (a != c) = " << std::boolalpha << (a != c) << endl;
//cout << " (a >= c) = " << std::boolalpha << (a >= c) << endl;

cout << endl << "Assign vector 'a' results of adding 'a + b' (overloading the operator '+=')" << endl;
a += b;
cout << " (a += b) = " << a << endl;

//cout << endl << "Scalar multiplication (overloading the operator '*=')" << endl;
//a *= -1.0;
//cout << " a = " << a << endl;

//cout << endl << "Scalar division (overloading the operator '/=')" << endl;
//b /= 0.5;
//cout << " b = " << b << endl;
//b /= 0.0; // Handle error case: displpay error message and assign 'x=quiet_NaN()' and 'y=quiet_NaN()'
//cout << " b = " << b << endl;

cout << endl << "Finished." << endl;
return 0;
}
I don't see a question.
Code tags, please.
What is your question? Do you have a problem? Are you wanting abetterway to do this? What are you trying to do anyway? And plz read this :)
http://www.cplusplus.com/search.do?q=using+code+tags
Topic archived. No new replies allowed.