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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include<iostream>
#include "Coordinate.h"
using namespace std;
Coordinate::Coordinate (double, double, double)
{
double x = 0.0;
double y = 0.0;
double z = 0.0;
}
Coordinate Coordinate::operator+(const Coordinate & a)const
{
return Coordinate(x + a.x , y + a.y, z + a.z);
}
Coordinate Coordinate::operator-(const Coordinate& a)const
{
return Coordinate(x - a.x, y - a.y, z - a.z);
}
Coordinate Coordinate::operator*(const Coordinate& a)const
{
return Coordinate(x * a.x, y * a.y, z * a.z);
}
Coordinate Coordinate::operator=(const Coordinate& a)const
{
x = a.Coordinate;
y = a.Coordinate;
z = a.Coordinate;
return *this;
}
bool Coordinate::operator==(const Coordinate& a)const
{
return true;
}
bool Coordinate::operator!=(const Coordinate& a)const
{
return true; // can return (!(*this)==a)
}
ostream &operator<<(ostream &output, const Coordinate& a)
{
output << endl;
return output;
}
istream &operator>>(istream &input, Coordinate& a)
{
return input;
}
#include<iostream>
using namespace std;
#ifndef COORDINATE_H
#define COORDINATE_H
class Coordinate
{
private:
double x, y, z;
public:
friend ostream &operator<<( ostream &, const Coordinate & );
friend istream &operator>>( istream &, Coordinate &);
Coordinate (double a = 0.0, double b = 0.0, double c= 0.0);
Coordinate operator+(const Coordinate & a) const;
Coordinate operator-(const Coordinate&) const;
Coordinate operator*(const Coordinate&) const;
Coordinate operator=(const Coordinate&) const;
bool operator==(const Coordinate&) const;
bool operator!=(const Coordinate&) const;
};
#endif
#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';
}
}
|