Some friends let me help him do homework, very simple, just write a class, including a friend function and a static function, I used to understand, but I found it not to write, I didn't write C . Almost no friends and static members, so they can't write, I checked the information, I finally finished, now summed it. I checked online information and a "C / C programming tutorial" (Guo Xiaogang, Jin Xing. People post office, is a good book), but these information must be too shallow, do not have much. I still don't understand it. Just like the school's language class, how to see it is interesting as the novel. Don't say nonsense, I will come here with an example. He is clear. /// # include
Class a {public: static int N; a (); a (int ix, int io); // constructor overloading Void PrintValue (); Friend Void Show (a *); // Declaration show is a friend of A Function, Show can access its private variables. Friend class b; static void showno (); // Static function private: int x; int y;};
Class B {public: void seta (int x, int y) {ax = x; AY = Y;} void geta () {cout << "(" << AX << "," << aY << ") << endl;} private: a a;
Int a :: n;
A :: A (int = = 1) // member function setting default parameter {x = xi; y = = IY; N ;} a :: a () {x = 0; y = 0; n ;}
Void A :: PrintValue () {cout << "(" << x << "," << Y << ")" << endl;}
Void Show (a * a) // defines a function here, which is generally not accessible to A private variables x and y, but declares that you can. {Cout << "(" << A-> X A-> Y << ")" << Endl;}
Void A :: Showno () {cout << "there" << a :: n << "as" << endl;}
Void main () {// C1 C2 is a different object A c1 (2) created with different constructor; // y Using the default value A C2; c1.printValue (); c2.printValue (); show (& c1) Show (& C2); A :: Showno (); B; B.Seta (4, 5); B.Geta ();} / through the VC6. First look at Friends. Friends is just a statement, just better than my statement is my friend, the premise is that there must be a person. Worth with our program is that there is a function void show (a *), then A can then use Friend Void Show (A *) to declare the show as a friend. Then SHOW can access the A-class private variables directly using A-> X. This way, it is understood that the implementation of Show does not need A :: Show (A * a) but use Void Show (A * a). By modifying the procedure, let it cause an error to prove: remove Friend Void Show (a *); it is not a statement to let Show () as his friend, of course, MAIN () can not use A :: Showno () ;. Compiling is wrong, you can't use A-> X. So the key to the friend is: first there is a function f (), then you can declare F () in class A declaration f () is his friend function, then f () can access A private variables. Friends are also so pushing. Look at the static member. Different static variables and normal variables are: 1 is different in memory in memory, it is in a static area. 2 Before use, you must declare the global statement: int A :: n; 3 No matter how many types of objects, their static variables account for the same memory. We now put int a :: n; remove, compiling, Link error, no declaration, there is no such memory. OK! The static function is very simple, he can only use the variable of the Static area.
When you are bored, I will sort him, I don't know if I understand it, my prawn has an empty advice. Outside: I actually found that my friend is this author to give a girl. Domestic friends come to the wife. Seriously despise.