"Item 1-member function, friend function and general function are different" references
(1) Read the following procedure and experience the instructions in the gaze (to execute the program, please find the course homepage and copy the Code)
//Example: Using the difference between a member function, a friend function, and a general function#include <iostream>using namespace STD;classtime{ Public: Time (intHintMints): Hour (h), Minute (m), SEC (s) {}voidDisplay1 ();//display1 is a member function friend voidDisplay2 (time &);//display2 is a friend function intGethour () {returnhour;}intGetminute () {returnminute;}intGetsec () {returnSEC;}Private:intHourintMinuteintsec;};voidTime::d isplay1 ()//member function Display1 implementation, dispaly1 before add time::{//Direct access to private data members in hour form, essentially this->hour form cout<":"<<minute<<":"<<sec<<endl;}voidDisplay2 (Time &t)//friend function Dispaly2 implementation, no time::, friend is not a member of the class{//Although not a member function of the class. Can directly access private data members in the form of T.hour-this is friend cout<<t.hour<<":"<<t.minute<<":"<<t.sec<<endl;}voidDisplay3 (Time &t)//display3 is a general function that does not add time before Dispaly3:{//cannot be directly interviewed. Only access private data members in the form of public Interface T.gethour () cout<<t.gethour () <<":"<<t.getminute () <<":"<<t.getsec () <<endl;}intMain () {Time T1 (Ten, -, About); T1.display1 ();The //member function calls: Object name. function name ()Display2 (t1);//The Call of the friend function is the same as the general function (but can be different in implementation)Display3 (t1);//General function call return 0;}
(2) Imitate the above demonstration sample, complete the task of finding the distance in the point class. You need to implement the three version number of the distance function: The function of the distance between two points is obtained by using the member function, the friend function and the general function respectively, and the main () function is designed to test.
Tip: The difference between this project and the sample is that "distance is a point and a distance from another point", and the different version numbers are reflected in the number of references. The three version numbers suggest a separate test, also can be shown as a sample, put in a program complete.
The following is a partial code for a point class.
class CPoint {private: double x; // 横坐标 double y; // 纵坐标 public: CPoint(double xx=0,double yy=0):x(xx),y(yy){} ……//请继续写须要的代码};
"Project 2-friend class" references
Define the member functions of the following two classes (in order to experience a friend class, this example is not necessarily a good design.) Merge two classes into one datetime, with better dates and times
Class Date;//Advance Reference declaration for the date classClass time{ Public: Time(int,int,int);voidAdd_a_second (Date &);//Add 1 seconds, 1 seconds later may be next day, but next month, next year voidDisplay (Date &);//Display time, Format: Month/day/year: minute: SecondsPrivate:intHourintMinuteintsec;}; Class date{ Public:Date(int,int,int); Friend class time;//time is a friend of datePrivate:intMonthintDayintyear;};intMain () {Time T1 ( at, -, +); Date D1 ( A, to, -);//When testing. Try again on date D1 (2,28,2013) for(intI=0; i<= -; i++) {t1.add_a_second (D1); T1.display (D1); }return 0;}//The following defines the member functions in two classes. Require no more member functions to be added//Notice the ability to invoke a private data member of the date class in a member function of time
"Item 3-Procedures for organizing multiple classes with multiple files" references
The previous week, "Project 2-the game character with weapons" is implemented as "multiple files of a project," where the declarations of two classes are placed in a. h file, each of which has a separate file for each class member function. The main () function uses a file. Appreciate the advantages of such an arrangement.
Level 2015 C + + 7th Week Project friend, shared data protection, multi-file structure