A<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FClass%2Ffriend_function</id>
		<title>C++ Tutorial/Class/friend function - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://cppe.ru/index.php?action=history&amp;feed=atom&amp;title=C%2B%2B_Tutorial%2FClass%2Ffriend_function"/>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/friend_function&amp;action=history"/>
		<updated>2026-04-10T22:14:04Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/friend_function&amp;diff=2329&amp;oldid=prev</id>
		<title> в 14:21, 25 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/friend_function&amp;diff=2329&amp;oldid=prev"/>
				<updated>2010-05-25T14:21:17Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 14:21, 25 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/friend_function&amp;diff=2330&amp;oldid=prev</id>
		<title>Admin: 1 версия:&amp;#32;Импорт контента...</title>
		<link rel="alternate" type="text/html" href="http://cppe.ru/index.php?title=C%2B%2B_Tutorial/Class/friend_function&amp;diff=2330&amp;oldid=prev"/>
				<updated>2010-05-25T10:29:28Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия: Импорт контента...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==A friend function==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class MyClass { &lt;br /&gt;
  int a, b; &lt;br /&gt;
public: &lt;br /&gt;
  MyClass(int i, int j) { a=i; b=j; } &lt;br /&gt;
  friend int friendFunction(MyClass x); // a friend function &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
// friendFunction() is a not a member function of any class. &lt;br /&gt;
int friendFunction(MyClass x) &lt;br /&gt;
{ &lt;br /&gt;
  /* Because friendFunction() is a friend of MyClass, it can &lt;br /&gt;
     directly access a and b. */ &lt;br /&gt;
  int max = x.a &amp;lt; x.b ? x.a : x.b; &lt;br /&gt;
 &lt;br /&gt;
  return max; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  MyClass n(18, 111); &lt;br /&gt;
 &lt;br /&gt;
  cout &amp;lt;&amp;lt; &amp;quot;friendFunction(n) is &amp;quot; &amp;lt;&amp;lt; friendFunction(n) &amp;lt;&amp;lt; &amp;quot;\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;friendFunction(n) is 18&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==A function can be a member of one class and a friend of another==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class Cylinder; // a forward declaration &lt;br /&gt;
 &lt;br /&gt;
enum colors { red, green, yellow }; &lt;br /&gt;
 &lt;br /&gt;
class Cube { &lt;br /&gt;
 colors color; &lt;br /&gt;
public: &lt;br /&gt;
  Cube(colors c) { color= c; } &lt;br /&gt;
  bool sameColor(Cylinder y); &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
class Cylinder { &lt;br /&gt;
 colors color; &lt;br /&gt;
public: &lt;br /&gt;
  Cylinder(colors c) { color = c; } &lt;br /&gt;
  friend bool Cube::sameColor(Cylinder y); &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
bool Cube::sameColor(Cylinder y) { &lt;br /&gt;
  if(color == y.color) &lt;br /&gt;
    return true; &lt;br /&gt;
  else &lt;br /&gt;
    return false; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  Cube cube1(red); &lt;br /&gt;
  Cube cube2(green); &lt;br /&gt;
  Cylinder cyl(green); &lt;br /&gt;
 &lt;br /&gt;
  if(cube1.sameColor(cyl)) &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube1 and cyl are the same color.\n&amp;quot;; &lt;br /&gt;
  else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube1 and cyl are different colors.\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  if(cube2.sameColor(cyl)) &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube2 and cyl are the same color.\n&amp;quot;; &lt;br /&gt;
  else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube2 and cyl are different colors.\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;cube1 and cyl are different colors.&lt;br /&gt;
cube2 and cyl are the same color.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Friend functions and operator overloading==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
#include &amp;lt;string&amp;gt;&lt;br /&gt;
using namespace std;&lt;br /&gt;
class MyClass&lt;br /&gt;
{&lt;br /&gt;
    friend void Peek(const MyClass&amp;amp; aMyClass);&lt;br /&gt;
    friend ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const MyClass&amp;amp; aMyClass);&lt;br /&gt;
public:&lt;br /&gt;
    MyClass(const string&amp;amp; name = &amp;quot;&amp;quot;);&lt;br /&gt;
private:&lt;br /&gt;
    string m_Name;&lt;br /&gt;
};&lt;br /&gt;
MyClass::MyClass(const string&amp;amp; name):&lt;br /&gt;
    m_Name(name)&lt;br /&gt;
{}&lt;br /&gt;
void Peek(const MyClass&amp;amp; aMyClass);&lt;br /&gt;
ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const MyClass&amp;amp; aMyClass);&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    MyClass myObject(&amp;quot;A&amp;quot;);&lt;br /&gt;
    Peek(myObject);&lt;br /&gt;
    cout &amp;lt;&amp;lt; myObject;&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
void Peek(const MyClass&amp;amp; aMyClass)&lt;br /&gt;
{&lt;br /&gt;
    cout &amp;lt;&amp;lt; aMyClass.m_Name &amp;lt;&amp;lt; endl;&lt;br /&gt;
}&lt;br /&gt;
ostream&amp;amp; operator&amp;lt;&amp;lt;(ostream&amp;amp; os, const MyClass&amp;amp; aMyClass)   &lt;br /&gt;
{&lt;br /&gt;
    os &amp;lt;&amp;lt; &amp;quot;MyClass Object - &amp;quot;;&lt;br /&gt;
    os &amp;lt;&amp;lt; &amp;quot;m_Name: &amp;quot; &amp;lt;&amp;lt; aMyClass.m_Name;&lt;br /&gt;
    return os;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Friend functions can access private members of a class==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;&lt;br /&gt;
using std::cout;&lt;br /&gt;
using std::endl;&lt;br /&gt;
class MyClass &lt;br /&gt;
{&lt;br /&gt;
   friend void setX( MyClass &amp;amp;, int ); // friend declaration&lt;br /&gt;
public:&lt;br /&gt;
   MyClass() : x( 0 )&lt;br /&gt;
   { &lt;br /&gt;
   }&lt;br /&gt;
   void print() const       &lt;br /&gt;
   { &lt;br /&gt;
      cout &amp;lt;&amp;lt; x &amp;lt;&amp;lt; endl; &lt;br /&gt;
   }&lt;br /&gt;
private:&lt;br /&gt;
   int x;&lt;br /&gt;
};&lt;br /&gt;
void setX( MyClass &amp;amp;c, int val )&lt;br /&gt;
{&lt;br /&gt;
   c.x = val;&lt;br /&gt;
} &lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
   MyClass counter;&lt;br /&gt;
   counter.print();&lt;br /&gt;
   setX( counter, 8 );&lt;br /&gt;
   counter.print();&lt;br /&gt;
   return 0;&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;0&lt;br /&gt;
8&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Friend functions can be shared by two or more classes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt; &lt;br /&gt;
using namespace std; &lt;br /&gt;
 &lt;br /&gt;
class Cylinder; // a forward declaration &lt;br /&gt;
 &lt;br /&gt;
enum colors { red, green, yellow }; &lt;br /&gt;
 &lt;br /&gt;
class Cube { &lt;br /&gt;
 colors color; &lt;br /&gt;
public: &lt;br /&gt;
  Cube(colors c) { color = c; } &lt;br /&gt;
  friend bool sameColor(Cube x, Cylinder y); &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
class Cylinder { &lt;br /&gt;
 colors color; &lt;br /&gt;
public: &lt;br /&gt;
  Cylinder(colors c) { color= c; } &lt;br /&gt;
  friend bool sameColor(Cube x, Cylinder y); &lt;br /&gt;
}; &lt;br /&gt;
 &lt;br /&gt;
bool sameColor(Cube x, Cylinder y) &lt;br /&gt;
{ &lt;br /&gt;
  if(x.color == y.color) &lt;br /&gt;
     return true; &lt;br /&gt;
  else &lt;br /&gt;
     return false; &lt;br /&gt;
} &lt;br /&gt;
 &lt;br /&gt;
int main() &lt;br /&gt;
{ &lt;br /&gt;
  Cube cube1(red); &lt;br /&gt;
  Cube cube2(green); &lt;br /&gt;
  Cylinder cyl(green); &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
  if(sameColor(cube1, cyl)) &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube1 and cyl are the same color.\n&amp;quot;; &lt;br /&gt;
  else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube1 and cyl are different colors.\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  if(sameColor(cube2, cyl)) &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube2 and cyl are the same color.\n&amp;quot;; &lt;br /&gt;
  else &lt;br /&gt;
    cout &amp;lt;&amp;lt; &amp;quot;cube2 and cyl are different colors.\n&amp;quot;; &lt;br /&gt;
 &lt;br /&gt;
  return 0; &lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;cube1 and cyl are different colors.&lt;br /&gt;
cube2 and cyl are the same color.&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==friend square() function for Distance==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include &amp;lt;iostream&amp;gt;  &lt;br /&gt;
  using namespace std;  &lt;br /&gt;
  class Distance{  &lt;br /&gt;
     private:  &lt;br /&gt;
        int feet;  &lt;br /&gt;
        float inches;  &lt;br /&gt;
     public:  &lt;br /&gt;
        Distance() : feet(0), inches(0.0){  }  &lt;br /&gt;
                                         &lt;br /&gt;
        Distance(int ft, float in) : feet(ft), inches(in){  }  &lt;br /&gt;
        void showdist(){ &lt;br /&gt;
           cout &amp;lt;&amp;lt; feet &amp;lt;&amp;lt; &amp;quot;\&amp;quot;-&amp;quot; &amp;lt;&amp;lt; inches &amp;lt;&amp;lt; &amp;quot;\&amp;quot;&amp;quot;; &lt;br /&gt;
        }  &lt;br /&gt;
        friend float square(Distance);    &lt;br /&gt;
  };  &lt;br /&gt;
  float square(Distance d){&lt;br /&gt;
     float fltfeet = d.feet + d.inches/12;  &lt;br /&gt;
     float feetsqrd = fltfeet * fltfeet;    &lt;br /&gt;
     return feetsqrd;               &lt;br /&gt;
    }  &lt;br /&gt;
  int main(){  &lt;br /&gt;
     Distance dist(3, 6.0);&lt;br /&gt;
     float sqft;  &lt;br /&gt;
    &lt;br /&gt;
     sqft = square(dist);  &lt;br /&gt;
     dist.showdist();  &lt;br /&gt;
     cout &amp;lt;&amp;lt; sqft;&lt;br /&gt;
     return 0;  &lt;br /&gt;
  }&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Share friend function between classes==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
&amp;lt;source lang=&amp;quot;cpp&amp;quot;&amp;gt;#include&amp;lt;iostream.h&amp;gt;&lt;br /&gt;
class MyClassB;&lt;br /&gt;
class MyClassA&lt;br /&gt;
{&lt;br /&gt;
  char *name;&lt;br /&gt;
public:&lt;br /&gt;
         MyClassA(char *s){name=s;}&lt;br /&gt;
       friend void print(MyClassA &amp;amp;,MyClassB &amp;amp;);&lt;br /&gt;
};&lt;br /&gt;
class MyClassB&lt;br /&gt;
{&lt;br /&gt;
  char *name;&lt;br /&gt;
public:&lt;br /&gt;
       MyClassB(char *s){name=s;}&lt;br /&gt;
       friend void print(MyClassA &amp;amp;,MyClassB &amp;amp;);&lt;br /&gt;
};&lt;br /&gt;
void print(MyClassA &amp;amp;a,MyClassB &amp;amp;b)&lt;br /&gt;
{&lt;br /&gt;
  cout&amp;lt;&amp;lt;&amp;quot;the MyClassA is&amp;quot;&amp;lt;&amp;lt;a.name&amp;lt;&amp;lt;endl;&lt;br /&gt;
  cout&amp;lt;&amp;lt;&amp;quot;the MyClassB is&amp;quot;&amp;lt;&amp;lt;b.name&amp;lt;&amp;lt;endl;&lt;br /&gt;
}&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
       MyClassA s(&amp;quot;Li Hu&amp;quot;);&lt;br /&gt;
       MyClassB t(&amp;quot;Wan Ping&amp;quot;);&lt;br /&gt;
       print(s,t);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;pre class=codeResult&amp;gt;the MyClassA isLi Hu&lt;br /&gt;
the MyClassB isWan Ping&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>