Program 2 – 2D Points Class

A point on the 2-D can be represented by two numbers: an x co-ordinate and a y co-ordinate. For example (4,5) represents a point 4 units to the right of the origin and 5 units up the origin. The sum of two points can be defined as a new point whose x co-ordinate is the sum of x co-ordinates of both points and same for y co-ordinates.Write a program that uses a structure called point to model a point. Define three points, and have the user input values of two of them. Then set the third point equal to the sum of the other two and display the value of the new point.

#include<iostream>
using namespace std;

struct point{
    float x, y;
    point(float x,float y)
    {
        this->x = x;
        this->y = y;
    }
}*p1,*p2,*p3;

point * sum(point *p1,point *p2)
{
    return new point(p1->x+p2->x, p1->y+p2->y);
}

int main()
{
    float x,y;
    cout << "Enter x coodinate of first point";
    cin >> x;
    cout << "Enter y coodinate of first point";
    cin >> y;
    p1 = new point(x,y);
    cout << "Enter x coodinate of second point";
    cin >> x;
    cout << "Enter y coodinate of second point";
    cin >> y;
    p2 = new point(x,y);
    p3 = sum(p1,p2);
    cout << "Third point is :- "<<"("<<p3->x<<","<<p3->y<<")\n";
    return 0;
}

LEAVE A REPLY