c friend -- 友元

友元用于突破protected 或者 private 保护的限制,首先要做的是在被访问者的类中声明是友元函数或者友元类。代码如下

  1. #include <iostream>
  2. using namespace std;
  3. class Square{
  4. private:
  5. int side;
  6. public:
  7. Square(int a):side(a){}
  8. friend class Rectangle ; //declare the Class is friend
  9. };
  10. class Rectangle {
  11. private:
  12. int width, height;
  13. public:
  14. Rectangle(int a, int b):width(a),height(b){}
  15. void set_values (int a, int b){
  16. width = a;
  17. height = b;
  18. }
  19. int girth();
  20. friend int area (Rectangle &); //declare the friend function here
  21. int get_width(){return width;}
  22. int get_height(){return height;}
  23. void conver_from_square(Square &s){
  24. width = height = s.side; //access so easily
  25. }
  26. };
  27. //implement the function here , access easily ,too
  28. int area (Rectangle &r){ return ( r.width * r.height); }
  29. int r_area(Rectangle &r){ return r.get_width() * r.get_height();}
  30. int Rectangle::girth(){return width + width + height + height; }
  31. int main () {
  32. test_sizeof:
  33. cout << "sizeof: Square " << sizeof(Square)
  34. << ",\tRectangle " << sizeof(Rectangle) << "\n\n" ;
  35. test_access:
  36. Rectangle r(2,3);
  37. cout << "area:" << area(r) << "\tgirth:" << r.girth() << endl;
  38. cout << "onather way:area " << r_area(r) << endl;
  39. Rectangle r1(2,3);
  40. Square s(5);
  41. r1.conver_from_square(s);
  42. cout << "rectangle convering from square , girth is " << r1.girth() << endl;
  43. return 0;
  44. }
结果
  1. sizeof: Square 4, Rectangle 8
  2. area:6 girth:10
  3. onather way:area 6
  4. rectangle convering from square , girth is 20
看函数
  • area
  • r_area
  • girth
如果不是友元函数或类,访问情况如 r_area()函数,友元函数就可以直接访问成员。但是和成员函数比起来还是要有区别的,看函数girth()
 

看看size,友元类或者友元函数并不增加类的大小,只是声明一下。

c friend -- 友元的更多相关文章

  1. C++的友元类和友元函数实例

    #include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...

  2. C++学习笔记 构造&析构 友元 new&delete

    构造&析构函数 构造函数 定义:与类同名,可以有参可以无参,主要功能用于在类的对象创建时定义初始化的状态,无返回值,也不能用void修饰,构造函数不能被直接调用,必须通过new运算符在创建对象 ...

  3. c++友元函数

    c++友元函数分两类: 一://友员全居函数 /*#include <iostream>using namespace std;class aaa{    friend void prin ...

  4. 重载运算符:类成员函数or友元函数

    类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...

  5. C++之友元

    友元提供了不同类的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通过友元,一个不同函数或另一个类中的成员函数可以访问类中的私有成员和保护成员.C++中的友元为封装隐藏这堵不透明的墙开了一 ...

  6. 不可或缺 Windows Native (20) - C++: 友元函数, 友元类

    [源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...

  7. InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集

    友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...

  8. c++ 操作符重载和友元

    操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...

  9. [Reprint]C++友元函数与拷贝构造函数详解

    这篇文章主要介绍了C++友元函数与拷贝构造函数,需要的朋友可以参考下   一.友元函数 1.友元函数概述: (1)友元函数是定义在一个类外的普通函数.友元函数和普通函数的定义一样;在类内必须将该普通函 ...

  10. C++——友元、异常和其他

    一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...

随机推荐

  1. Bad Hair Day(单调栈 )

    Bad Hair Day Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15941   Accepted: 5382 Des ...

  2. GlusterFS常用命令

    1.启动/关闭/查看glusterd服务 # /etc/init.d/glusterd start # /etc/init.d/glusterd stop # /etc/init.d/glusterd ...

  3. BZOJ 3036: 绿豆蛙的归宿( 期望dp )

    从终点往起点倒推 . 在一个图 考虑点 u , 出度为 s : s = 0 , d[ u ] = 0 ; s ≠ 0 , 则 d( u ) = ( ∑ d( v ) ) / s ( ( u , v ) ...

  4. javascript每日一练(十四)——弹性运动

    一.弹性运动 运动原理:加速运动+减速运动+摩擦运动: <!doctype html> <html> <head> <meta charset="u ...

  5. Qt学习之路:自定义Model三篇,自定义委托等等

    http://devbean.blog.51cto.com/448512/d-8/p-2

  6. WPF Multi-Touch 开发:基础触屏操作(Raw Touch)

    原文 WPF Multi-Touch 开发:基础触屏操作(Raw Touch) 多点触控(Multi-Touch)就是通过与触屏设备的接触达到人与应用程序交互的操作过程.例如,生活中经常使用的触屏手机 ...

  7. solr 从零学习开始

    2010-10 目 录 1 1.1 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4. ...

  8. 解决yum升级的问题“There was a problem importing one of the Python modules”

    yum命令升级的时候,报出这个错误. There was a problem importing one of the Python modules required to run yum. The ...

  9. Android圆环形颜色选择器:HoloColorPicker

    HoloColorPicker实现圆环形颜色选择器,可以改变颜色饱和度来选择颜色.选择颜色时,可以用手指沿着圆环滑动一个滑块,从而选择颜色. 添加以下XML至你的布局中: ? 1 2 3 4 < ...

  10. NSThread的一些细节

    1.NSThread创建方式(一个NSThread对象就代表一条线程)1.1>创建\启动线程(1)线程一启动,就会在thread中执行self的run方法NSTread *thread = [[ ...