The difference between member function, friend function and general function

Source: Internet
Author: User
Tags add time

Read the program:

#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<


Operation Result:

Enter the code:

/* Copyright (c) 2014, Yantai University School of Computer * All rights reserved.        * File name: Sum123.cpp * Author: Lin Haiyun * Completion Date: April 15, 2015 * Version number: v2.0 * * Problem Description: The difference between this project and an example is that "distance is the distance between a point and another point", and different versions are reflected in the parameters. The three versions suggest separate testing, or, as an example, in a program. * Input Description: No * program output: According to the distance between the required points */#include <iostream> #include <cmath>using namespace Std;class cpoint{private:d  Ouble x;  horizontal double y; Ordinate Public:cpoint (double xx=0,double yy=0): X (xx), Y (yy) {} double Distance1 (CPoint &);    Parameters with const CPoint & better friend Double Distance2 (CPoint & CPoint &);    Double GetX () {return x;    } double GetY () {return y;    }};d ouble Distance3 (CPoint &,cpoint &);d ouble CPoint::d istance1 (CPoint &p) {double dx,dy,d;    dx=x-p.x;    DY=Y-P.Y;    D=SQRT (Dx*dx+dy*dy); return D;}    Double Distance2 (CPoint &p1, CPoint &p2)//Note This item does not have CPoint, the function of the friend class does not add the predecessor {double dx,dy,d;    dx=p1.x-p2.x;    DY=P1.Y-P2.Y;    D=SQRT (Dx*dx+dy*dy); return D;} Double DistanCe3 (CPoint &p1,cpoint &p2) {double dx,dy,d;    Dx=p1.getx ()-p2.getx ();    Dy=p1.getx ()-p2.getx ();    D=SQRT (Dx*dx+dy*dy); return D;}    int main () {CPoint P1 (5,8), p2 ( -5,10);  cout<< "1.    "<<p1.distance1 (p2) <<endl;  cout<< "2.    "<<distance2 (P1,P2) <<endl;  cout<< "3.    "<<distance3 (P1,P2) <<endl; return 0;}


Operation Result:

The difference between member function, friend function and 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.