What are the differences between membership functions and membership functions?
Reading procedure:
# 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 the member function friend void display2 (Time &); // display2 is the friend function int getHour () {return hour;} int getMinute () {return minute;} int getSec () {return sec;} private: int hour; int minute; int sec ;}; void Time: display1 () // Implementation of the member function display1. Before dispaly1, add Time ::{// to directly access private data members in the form of hour, the essence is this-> hour form cout
Running result:
Enter the code:
/** Copyright (c) 2014, School of Computer Science, Yantai University * All rights reserved. * file name: sum123.cpp * Prepared by: Lin haiyun * Completion Date: July 15, April 15, 2015 * version No.: v2.0 ** Problem description: the difference between this project and the example is that "distance is the distance between one point and another point". Different versions are represented in parameters. We recommend that you test the three versions separately, as shown in an example. * Input Description: none * program output: distance between required points */# include <iostream> # include <Cmath> using namespace std; class CPoint {private: double x; // abscissa double y; // ordinate public: CPoint (double xx = 0, double yy = 0): x (xx), y (yy) {} double distance1 (CPoint &); // use const CPoint & better friend double distance2 (CPoint &, CPoint &); double getX () {return x ;} double getY () {return y ;}}; double distan2010( CPoint &, CPoint &); double CPoint: distance1 (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 that this item does not have a Cpoint. The functions and defined classes of the youyuan class do not have the pre-name {double dx, dy, d; dx = p1.x-p2.x; dy = p1.y-p2.y; d = sqrt (dx * dx + dy * dy); return d;} double distanpushed (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. "<distan2010( p1, p2) <endl; return 0 ;}
Running result: