16th Week OJ Brush question--problem d:b friend class-calculate distance between two points

Source: Internet
Author: User

Description

Class distance is defined as a friend of class point to implement the ability to calculate distances between two points.

The point class has two private data members X and Y to represent the two coordinates (horizontal and vertical) of the points, and the member functions need to be defined by themselves.

The main program enters the coordinates of two point points to calculate the distance between the two points.

Input

Coordinates of two points (horizontal and vertical)

Output

Distance between two points (two decimal places reserved)

Sample Input
1.0 1.0 2.0 2.0
Sample Output
1.41

/* All rights reserved. * File name: Test.cpp * Chen Dani * Completion date: June 25, 2015 * Version number: v1.0 * * #include <iostream> #include <iomanip>using namespac e std; #include <cmath>class point;class distance{public:    float Dis (Point & P,point & Q);}; Class Point{public: Point    (Double xx,double yy): X (xx), Y (yy) {}    friend Distance;private:    double x;    Double y;}; Float Distance::D is (Point & p,point & q) {    double S;    S=sqrt ((p.x-q.x) * (p.x-q.x) + (P.Y-Q.Y) * (P.Y-Q.Y));    return s;} int main () {    float x1,y1,x2,y2;    cin>>x1>>y1>>x2>>y2;    Point P (x1,y1), Q (x2,y2);    Cout<<setiosflags (ios::fixed);    Cout<<setprecision (2);    Distance D;    Cout<<d.dis (p,q) <<endl;    return 0;}


Learning experience: Very smooth, continue to work!! Brush questions to the end!!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

16th Week OJ Brush question--problem d:b friend class-calculate distance between two points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.