7-2 friend functions (time class)

Source: Internet
Author: User
Tags gety

/*
* Copyright and version Declaration of the program
* Copyright (c) 2011, a student from the computer College of Yantai University
* All Rights Reserved.
* File Name:
* Author: Zhang chuanxin
* Completion date: January 1, April 02, 2012
* Version: 1.0

* Description of tasks and Solutions
* Input description:
* Problem description: differences between using member functions, friend functions, and general functions
* Program output:
* Problem Analysis :......
* Algorithm Design :......
*/

# Include <iostream> # include <cmath> using namespace STD; Class cpoint {PRIVATE: Double X; // abscissa Double Y; // ordinate public: cpoint (double xx = 1, double YY = 2): X (XX), y (yy) {} double distance1 (cpoint P, cpoint Q); // the distance between two points: Friend double distance2 (cpoint P, cpoint Q); // you can declare int getx () {return X;} int Gety () {return y;} void input (); // use X, Y form input coordinate point void output (); // output coordinate point in the form of (x, y)}; double distane3 (cpoint P, CP Oint Q); // General function declaration void main () {cpoint A1; cpoint A2; a1.input (); a1.output (); a2.input (); a2.output (); a1.distance1 (A1, a2); distance2 (a1, a2); distane3 (a1, a2); System ("pause");} void cpoint: input () {char; cout <"Enter coordinate points in the form of" X, Y ":" <Endl; CIN> x> A> Y; if (! = ',') {Exit (0) ;}// output coordinate point void cpoint: output () in the form of (x, y () {cout <"(" <x <"," <Y <")" <Endl ;}// distance between two points (one point is the current point, another point is the parameter P) Double cpoint: distance1 (cpoint P, cpoint q) // Implementation of the member function, before distance1 add cpoint :: {cout <"the distance between two points is:" <Endl; cout <SQRT (Q. x-p.x) * (Q. x-p.x) + (Q. y-p.y) * (Q. y-p.y) <Endl; return 0;} double distance2 (cpoint P, cpoint q) // implementation of the youyuan function, distance2 without cpoint ::, it is not a member function of the class {cout <"the distance between two points is:" <Endl; cout <SQRT (Q. x-p.x) * (Q. x-p.x) + (Q. y-p.y) * (Q. y-p.y) <Endl; return 0;} double distan2010( cpoint P, cpoint q) // only public interface Q can be used. getx () access private data member {cout <"two points:" <Endl; cout <SQRT (Q. getx ()-P. getx () * (Q. getx ()-P. getx () + (Q. gety ()-P. gety () * (Q. gety ()-P. gety () <Endl; return 0 ;}

Running result:

Experience Accumulation:
1. In the three versions, finding the distance between two points makes my use of these three functions clearer.

Computer comments: the friend function makes the relationship between two "persons" closer...

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.