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