Answer to Question #238543 in C++ for Jeanett

Question #238543

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.


1
Expert's answer
2021-09-17T17:54:34-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS