设为首页 加入收藏

TOP

二级C++类与对象:友员
2014-11-19 08:09:10 来源: 作者: 【 】 浏览:24
Tags:二级 对象 友员

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


1 、友元函数:


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


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


#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”<


}


运行结果:


(3.0,4.0)


(6.0,8.0)


Distance is 5


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


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


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


}


运行结果:


8:30PM


20:30


10:45AM


10:45


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


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


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


}


执行结果:


x=5,y=1


x=6,y=9


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


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


}


运行结果:


B=20,A=3


B=30,A=3


B=40,A=3


A=2


A=1


A=0


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


#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<


}


运行结果为:


44


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


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇编程技巧:C++调用函数用法介绍 下一篇二级C++类与对象:静态成员函数

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: