(1) Read the following procedure to understand the instructions in the note.
Example: differences between using member functions, friend functions, and general functions # include <iostream>using namespace Std;class time{public:time (int h,int m,int s): Hour (h) , Minute (m), SEC (s) {} void Display1 (); Display1 is a member function friend void Display2 (time &); Display2 is a friend function int gethour () {return hour;} int Getminute () {return minute;} int getsec () {return sec;} Private:int hour; int minute; int sec;}; void time::d isplay1 ()//member function Display1 implementation, DISPALY1 time::{//Direct access to private data members in hour form, essentially this->hour form cout<(2) Imitate the above example, complete the task of finding the distance in the point class. You need to implement three versions of the distance function: The function of the distance between two points is calculated by using the member function, the friend function and the general function respectively, and the main () function is designed to complete the test.
Tip: The difference between this project and the example is that "distance is a point and a distance from another point", and different versions are reflected in the parameters. The three versions suggest separate testing, or, as an example, in a program.
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Author: Zhao Song * file: demo.cpp* finish: April 22, 2015 * version: v1.0*/#include < Iostream> #include <cmath>using namespace Std;class cpoint{private: double x; Horizontal double y; Ordinate public: CPoint (Double xx=0,double yy=0): X (xx), Y (yy) {} double Distance (CPoint p); void input (); void output ();}; Double CPoint::D istance (CPoint p) { double S; S=sqrt ((p.x-x) * (p.x-x) + (p.y-y) * (p.y-y)); return s;} void Cpoint::input () { char ch; cout<< "Please enter coordinates (x, y):"; Cin>>x>>ch>>y;} void Cpoint::output () { cout<< "(" <<x<< "," <<y<< ")" <<ENDL;} int main () { double D; CPoint p1,p2,p; cout<< "Please enter a 1th point P1,"; P1.input (); cout<< "Please enter a 2nd point P2,"; P2.input (); D=p1. Distance (p2); cout<< "Two points distance:" <<d<<endl; return 0;}
Operation Result:
Sixth week item four-the difference between a member function, a friend function, and a general function