c friend -- 友元
c friend -- 友元
友元用于突破protected 或者 private 保护的限制,首先要做的是在被访问者的类中声明是友元函数或者友元类。代码如下
- #include <iostream>
- using namespace std;
- class Square{
- private:
- int side;
- public:
- Square(int a):side(a){}
- friend class Rectangle ; //declare the Class is friend
- };
- class Rectangle {
- private:
- int width, height;
- public:
- Rectangle(int a, int b):width(a),height(b){}
- void set_values (int a, int b){
- width = a;
- height = b;
- }
- int girth();
- friend int area (Rectangle &); //declare the friend function here
- int get_width(){return width;}
- int get_height(){return height;}
- void conver_from_square(Square &s){
- width = height = s.side; //access so easily
- }
- };
- //implement the function here , access easily ,too
- int area (Rectangle &r){ return ( r.width * r.height); }
- int r_area(Rectangle &r){ return r.get_width() * r.get_height();}
- int Rectangle::girth(){return width + width + height + height; }
- int main () {
- test_sizeof:
- cout << "sizeof: Square " << sizeof(Square)
- << ",\tRectangle " << sizeof(Rectangle) << "\n\n" ;
- test_access:
- Rectangle r(2,3);
- cout << "area:" << area(r) << "\tgirth:" << r.girth() << endl;
- cout << "onather way:area " << r_area(r) << endl;
- Rectangle r1(2,3);
- Square s(5);
- r1.conver_from_square(s);
- cout << "rectangle convering from square , girth is " << r1.girth() << endl;
- return 0;
- }
结果
- sizeof: Square 4, Rectangle 8
- area:6 girth:10
- onather way:area 6
- rectangle convering from square , girth is 20
看函数
- area
- r_area
- girth
如果不是友元函数或类,访问情况如 r_area()函数,友元函数就可以直接访问成员。但是和成员函数比起来还是要有区别的,看函数girth()
看看size,友元类或者友元函数并不增加类的大小,只是声明一下。
c friend -- 友元的更多相关文章
- C++的友元类和友元函数实例
#include <math.h> #include<iostream> using namespace std; class Point { public: Point(do ...
- C++学习笔记 构造&析构 友元 new&delete
构造&析构函数 构造函数 定义:与类同名,可以有参可以无参,主要功能用于在类的对象创建时定义初始化的状态,无返回值,也不能用void修饰,构造函数不能被直接调用,必须通过new运算符在创建对象 ...
- c++友元函数
c++友元函数分两类: 一://友员全居函数 /*#include <iostream>using namespace std;class aaa{ friend void prin ...
- 重载运算符:类成员函数or友元函数
类成员函数: bool operator ==(const point &a)const { return x==a.x; } 友元函数: friend bool operator ==(co ...
- C++之友元
友元提供了不同类的成员函数之间.类的成员函数与一般函数之间进行数据共享的机制.通过友元,一个不同函数或另一个类中的成员函数可以访问类中的私有成员和保护成员.C++中的友元为封装隐藏这堵不透明的墙开了一 ...
- 不可或缺 Windows Native (20) - C++: 友元函数, 友元类
[源码下载] 不可或缺 Windows Native (20) - C++: 友元函数, 友元类 作者:webabcd 介绍不可或缺 Windows Native 之 C++ 友元函数 友元类 示例演 ...
- InternalsVisibleToAttribute——把internal成员暴露给指定的友元程序集
友元程序集简介 我们知道一个类中被定义为internal的成员(包括类型.方法.属性.变量.事件)是只能在同一个程序集中被访问到的(当然了,我这里说的是正常的方式,不包括通过反射来访问).这个规则在. ...
- c++ 操作符重载和友元
操作符重载(operator overloading)是C++中的一种多态,C++允许用户自定义函数名称相同但参数列表不同的函数,这被称为函数重载或函数多态.操作符重载函数的格式一般为: operat ...
- [Reprint]C++友元函数与拷贝构造函数详解
这篇文章主要介绍了C++友元函数与拷贝构造函数,需要的朋友可以参考下 一.友元函数 1.友元函数概述: (1)友元函数是定义在一个类外的普通函数.友元函数和普通函数的定义一样;在类内必须将该普通函 ...
- C++——友元、异常和其他
一.友元 类并非只能拥有友元函数,也可以将类作为友元.在这种情况下,友元类的所有方法都可以访问原始类的私有成员和保护成员.另外,也可以做更严格的限制,只将特定的成员函数指定为另一个类的友元.哪些函数. ...
随机推荐
- Fruit Ninja(树状数组+思维)
Fruit Ninja Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- swift-数组array
// Playground - noun: a place where people can play import UIKit //--------------------------------- ...
- ListBox控件
主要介绍:自定义数据.绑定数据库数据 前台代码: <div> <asp:ListBox ID=" Width ="100px"> <asp: ...
- 一口一口吃掉Hibernate(八)——Hibernate中inverse的用法
一.Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之 ...
- 在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编
在 Perl看来, 字符串只有两种形式. 一种是octets, 即8位序列, 也就是我们通常说的字节数组. 另一种utf8编码的字符串, perl管它叫string. 也就是说: Perl只熟悉两种编 ...
- VS2008查看dll导出函数
打开Visual Studio 2008 命令提示,使用命令 [plain] view plaincopyprint? dumpbin /exports simple.dll 即可查看
- ETC_百度百科
ETC_百度百科 ETC(电子不停车收费系统)
- uva 10313 Pay the Price(完全背包)
题目连接:10313 - Pay the Price 题目大意:有0~300这300种价值的金额. 现在可能给出参数: 1个:n, 输出可以组成价值n的方式的个数. 2个:n, a输出用个数小于a的价 ...
- Hbiernate关联排序问题
使用场景: 假设有两张表请求信息.账户表,它们之间是一对多的关系.对应的java类分别为Sfcx_RequestInfo和Sfcx_Zhxx.Sfcx_RequestInfo有一个Set属性 sfcx ...
- 工作线程AfxBeginThread的使用
工作线程通常用来执行一些后台任务,如:数据计算.后台杀毒等等.因为不需要创建窗口和处理用户消息,编写比较容易,在程序中只要调用AfxBeginThread 函数就可以创建并启动一个工作线程了. Afx ...