The coordinates of a point are a pair of numbers that define its exact location on a two dimensional plane. Create a class named Coordinate that will represent a two dimensional point. Include two data fields for the x and y axis, a constructor that will require arguments to initialize the data values. Overload the + operator and - operator such that we can add, subtract, coordinates and return doubles, respectively. Write a main() that demonstrates that your class and function work correctly.
#include <iostream>
using namespace std;
class Coordinate{
private:
int x;
int y;
public:
Coordinate(int a,int b){
x=a;
y=b;
}
void add(Coordinate a1, Coordinate a2){
int x1=a1.x;
int x2=a2.x;
int y1=a1.y;
int y2=a2.y;
int m=x1+x2;
int n=y1+y2;
cout<<"Sum= ("<<m<<","<<n<<")\n";
}
void sub(Coordinate a1, Coordinate a2){
int x1=a1.x;
int x2=a2.x;
int y1=a1.y;
int y2=a2.y;
int m=x1-x2;
int n=y1-y2;
cout<<"Sub= ("<<m<<","<<n<<")\n";
}
};
int main()
{
Coordinate c1(7,5);
Coordinate c2(3,4);
c1.add(c1,c2);
c2.sub(c1,c2);
return 0;
}
Comments
Leave a comment