Application of sdut 3-7 class friend Functions

Source: Internet
Author: User
Tags cmath

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 ;}
  
 


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.