Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370685
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 10:52:38

Friend function:
If we want to declare an external function as friend of a class, thus allowing this function to have access to the private and protected members of this class, we do it by declaring a prototype of this external function within the class, and preceding it with the keyword friend:
It simply has access to its private and protected members without being a member.
  1. #include <iostream>
  2. using namespace std;

  3. class CRectangle {
  4.                 int width, height;
  5.         public:
  6.                 void set_values (int, int);
  7.                 int area () {return (width * height);}
  8.                 friend CRectangle duplate (CRectangle);
  9. };

  10. void
  11. CRectangle::set_values (int a, int b)
  12. {
  13.         width = a;
  14.         height = b;
  15. }

  16. CRectangle
  17. duplate(CRectangle rectparam)
  18. {
  19.         CRectangle rectres;
  20.         rectres.width = rectparam.width;
  21.         rectres.height = rectparam.height;

  22.         return (rectres);
  23. }

  24. int
  25. main(void)
  26. {
  27.         CRectangle rect, rectb;
  28.         rect.set_values(2,3);
  29.         rectb = duplate(rect);
  30.         cout << rectb.area() << endl;

  31.         return (0);
  32. }

Friend class:
  1. #include <iostream>
  2. using namespace std;

  3. class CSquare;

  4. class CRectangle {
  5.                 int width, height;
  6.         public:
  7.                 int area () {return (width * height);}
  8.                 void convert (CSquare a);
  9. };

  10. class CSquare {
  11.         private:
  12.                 int side;
  13.         public:
  14.                 void set_side (int a) {side = a;}
  15.                 friend class CRectangle;
  16. };

  17. void
  18. CRectangle::convert(CSquare a)
  19. {
  20.         width = a.side;
  21.         height = a.side;
  22. }

  23. int
  24. main(void)
  25. {
  26.         CSquare sqr;
  27.         CRectangle rect;
  28.         sqr.set_side(4);
  29.         rect.convert(sqr);
  30.         cout << rect.area() << endl;

  31.         return (0);
  32. }
Consider that friendships are not corresponded if we do not explicitly specify so.
Another property of friendships is that they are not transitive: The friend of a friend is not considered to be a friend unless explicitly specified.

Inheritance:
Format:
  1. class derived_class_name: public base_class_name
  2. { /*...*/ };

  1. #include <iostream>
  2. using namespace std;

  3. class CPolyon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11. };

  12. class CRectangle: public CPolyon {
  13.         public:
  14.                 int area() {
  15.                         return (width * height);
  16.                 }
  17. };

  18. class CTriangle: public CPolyon {
  19.         public:
  20.                 int area() {
  21.                         return (width * height) / 2;
  22.                 }
  23. };

  24. int
  25. main(void)
  26. {
  27.         CRectangle rect;
  28.         CTriangle trgl;
  29.         rect.set_values(4,5);
  30.         trgl.set_values(4,5);

  31.         cout << rect.area() << endl;
  32.         cout << trgl.area() << endl;

  33.         return (0);
  34. }


In principle, a derived class inherits every member of a base class except:

  • its constructor and its destructor
  • its operator=() members
  • its friends

  1. #include <iostream>
  2. using namespace std;

  3. class mother {
  4.         public:
  5.                 mother() {
  6.                         cout << "mother: no parameters\n";
  7.                 }
  8.                 mother(int a) {
  9.                         cout << "mother: int parameters\n";
  10.                 }
  11. };

  12. class daughter: public mother {
  13.         public:
  14.                 daughter(int a) {
  15.                         cout << "daughter: int parameters\n\n";
  16.                 }
  17. };

  18. class son: public mother {
  19.         public:
  20.                 son(int a) : mother(a) {
  21.                         cout << "son: int parameters\n\n";
  22.                 }
  23. };

  24. int
  25. main(void)
  26. {
  27.         daughter cynthia (0);
  28.         son daniel(0);

  29.         return (0);
  30. }
Multiple inheritance:
  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11. };

  12. class COutput {
  13.         public:
  14.                 void output(int i);
  15. };

  16. void
  17. COutput::output(int i)
  18. {
  19.         cout << i << endl;
  20. }

  21. class CRectangle: public CPolygon, public COutput {
  22.         public:
  23.                 int area() {
  24.                         return (width * height);
  25.                 }
  26. };

  27. class CTriangle: public CPolygon, public COutput {
  28.         public:
  29.                 int area() {
  30.                         return (width * height) / 2;
  31.                 }
  32. };

  33. int
  34. main(void)
  35. {
  36.         CRectangle rect;
  37.         CTriangle trgl;
  38.         rect.set_values(4,5);
  39.         trgl.set_values(4,5);
  40.         rect.output(rect.area());
  41.         trgl.output(trgl.area());

  42.         return (0);
  43. }

阅读(954) | 评论(0) | 转发(0) |
0

上一篇:类 Class

下一篇:多态

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