友元:友元是一種定義在類外部的普通函數(shù),它需要在類體內(nèi)進行說明,在說明時,前面加上 friend 。友元不是成員函數(shù),但可以訪問類的私有成員。目的在于提高函數(shù)的運行效率。但是設(shè)置友元是以破壞封裝性為代價的。
1 、友元函數(shù):
友元函數(shù)不是類的成員函數(shù),但是友元函數(shù)可以訪問該類的私有成員。
例 3 :分析下列程序的輸出結(jié)果:
#include
#include
class point
{
public:
point(doube xx,double yy)
{
x=xx;
y=yy;
}
void Getxy();
friend double Distance(point &a,point &b);
private:
double x,y;
};
void point::Getxy()
{
cout<<”(“< }
double Distance(point &a,point &b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
void main()
{
point p1(3.0,4.4),p2(6.0,8.0);
p1.Getxy();
p2.Getxy();
double d=Distance(p1,p2);
cout<<”Distance is”< }
運行結(jié)果:
(3.0,4.0)
(6.0,8.0)
Distance is 5
Point 類中說明了一個友元函數(shù) Distance() ,它可以 point 類的私有成員。該程序的功能是已知兩點坐標(biāo),求兩點間距。
例 4 :分析下列程序的輸出結(jié)果:
#include
class Time
{
public:
Time(int new_hours,int new_minutes)
{
hours=new_hours;
minutes=new_minutes;
}
friend void Time12(Time time);
friend void Time24(Time time);
private:
int hours,minutes;
};
void Time12(Time time)
{
if(time.hours>12)
{
time.hours-=12;
cout< }
else
cout< }
void Time24(Time time)
{
cout< }
void main()
{
Time Time1(20,30),Time2(10,45);
Time12(Time1);
Time24(Time1);
Time12(Time2);
Time24(Time2);
}
運行結(jié)果:
8:30PM
20:30
10:45AM
10:45
2、友元類:當(dāng)一個類作為另一個類的友元時,這個類的所有成員函數(shù)都是另一個類的友元函數(shù)。
例 5 :分析下列的輸出結(jié)果:
#include
class X
{
friend class Y;
public:
void Set(int I)
{
X=I;
}
void Display()
{
cout<<”x=”< cout<<”y=”< }
public:
int x;
static int y;
};
class Y
{
public:
Y(int I,int j);
Void Display();
Private:
X a;
};
int X::y=1;
Y::Y(int I,int j)
{
a.x=I;
X::y=j;
}
void Y::Display()
{
cout<<”x=”<
cout<<”y=”< }
void main()
{
X b;
b.Set(5);
b.Display();
Y c(6,9);
c.Display();
b.Display();
}
執(zhí)行結(jié)果:
x=5,y=1
x=6,y=9
x=5,y=9
例 6 :編寫一個類,聲明一個數(shù)據(jù)成員和一個靜態(tài)數(shù)據(jù)成員。讓構(gòu)造函數(shù)初始化數(shù)據(jù)成員,并把靜態(tài)數(shù)據(jù)成員加1,讓析構(gòu)函數(shù)把靜態(tài)數(shù)據(jù)成員減1。然后創(chuàng)建三個對象,顯示它們的數(shù)據(jù)成員和靜態(tài)數(shù)據(jù)成員。
#include
class example
{
public:
example(int num)
{
B=num;
A++;
}
void Display()
{
cout<<"B="< }
~example()
{
A--;
cout<<"A="<
}
private:
int B;
static int A;
};
int example::A=0;
void main()
{
example a(20),b(30),c(40);
a.Display();
b.Display();
c.Display();
}
運行結(jié)果:
B=20,A=3
B=30,A=3
B=40,A=3
A=2
A=1
A=0
例 7 :分析下列的程序的運行結(jié)果:
#include
class cat;
class dog
{
public:
dog(int i)
{
weight=i;
}
friend int sum(int total,cat&a1,dog&a2);
protected:
int weight;
};
class cat
{
public:
cat(int j)
{
weight=j;
}
friend int sum(int total,cat&a1,dog&a2);
protected:
int weight;
};
int sum(int total,cat&a1,dog&a2)
{
return total+a1.weight+a2.weight;
}
void main()
{
cat b1(24);
dog b2(20);
int total=0;
cout< }
運行結(jié)果為:
44
這里, sum() 是 cat 類和 dog 類的友元函數(shù),可以通過對象訪問類的私有數(shù)據(jù)成員 weight 。
1 、友元函數(shù):
友元函數(shù)不是類的成員函數(shù),但是友元函數(shù)可以訪問該類的私有成員。
例 3 :分析下列程序的輸出結(jié)果:
#include
#include
class point
{
public:
point(doube xx,double yy)
{
x=xx;
y=yy;
}
void Getxy();
friend double Distance(point &a,point &b);
private:
double x,y;
};
void point::Getxy()
{
cout<<”(“<
double Distance(point &a,point &b)
{
double dx=a.x-b.x;
double dy=a.y-b.y;
return sqrt(dx*dx+dy*dy);
}
void main()
{
point p1(3.0,4.4),p2(6.0,8.0);
p1.Getxy();
p2.Getxy();
double d=Distance(p1,p2);
cout<<”Distance is”<
運行結(jié)果:
(3.0,4.0)
(6.0,8.0)
Distance is 5
Point 類中說明了一個友元函數(shù) Distance() ,它可以 point 類的私有成員。該程序的功能是已知兩點坐標(biāo),求兩點間距。
例 4 :分析下列程序的輸出結(jié)果:
#include
class Time
{
public:
Time(int new_hours,int new_minutes)
{
hours=new_hours;
minutes=new_minutes;
}
friend void Time12(Time time);
friend void Time24(Time time);
private:
int hours,minutes;
};
void Time12(Time time)
{
if(time.hours>12)
{
time.hours-=12;
cout<
else
cout<
void Time24(Time time)
{
cout<
void main()
{
Time Time1(20,30),Time2(10,45);
Time12(Time1);
Time24(Time1);
Time12(Time2);
Time24(Time2);
}
運行結(jié)果:
8:30PM
20:30
10:45AM
10:45
2、友元類:當(dāng)一個類作為另一個類的友元時,這個類的所有成員函數(shù)都是另一個類的友元函數(shù)。
例 5 :分析下列的輸出結(jié)果:
#include
class X
{
friend class Y;
public:
void Set(int I)
{
X=I;
}
void Display()
{
cout<<”x=”<
public:
int x;
static int y;
};
class Y
{
public:
Y(int I,int j);
Void Display();
Private:
X a;
};
int X::y=1;
Y::Y(int I,int j)
{
a.x=I;
X::y=j;
}
void Y::Display()
{
cout<<”x=”<
cout<<”y=”<
void main()
{
X b;
b.Set(5);
b.Display();
Y c(6,9);
c.Display();
b.Display();
}
執(zhí)行結(jié)果:
x=5,y=1
x=6,y=9
x=5,y=9
例 6 :編寫一個類,聲明一個數(shù)據(jù)成員和一個靜態(tài)數(shù)據(jù)成員。讓構(gòu)造函數(shù)初始化數(shù)據(jù)成員,并把靜態(tài)數(shù)據(jù)成員加1,讓析構(gòu)函數(shù)把靜態(tài)數(shù)據(jù)成員減1。然后創(chuàng)建三個對象,顯示它們的數(shù)據(jù)成員和靜態(tài)數(shù)據(jù)成員。
#include
class example
{
public:
example(int num)
{
B=num;
A++;
}
void Display()
{
cout<<"B="< }
~example()
{
A--;
cout<<"A="<
}
private:
int B;
static int A;
};
int example::A=0;
void main()
{
example a(20),b(30),c(40);
a.Display();
b.Display();
c.Display();
}
運行結(jié)果:
B=20,A=3
B=30,A=3
B=40,A=3
A=2
A=1
A=0
例 7 :分析下列的程序的運行結(jié)果:
#include
class cat;
class dog
{
public:
dog(int i)
{
weight=i;
}
friend int sum(int total,cat&a1,dog&a2);
protected:
int weight;
};
class cat
{
public:
cat(int j)
{
weight=j;
}
friend int sum(int total,cat&a1,dog&a2);
protected:
int weight;
};
int sum(int total,cat&a1,dog&a2)
{
return total+a1.weight+a2.weight;
}
void main()
{
cat b1(24);
dog b2(20);
int total=0;
cout<
運行結(jié)果為:
44
這里, sum() 是 cat 類和 dog 類的友元函數(shù),可以通過對象訪問類的私有數(shù)據(jù)成員 weight 。

