209: Create a function that takes 2 point objects and computes distance between them

 

// Create a function that takes 2 point objects and computes distance between them

#include <iostream>
#include <cmath>
using namespace std;
class Point
{
private:
    int x_coord, y_coord;
    friend float distance(Point, Point);

public:
    Point(int, int);
};
Point ::Point(int x, int y)
{
    x_coord = x;
    y_coord = y;
}

float distance(Point, Point);
int main()
{
    Point p1(3, 4), p2(2, 2);
    cout << "Distance between p1 and p2 is: " << distance(p1, p2) << endl;

    return 0;
}

float distance(Point a, Point b)
{
    return sqrt(pow((b.x_coord - a.x_coord), 2) + pow((b.y_coord - a.y_coord), 2));
}


Comments

Popular posts from this blog

88: Using switch statement Write a C program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: // Percentage >= 90% : Grade A Percentage >= 80% : Grade B Percentage >= 70% : Grade C Percentage >= 60% : Grade D Percentage >= 40% : Grade E Percentage < 40% : Grade F

205: Book Catalog: Define a struct to represent a book with attributes like title, author, and publication year. Write a program to create a catalog of books by taking user input and display books published after a certain year.

15: Take input of age and name of 3 people by user and determine oldest and youngest among them with his age. -_-_-_-_-_-_-_-_-(line with spaces input concept)-_-_-_-_-_-_-_-_