C++友元函数和友元类
来源:互联网 发布:云同步盘 linux 编辑:程序博客网 时间:2024/06/09 04:59
1 友元函数
友元函数不是类的内部函数,是一个全局函数,但是可以改变类的私有属性
友元函破坏了类的封装性
class A1{public: A1() { a1 = 100; a2 = 200; } int getA1() { return this->a1; } //声明一个友元函数 friend void setA1(A1 *p, int a1); //这个函数是这个类的好朋友protected:private: int a1; int a2;};void setA1(A1 *p, int a1){ p->a1 = a1;}void main(){ A1 mya1; cout<<mya1.getA1()<<endl; setA1(&mya1, 300); //通过友元函数 修改A类的私有属性 cout<<mya1.getA1()<<endl; system("pause");}
友元函数不属于类的成员函数,所以只在类里面声明,实现的时候不需要加命名空间!
2 友元类
- 若B类是A类的友员类,则B类的所有成员函数都是A类的友员函数
- 友员类通常设计为一种对数据操作或类之间传递消息的辅助类
#include <iostream>using namespace std;class A{public: friend class B;//B类 是 A的好朋友 ,在B中可以访问A类的私有成员 私有函数 //1 声明的位置 和 public private没有关系 friend void modifyA(A *pA, int _a); //2 函数modifyA 是 类A的好朋友 A(int a=0, int b=0) { this->a = a; this->b = b; } int getA() { return this->a; }private: int a; int b;};// void modifyA(A *pA, int _a){ //pA->a = 100; pA->a = _a;}//class B{public: void Set(int a) { Aobject.a = a; } void printB() { cout<<Aobject.a <<endl; }private: A Aobject;};
3 设计一个数组类的最初模型
- 数组类头文件
#ifndef __MYARRAY_H__#define __MYARRAY_H__#include <iostream>class Array{public: Array(int len); Array(const Array &obj); ~Array(); int length(); void setData(int index, int data); int getData(int index);public: int& operator[](int index) const; Array& operator=(const Array &obj); bool operator==(const Array &obj); bool operator!= (const Array &obj);private: int m_len; int *m_p; // 数组空间};#endif //__MYARRAY_H__
- 数组类实现文件
#include "MyArray.h"Array::Array(int len){ m_len = len; m_p = new int[len];}Array::Array(const Array &obj){ m_len = obj.m_len; m_p = new int[m_len]; for (int i = 0; i < m_len; i++) { m_p[i] = obj.m_p[i]; }}Array::~Array(){ if (m_p != NULL) { delete [] m_p; m_p = NULL; } m_len = 0;}int Array::length(){ return m_len;}void Array::setData(int index, int data){ m_p[index] = data;}int Array::getData(int index){ return m_p[index];}int& Array::operator[](int index) const{ return m_p[index];}Array& Array::operator=(const Array &obj){ if (this == &obj) return *this; // 1、释放旧空间 if (m_p != NULL) { delete [] m_p; m_p = NULL; } // 2、开辟新空间 m_p = new int[obj.m_len]; // 3、复制 m_len = obj.m_len; for (int i = 0; i < m_len; i++) { // obj.operator[](i) ==> operator[](&obj, i) m_p[i] = obj[i]; } return *this;}bool Array::operator==(const Array &obj){ if (m_len != obj.m_len) return false; for (int i = 0; i < m_len; i++) { if (m_p[i] != obj[i]) return false; } return true;}bool Array::operator!= (const Array &obj){ return !(*this == obj);}
- 测试文件
#include <iostream>#include "MyArray.h"using namespace std;int main1(){ // 自己写MyArray.h 头文件和 MyArray.cpp 源文件 实现数组类 Array // 数组类的构造函数有一个 int 型参数 表示数组的大小 Array a1(10); // 数组类有一个 length 方法 用于获取数组的大小 for (int i=0; i<a1.length(); i++) { // 设置数组的元素 ,第一个参数表示下标,第二个参数表示 下标对应的值 a1.setData(i, i); } cout<<"\n打印数组a1: "; for (int i=0; i<a1.length(); i++) { // 获取数组元素 ,参数表示数组下标 cout<<a1.getData(i)<<" "; } cout<<endl; // 完成数组的拷贝构造函数,允许两个数组对象互相赋值 Array a2 = a1; cout<<"\n打印数组a2: "; for (int i=0; i<a2.length(); i++) { cout<<a2.getData(i)<<" "; } cout<<endl; cout<<"hello..."<<endl; return 0;}int main(){ Array a1(10); for (int i=0; i < a1.length(); i++) { a1[i] = i; } cout<<"\n打印数组a1: "; for (int i=0; i<a1.length(); i++) { cout << a1[i] << " "; } cout<<endl; Array a2 = a1; cout << "\n打印数组a2: "; for (int i=0; i< a2.length(); i++) { cout << a2[i] <<" "; } cout<<endl; //3 Array a3(5); { a3 = a1; a3 = a2 = a1; cout<<"\n打印数组a3: "; for (int i=0; i<a3.length(); i++) { cout << a3[i] << " "; } cout << endl; //a3.operator=(a1) //Array& operator=(Array &a1) } a3[2] = 100; //功能4 if (a3 == a1) { printf("相等\n"); } else { printf("不相等\n"); } if (a3 != a1) { printf("不相等\n"); } else { printf("相等\n"); } cout<<"hello..."<<endl; return 0;}
4.小结
- 类通常用关键字class定义。类是数据成员和成员函数的封装。类的实例称为对象。
- 结构类型用关键字struct定义,是由不同类型数据组成的数据类型。
- 类成员由private, protected, public决定访问特性。public成员集称为接口。
- 构造函数在创建和初始化对象时自动调用。析构函数则在对象作用域结束时自动调用。
- 重载构造函数和复制构造函数提供了创建对象的不同初始化方式。
- 静态成员是局部于类的成员,提供一种同类对象的共享机制。
- 友员用关键字friend声明。友员是对类操作的一种辅助手段。一个类的友员可以访问该类各种性质的成员。
- 链表是一种重要的动态数据结构,可以在程序运行时创建或撤消数据元素。
阅读全文
0 0
- 【C++】友元类和友元函数(转)
- C++: 友元函数和友元类详解
- 『C/C++』友元函数和友元类
- C++——友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元类和友元函数
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元函数和友元类
- 友元类和友元函数
- 友元函数和友元类
- 友元函数和友元类
- easyUI 设置文本框是否可编辑
- n皇后问题
- IOC控制反转(依赖注入)
- ExoPlayer里里外外之:核心类和数据流
- css3--flex
- C++友元函数和友元类
- QT 嵌入exe应用程序 并获取鼠标事件 mouse keyboard event
- Axure RP 8最新激活码
- js中基本类型操作-----数组基本操作、字符串操作、json操作
- 数据库_1
- oracle的rownum和rowid区别
- 压力测试工具JMeter入门教程
- QSettings读写注册表、配置文件
- HTTP第一篇【HTTP基础概念、与HTTP相关的协议】