您现在的位置: 中国男护士网 >> 考试频道 >> 计算机等级 >> 二级辅导 >> C十十 >> 辅导 >> 正文    
  二级C++类与对象:友员 【注册男护士专用博客】          

二级C++类与对象:友员

www.nanhushi.com     佚名   不详 

  友元:友元是一种定义在类外部的普通函数,它需要在类体内进行说明,在说明时,前面加上 friend 。友元不是成员函数,但可以访问类的私有成员。目的在于提高函数的运行效率。但是设置友元是以破坏封装性为代价的。

1 、友元函数:

  友元函数不是类的成员函数,但是友元函数可以访问该类的私有成员。

例 3 :分析下列程序的输出结果:

#include<iostream.h>

#include<math.h>

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<<”(“<<x<<”,”<<y<<”)”<<endl;

}

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”<<d<<endl;

}

运行结果:

(3.0,4.0)

(6.0,8.0)

Distance is 5

Point 类中说明了一个友元函数 Distance() ,它可以 point 类的私有成员。该程序的功能是已知两点坐标,求两点间距。

例 4 :分析下列程序的输出结果:

#include<iostream.h>

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<<time.hours<<”:”<<time.minutes<<”PM”<<endl;

}

else

cout<<time.hours<<”:”<<time.minutes<<”AM”<<endl;

}

void Time24(Time time)

{

cout<<time.hours<<”:”<<time.minutes<<endl;

}

void main()

{

Time Time1(20,30),Time2(10,45);

Time12(Time1);

Time24(Time1);

Time12(Time2);

Time24(Time2);

}

运行结果:

8:30PM

20:30

10:45AM

10:45

2、友元类:当一个类作为另一个类的友元时,这个类的所有成员函数都是另一个类的友元函数。

例 5 :分析下列的输出结果:

#include<iostream.h>

class X

{

friend class Y;

public:

void Set(int I)

{

X=I;

}

void Display()

{

cout<<”x=”<<x<<”,”;

cout<<”y=”<<y<<endl;

}

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=”<<a.x<<”,”;

cout<<”y=”<<X::y<<endl;

}

void main()

{

X b;

b.Set(5);

b.Display();

Y c(6,9);

c.Display();

b.Display();

}

执行结果:

x=5,y=1

x=6,y=9

x=5,y=9
例 6 :编写一个类,声明一个数据成员和一个静态数据成员。让构造函数初始化数据成员,并把静态数据成员加1,让析构函数把静态数据成员减1。然后创建三个对象,显示它们的数据成员和静态数据成员。

#include<iostream.h>

class example

{

public:

example(int num)

{

B=num;

A++;

}

void Display()

{

cout<<"B="<<B<<","<<"A="<<A<<endl;

}

~example()

{

A--;

cout<<"A="<<A<<endl;

}

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();

}

运行结果:

B=20,A=3

B=30,A=3

B=40,A=3

A=2

A=1

A=0

例 7 :分析下列的程序的运行结果:

#include<iostream.h>

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<<sum(total,a1,a2);

}

运行结果为:

44

这里, sum() 是 cat 类和 dog 类的友元函数,可以通过对象访问类的私有数据成员 weight 。

 

文章录入:杜斌    责任编辑:杜斌 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
     

    联 系 信 息
    QQ:88236621
    电话:15853773350
    E-Mail:malenurse@163.com
    免费发布招聘信息
    做中国最专业男护士门户网站
    最 新 热 门
    最 新 推 荐
    相 关 文 章
    没有相关文章
    专 题 栏 目