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. Fruit Ninja(树状数组+思维)

    Fruit Ninja Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. swift-数组array

    // Playground - noun: a place where people can play import UIKit //--------------------------------- ...

  3. ListBox控件

    主要介绍:自定义数据.绑定数据库数据 前台代码: <div> <asp:ListBox ID=" Width ="100px"> <asp: ...

  4. 一口一口吃掉Hibernate(八)——Hibernate中inverse的用法

    一.Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之 ...

  5. 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编

    在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...

  6. VS2008查看dll导出函数

    打开Visual Studio 2008 命令提示,使用命令 [plain] view plaincopyprint? dumpbin /exports simple.dll 即可查看

  7. ETC_百度百科

    ETC_百度百科 ETC(电子不停车收费系统)

  8. uva 10313 Pay the Price(完全背包)

    题目连接:10313 - Pay the Price 题目大意:有0~300这300种价值的金额. 现在可能给出参数: 1个:n, 输出可以组成价值n的方式的个数. 2个:n, a输出用个数小于a的价 ...

  9. Hbiernate关联排序问题

    使用场景: 假设有两张表请求信息.账户表,它们之间是一对多的关系.对应的java类分别为Sfcx_RequestInfo和Sfcx_Zhxx.Sfcx_RequestInfo有一个Set属性 sfcx ...

  10. 工作线程AfxBeginThread的使用

    工作线程通常用来执行一些后台任务,如:数据计算.后台杀毒等等.因为不需要创建窗口和处理用户消息,编写比较容易,在程序中只要调用AfxBeginThread 函数就可以创建并启动一个工作线程了. Afx ...