Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7724449
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-03-30 14:23:39

  1. /****************************************************
  2. * 文件名:
  3. * 功能:友元成员简单应用
  4. * 说明:友元提供了不同类的成员函数之间、
  5.         类的成员函数与一般函数之间进行数据共享的机制
  6. * 时间:2011-3-30 --Lzy
  7. ****************************************************/

  8. //普通函数作为友元函数

  9. #include <iostream.h>
  10. #include <math.h>

  11. const double PI = 3.141593;        //定义常量


  12. class Circle
  13. {
  14. private:
  15.     double x, y,r;

  16. public:
  17.     Circle(double xx = 0, double yy = 0, double rr = 0)
  18.     {
  19.         x = xx; y = yy; r = rr;
  20.     }

  21.     void display()
  22.     {
  23.         cout<<"中点"<<"("<<x<<" ,"<<y<<")"<<endl;
  24.         cout<<"半径"<<r<<endl;
  25.         cout<<"面积"<<this->area()<<endl;
  26.         cout<<"周长"<<this->circular()<<endl;
  27.     }
  28.     
  29.     double area(){return PI*r*r;}
  30.     double circular(){return 2*PI*r;}
  31.     friend double distance(Circle &p1, Circle &p2);            //普通函数作友元函数

  32. };

  33. double distance(Circle &p1, Circle &p2)
  34. {
  35.     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
  36. }

  37. void main(void)
  38. {
  39.     Circle c1(1,1,1), c2(3,3,3);
  40.     c1.display();    
  41.     c2.display();
  42.     cout<<distance(c1,c2);
  43. }


  44. //成员函数作为友元函数

  45. #include <iostream.h>
  46. #include <math.h>

  47. const double PI = 3.141593;        //定义常量


  48. class Circle;            //前向声明


  49. class Calculate
  50. {
  51. public:
  52.     double distance(Circle &p1, Circle &p2);    
  53. };

  54. class Circle
  55. {
  56. private:
  57.     double x, y,r;

  58. public:
  59.     Circle(double xx = 0, double yy = 0, double rr = 0)
  60.     {
  61.         x = xx; y = yy; r = rr;
  62.     }

  63.     void display()
  64.     {
  65.         cout<<"中点"<<"("<<x<<" ,"<<y<<")"<<endl;
  66.         cout<<"半径"<<r<<endl;
  67.         cout<<"面积"<<this->area()<<endl;
  68.         cout<<"周长"<<this->circular()<<endl;
  69.     }
  70.     
  71.     double area(){return PI*r*r;}
  72.     double circular(){return 2*PI*r;}
  73.     friend double Calculate::distance(Circle &p1, Circle &p2);            //友元函数

  74. };

  75. double Calculate::distance(Circle &p1, Circle &p2)
  76. {
  77.     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
  78. }

  79. void main(void)
  80. {
  81.     Circle c1(1,1,1), c2(3,3,3);
  82.     Calculate cal;
  83.     c1.display();    
  84.     c2.display();
  85.     cout<<cal.distance(c1,c2)<<endl;
  86. }

  87. //友元类

  88. #include <iostream.h>
  89. #include <math.h>

  90. const double PI = 3.141593;        //定义常量


  91. class Circle;            //前向声明


  92. class Calculate
  93. {
  94. public:
  95.     double distance(Circle &p1, Circle &p2);
  96.     double area(Circle &c);
  97.     double circular(Circle &c);
  98.     void display(Circle &c);
  99. };

  100. class Circle
  101. {

  102. public:
  103.     double x, y,r;
  104.     Circle(double xx = 0, double yy = 0, double rr = 0)
  105.     {
  106.         x = xx; y = yy; r = rr;
  107.     }

  108.     void display()
  109.     {
  110.         cout<<"中点"<<"("<<x<<" ,"<<y<<")"<<endl;
  111.         cout<<"半径"<<r<<endl;    
  112.     }

  113.     friend class Calulate;    
  114.     
  115. };

  116. double Calculate::distance(Circle &p1, Circle &p2)
  117. {
  118.     return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
  119. }

  120. double Calculate::area(Circle &c)
  121. {
  122.     return PI*c.r*c.r;
  123. }

  124. double Calculate::circular(Circle &c)
  125. {
  126.     return PI*c.r*2;
  127. }

  128. void Calculate::display(Circle &c)
  129. {
  130.     cout<<"面积"<<this->area(c)<<endl;
  131.     cout<<"周长"<<this->circular(c)<<endl;
  132. }

  133. void main(void)
  134. {
  135.     Circle c1(1,1,1), c2(3,3,3);
  136.     Calculate cal;
  137.     c1.display();    
  138.     c2.display();
  139.     cout<<cal.distance(c1,c2)<<endl;
  140. }
阅读(1371) | 评论(0) | 转发(2) |
0

上一篇:单继承简单应用

下一篇:多继承简单应用

给主人留下些什么吧!~~