Week six project four-the difference between member functions, friend functions, and general functions

Source: Internet
Author: User
Tags add time

(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

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.