Help with overloading operators

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:

Coordinate.h

#include <iostream>
using namespace std;

class Coordinate
{
public:
Coordinate();
Coordinate(double,double, double);
void setCoordinate(double newX,double newY,double newZ);
const Coordinate add (Coordinate otherCoordinate);
const Coordinate subtract (Coordinate otherCoordinate);
Coordinate printCoordinate();

private:
double x,y,z;
};

Coordinate.cpp

#include "Coordinate.h"

Coordinate::Coordinate()
{
x=0.0;
y=0.0;
z=0.0;
}

Coordinate::Coordinate(double newX,double newY,double newZ)
{
x=newX;
y=newY;
z=newZ;
}

void Coordinate::setCoordinate(double newX,double newY,double newZ)
{
x=newX;
y=newY;
z=newZ;
}

const Coordinate Coordinate::add (Coordinate p)//const goes before not after
{
Coordinate z;
z.x=x+p.x;
z.y=y+p.y;

return z;
}

const Coordinate Coordinate::subtract (Coordinate p) //const goes before not after
{
Coordinate z;
z.x=x-p.x;
z.y=y-p.y;

return z;
}

Coordinate Coordinate::printCoordinate()
{
cout<<"("<<x<<","<<y<<","<<z<<")";
}

and pa9.cp

#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
There is nothing to it, just remember a few basic rules:

header:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
// never put a 'using namespace' in a header file
class Coordinate
{
public:
 Coordinate();
 Coordinate(double,double, double);
 // compound assignment ops make binary ops easy to write
 Coordinate& operator+=(const Coordinate&);
 Coordinate& operator-=(const Coordinate&);
 // some ops need to access the internals
 friend bool operator==(const Coordinate&, const Coordinate&);
 friend std::ostream& operator<<(std::ostream&, const Coordinate&);
 friend std::istream& operator>>(std::istream&, Coordinate&);
private:
 double x,y,z;
};
// binary ops are non-members
Coordinate operator+(Coordinate lhs, const Coordinate& rhs);
Coordinate operator-(Coordinate lhs, const Coordinate& rhs);
bool operator==(const Coordinate& lhs, const Coordinate& rhs);


source:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
}

bool operator==(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 <
bool operator!=(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
Getting people online to do your homework. REAL classy and not un- ethical in any way.
Oh, and they didn't do it how Temple wants it. Use this code, and the grader will be able to tell you copied from somewhere.
im not using if for this "Temple" person, and i was asking how to use it correctly, which is exactly what he showed me.
Topic archived. No new replies allowed.