Youyuan, youyuan
Zookeeper
1. youyuan: C ++ controls the access to the private part of the class object, but sometimes it needs to be a private member of the class's external member class. In this case, C ++ provides a youyuan mechanism.
To create a friend function:
A: place the function declaration in the class declaration, and add the keyword friend before the prototype.
Function Name (parameter list) returned by friend );
Friend classclassname;
B: Write a friend function definition. You do not need to use the keyword "friend" in the definition.
Friend int youyuan function name (const classname & );
# Include <iostream>
Usingnamespacestd;
Classdemo
{
Private:
Inti; // can only be accessed within the class
Public:
Demo () // Constructor
{
Cout <"demo" <this <endl;
}
Demo (constdemo & it) // copy Structure
{
// Once you have implemented the copy constructor, You need to assign values between class members. The Compiler does not care.
This-> I = it. I;
Cout <"copydemo" <this <endl;
}
~ Demo ()
{
Cout <"~ Demo "<this <endl;
}
Voidset_ I (inti)
{
This-> I = I;
}
Intget_ I ()
{
Returni;
}
// Declares a friend function of this class. This function can call the information of this class.
Friendvoidtest ();
// Declares a friend of this class. The information of this class can be called by the following class.
Friendclassdemo1;
// Demo and demo0 are mutual friends
Friendclassdemo0;
};
Voidtest ()
{
// Because the above
Demod;
// Here, d. I is the private variable in the demo. Because youyuan is used, test can be used.
D. I = 100;
Printf ("% d \ n", d. I );
}
Classdemo0
{
Private:
Inti;
Public:
Demo0 ()
{
Demod;
D. I = 123;
// 123 is printed, indicating that the class in youyuan can contain the variable in the category class.
Printf ("% d \ n", d. I );
}
};
Classdemo1
{
Public:
Demo1 ()
{
Demod;
D. I = 144;
Printf ("% d \ n", d. I );
}
Friendclassdemo2;
};
// The following example shows that a friend of mine is not a friend of mine.
Classdemo2
{
Public:
Demo2 ()
{
Demod;
// This sentence cannot be opened. If it is released, an error is returned.
// D. I = 155;
}
};
Intmain (void)
{
Test ();
Demo0d0;
Demo1d1;
// An error is reported during the following operation, indicating a feature of youyuan: My friends are not my friends.
// Demo2d2;
Return0;
}
2. Run:
3. youyuan relationship description
How do I use the friend functions?
You can use the private member variable and member function of an object of the sort class. The Code is as follows:
Class TestClass
{
Private:
Int length;
Int width;
Public:
TestClass (int len, int wide) {length = len; width = wide ;};
Friend int CaculateArea (TestClass test); // Add the keyword friend.
Friend int CaculateCircle (TestClass test );
};
Int CaculateArea (TestClass test) // you can access the private member variable length and width of the test object.
{
Return test. length * test. width;
}
Int CaculateCircle (TestClass test)
{
Return 2 * (test. length + test. width );
}
Int main ()
{
TestClass testClass (2, 3 );
Cout <CaculateArea (testClass) <endl;
Cout <CaculateCircle (testClass) <endl;
Return 0;
}
How do I use the friend functions?
You can use the private member variable and member function of an object of the sort class. The Code is as follows:
Class TestClass
{
Private:
Int length;
Int width;
Public:
TestClass (int len, int wide) {length = len; width = wide ;};
Friend int CaculateArea (TestClass test); // Add the keyword friend.
Friend int CaculateCircle (TestClass test );
};
Int CaculateArea (TestClass test) // you can access the private member variable length and width of the test object.
{
Return test. length * test. width;
}
Int CaculateCircle (TestClass test)
{
Return 2 * (test. length + test. width );
}
Int main ()
{
TestClass testClass (2, 3 );
Cout <CaculateArea (testClass) <endl;
Cout <CaculateCircle (testClass) <endl;
Return 0;
}