Application of sdut 3-7 class friend Functions
Applications of class 3-7 friend Functions Time Limit: 1000 MS Memory limit: 65536 K Through the exercises in this topic, you can understand the definition and usage of the class membership function. You need to design a Point class, which has two double data members x and y. Design constructor for this class. Add a friend function to calculate and output the distance between two points. Then, add an output member function to output the vertex information. Write the main function to implement the following input and output content. Input
Four double-type numbers, separated by a space in the middle.
Output
There are three rows of output data. The first two rows are used to display the information of the two vertices whose distance is required, and the third row shows the distance between two points.
Sample Input
5 6 2 3
Sample output
The first point is the coordinate:X=5,Y=6The second point is the coordinate:X=2,Y=3The distance between the two points is:4.24264
Prompt
Sample program from Huang Jingjing
# Include
# Include
Using namespace std; class point {private: double x, y; public: // specify the display function as the friend function of the point class friend void display2 (point & t, point & q ); void display1 () {cout <"point is the coordinate: X =" <x <',' <"Y =" <y <endl ;} point (double a, double B) {x = a; y = B ;}}; void display2 (point & t, point & q) // define The friend meta function {// calculate the cout distance between two points using a mathematical formula <"The distance between the two points is:" <sqrt (t. x-q. x) * (t. x-q. x) + (t. y-q. y) * (t. y-q. y) <endl ;}int main () {double a, B, c, d; cin >>a> B> c> d; point x (a, B); point y (c, d); cout <"The first"; x. display1 (); cout <"The second"; y. display1 (); display2 (x, y); return 0 ;}