4.4 友元

在程序中,有些私有属性也想让类外特殊的一些函数或者类进行访问,就需要用到友元的技术

友元的目的就是让一个函数或者类访问一个类中的私有成员

友元的关键字为 friend

友元的三种实现

  • 全局函数做友元

  • 类做友元

  • 成员函数做友元

友元成员函数的使用

限定友元类中指定的成员函数才可以访问被友元类的数据。

  1. 先声明被友元的类

  2. 定义友元类,并声明友元成员函数

  3. 定义被友元类,并声明友元成员函数

  4. 定义友元成员函数

// 声明被友元的类
class CTest;
// 定义友元类,并声明友元成员函数
class CBase
{
public:
    void fun(CTest obj);
    void fun1(CTest obj);
};
​
// 定义被友元类,声明被哪一个成员函数友元
class CTest
{
private:
    int m_age;
    // 为什么被友元类要在友元类的下面声明
    // 因为只有在声明完友元类之后,才能知道有哪些成员函数
    friend void CBase::fun(CTest obj);
};
// 定义友元成员函数
void CBase::fun(CTest obj)
{
    obj.m_age = 20;
}
​
void CBase::fun1(CTest obj)
{
    // 因为这个成员函数没有被声明为 CTest 类的友元成员函数
    // 所以这里访问 私有属性就 报错
    obj.m_age;  
}
​
​
int main()
{
    return 0;
}

4.4.1 全局函数做友元

#include<iostream>
using namespace std;
​
//家类
class Building
{
    friend void goodGay(Building* building);
public:
    Building()
    {
        m_SittingRoom = "客厅";
        m_BedRoom = "卧室";
    }
​
    string m_SittingRoom;
​
private:
    string m_BedRoom;
};
​
//全局函数
void goodGay(Building* building)
{
    cout << "好基友的全局函数正在访问" << building->m_SittingRoom << endl;
    cout << "好基友的全局函数正在访问" << building->m_BedRoom << endl;
}
​
void test()
{
    Building building;
    goodGay(&building);
}
int main()
{
    test();
    system("pause");
    return 0;
}

4.4.2类做友元

#include<iostream>
using namespace std;
​
class Building;
class GoodGay
{
public:
    void visit()
    {
    
    }
    Building building;
​
};
class Building
{
    friend class GoodGay;
public:
    Building()
    {
        m_SittingRoom = "客厅";
        m_BedRoom = "卧室";
    }
​
    string m_SittingRoom;
​
private:
    string m_BedRoom;
​
}
​
int main()
{
​
​
    system("pause");
    return 0;
}

4.4.3 成员函数做友元

#include<iostream>
using namespace std;
​
class Building;
class GoodGay
{
public:
    void visit()
    {
​
    }
    Building building;
​
};
class Building
{
    friend void GoodGay::visit();
public:
    Building()
    {
        m_SittingRoom = "客厅";
        m_BedRoom = "卧室";
    }
​
    string m_SittingRoom;
​
private:
    string m_BedRoom;
​
};
​
int main()
{
​
​
    system("pause");
    return 0;
}

2、理解下面的程序,并运行查看结果,回答程序后面的问题。 #include <iostream.h> class CComplex { public: CComplex(double r = 0, double i = 0) { real = r; imag = i; } int operator int() { return (int)real; } void Display(void) { cout << "(" << real << "," << imag << ")" << endl; } protected: double real; double imag; }; class CVector { public: CVector(CComplex &obj1, CComplex &obj2, CComplex &obj3, CComplex &obj4) { objArray[0] = obj1; objArray[1] = obj2; objArray[2] = obj3; objArray[3] = obj4; } friend CComplex &operator[](CVector obj, int n); private: CComplex objArray[4]; }; CComplex &operator[](CVector obj, int n) { if(n<0 || n>3) { cout<<"Out of range!"<<endl; exit(0); } return obj.objArray[n]; } int main() { CComplex c1(1.1, 1.1); CComplex c2(2.2, 2.2); CComplex c3(3.3, 3.3); CComplex c4(4.4, 4.4); CVector v(c1,c2,c3,c4); v[0].Display(); v[1].Display(); v[2].Display(); v[3].Display(); v[0] = 5.5; ----------------------------------------------------------① v[1] = CComplex(6.6); -------------------------------------------② v[2] = int(CComplex(7.7)); --------------------------------------③ v[3] = int(CComplex(8.8,9.9)); ----------------------------------④ v[0].Display(); v[1].Display(); v[2].Display(); v[3].Display(); return 0; } 问题一:上述程序存在两大错误,在不修改主函数和程序原意的前提下,改正该程序中存在的错误。 问题二:①处的转换属于显式转换还是隐式转换,并解释该转换过程。 问题三:②处的转换属于显式转换还是隐式转换,并解释该转换过程。 问题四:解释③处的转换过程。 问题五:解释④处的转换过程。
05-26
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑在抓耳挠腮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值