Tuesday, July 2, 2024

Program 2 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 2 Write a Point class that represents a 2-d point in a plane. Write member functions to a. Set and show the value of a point b. Find the distance between two points c. Check whether two points are equal or not

 

BCA

Object Oriented Programming using C++ Practical

Madras University 

Program 2

Write a Point class that represents a 2-d point in a plane. Write member functions to a. Set and show the value of a point b. Find the distance between two points c. Check whether two points are equal or not

SOURCE CODE:
#include <iostream>
#include <cmath>
using namespace std;

class Point {
private:
    double x, y;

public:
    // Constructor
    Point(double x = 0, double y = 0) : x(x), y(y) {}

    // Set the value of the point
    void setPoint(double x, double y) {
        this->x = x;
        this->y = y;
    }

    // Show the value of the point
    void showPoint() const {
        cout << "Point(" << x << ", " << y << ")" << std::endl;
    }

    // Find the distance between two points
    double distanceTo(const Point &other) const {
        return sqrt(std::pow(x - other.x, 2) + std::pow(y - other.y, 2));
    }

    // Check whether two points are equal or not
    bool equals(const Point &other) const {
        return x == other.x && y == other.y;
    }
};

int main() {
    Point p1(1, 2);
    Point p2(4, 6);

    p1.showPoint();
    p2.showPoint();

    cout << "Distance between points: " << p1.distanceTo(p2) << std::endl;
    cout << "Points are equal: " << (p1.equals(p2) ? "Yes" : "No") << std::endl;

    return 0;
}


OUTPUT:

Point(1, 2)
Point(4, 6)
Distance between points: 5
Points are equal: No

No comments:

Post a Comment

Program 12 BCA Madras University BCA Object Oriented Programming using C++ Practical Madras University Program 12 Implement a telephone directory using files

  BCA Object Oriented Programming using C++ Practical Madras University  Program 12  Implement a telephone directory using files SOURCE CODE...