I am having trouble with my homework for my programming class. This is what I need to do:
Coordinates have the form (x, y, z)
Use double variables to represent the private data of the class.
Provide a constructor that enables an object to be initialized when it is declared and has default values of 0.0.
Provide a friend function << for cout
Provide a friend function >> for cin
Provide an overloaded operator + for this class
Provide an overloaded operator - for this class
Provide an overloaded operator * for this class
Provide an overloaded operator = for this class
Provide an overloaded operator == for this class
Provide an overloaded operator != for this class
The output should look as follows:
Enter a graphical coordinate in the form: (x, y, z)
? (3, 4, 5)
a = b + c:
(7, 9, 14) = (4, 8, 16) + (3, 1, -2)
a = b - c:
(1, 7, 18) = (4, 8, 16) - (3, 1, -2)
a = b * c:
(12, 8, -32) = (4, 8, 16) * (3, 1, -2)
(12, 8, -32) != (3, 4, 5)
(3, 4, 5) == (3, 4, 5)
my main problem is that I cant seem to get the overload operators right. I have to use these three files: Coordinate.cpp, Coordinate.h and pa9.cpp So here is what I have:
#include "Coordinate.h"
#include <iostream>
using namespace std;
int main()
{
Coordinate a, b( 4, 8, 16 ), c( 3, 1, -2 ), k;
cout << "Enter a graphical coordinate in the form: (x, y, z)\n? ";
cin >> k;
a = b + c;
cout << "\na = b + c:\n" << a << " = " << b << " + " << c << '\n';
a = b - c;
cout << "\na = b - c:\n" << a << " = " << b << " - " << c << '\n';
a = b * c;
cout << "\na = b * c:\n" << a << " = " << b << " * " << c << "\n\n";
if ( a != k )
{
cout << a << " != " << k << '\n';
}
cout << '\n';
a = k;
if ( a == k )
{
cout << a << " == " << k << '\n';
}
}
I'm a bit lost on why I cant get it to work. I uploaded it without my work on overloaded operators because it was a mess and just confusing. Oh, and I cant edit the pa9 file as its what I have to work around. I have to use a g++ compiler to compile it all. Help would be much appreciated
Coordinate::Coordinate()
: x(0), y(0), z(0)
{
// constructor body is almost always empty
}
Coordinate::Coordinate(double newX,double newY,double newZ)
: x(newX), y(newY), z(newZ)
{
// same
}
booloperator==(const Coordinate& l, const Coordinate& r)
{
// actually this is a VERY bad idea to use == on doubles,
// but that would take us away from the main topic
return l.x == r.x && l.y == r.y && l.z == r.z;
}
// express all rel ops through == and <
booloperator!=(const Coordinate& lhs, const Coordinate& rhs)
{
return !(lhs == rhs);
}
Coordinate& Coordinate::operator+=(const Coordinate& other)
{
x += other.x;
y += other.y;
z += other.z;
return *this;
}
Coordinate& Coordinate::operator-=(const Coordinate& other)
{
x -= other.x;
y -= other.y;
z -= other.z;
return *this;
}
// binary ops usually take the left arg by value to take advantage
// of optimizations and because it's easier to write
Coordinate operator+(Coordinate lhs, const Coordinate& rhs)
{
return lhs += rhs;
}
Coordinate operator-(Coordinate lhs, const Coordinate& rhs)
{
return lhs -= rhs;
}
// stream output
std::ostream& operator<<(std::ostream& os, const Coordinate& c)
{
return os << '(' << c.x << ',' << c.y << ',' << c.z << ')';
}
// stream input can be more elaborate
std::istream& operator>>(std::istream& is, Coordinate& c)
{
char open, comma1, comma2, close;
is >> open >> c.x >> comma1 >> c.y >> comma2 >> c.z >> close;
// minimal test for invalid input
if(open != '(' || comma1 != ',' || comma2 != ',' || close != ')')
is.setstate(std::ios::failbit);
return is;
}
You should be able to do the multiplication yourself now