类与对象\友元

  • 最前面加上关键字 friend
  • 友元是单向的,不具有交换性
    • 实现互访需要两个类都将对方声明为自己的友元类
  • 友元关系不具备传递性
  • 使用友元可以避免频繁调用类的接口函数,提高效率,节省开销

3种形式

  • 友元函数:不属于任何类的普通函数
  • 友元成员:其他类的成员函数
  • 友元类:另一个类

友元函数

  • 通常友元函数是在类的定义中给出原型声明,声明位置不受访问属性限制
    • 不同的属性但结果一样的

友元成员

  • 向前声明
    • 仅是类型说明符
    • 只能用于定义引用和指针
    • 不能用于定义对象

代码示例

#include<iostream>
#include<iomanip>
using namespace std;

class Score;

class Croster
{
private:
    string name;
public:
    Croster(string na = "undef");
    void PrintReport(const Score& s)const;
};

class Score
{
private:
    int math, english;
    double GPA;
public:
    Score(int x = 0, int y = 0);
    friend void Croster::PrintReport(const Score& s)const;
};

Croster::Croster(string na) :name(na)
{}

Score::Score(int x, int y) :math(x), english(y)
{
    GPA = (math + english) / 200.0 * 5;
}

void Croster::PrintReport(const Score& s) const
{
    cout << "Name: " << name << endl;
    cout << "GPA: " << setprecision(2) << s.GPA << endl;
    cout << "Math: " << s.math << endl;
    cout << "English: " << s.english << endl;
}

int main()
{
    int i;
    Croster stu[3] = { Croster("Alice"), Croster("Bob"), Croster("Charlie") };
    Score s[3] = { Score(90, 80), Score(85, 95), Score(70, 80) };
    for (i = 0; i < 3; i++)
        stu[i].PrintReport(s[i]);

    return 0;
}

友元类

  • 若 A 被声明为 B 的友元
    • A 中的所有成员函数都是 B 类的友元成员,都可以访问 B 类的所有成员

代码示例

#include<iostream>
#include<iomanip>
using namespace std;

class Croster;	//向前声明
class CDate
{
private:
	int Date_year, Date_month, Date_day;
public:
	CDate(int y=2000, int m=1, int d=1);
    friend Croster;
};

class Croster
{
private:
    string name;
    double GPA;
public:
    Croster(string n="undef", double G=3);
    void PrintInfo(const CDate& date) const;
};


Croster::Croster(string n, double G):name(n), GPA(G)
{}

void Croster::PrintInfo(const CDate &date) const
{
    cout << "Name: " << name << endl;
    cout << "GPA: " << fixed << setprecision(2) << GPA << endl;
    cout << "Date: " << date.Date_year << "-" << date.Date_month << "-" << date.Date_day << endl;
}

CDate::CDate(int y, int m, int d):Date_year(y), Date_month(m), Date_day(d)
{}

int main()
{
    Croster stu("Tom", 3.8);
    CDate date(2019, 12, 10);
    stu.PrintInfo(date);

	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当我们声明一个类模板时,有时候我们需要让某个类能够访问到这个模板类的私有成员或保护成员。这时我们就可以使用友元类来实现。 下面是一个声明类模板的友元类的示例代码: ``` template<typename T> class MyTemplateClass { friend class MyFriendClass; public: MyTemplateClass(T value); private: T m_value; }; template<typename T> MyTemplateClass<T>::MyTemplateClass(T value) : m_value(value) {} class MyFriendClass { public: template<typename T> void printValue(const MyTemplateClass<T>& obj) { std::cout << "The value is: " << obj.m_value << std::endl; } }; int main() { MyTemplateClass<int> obj(42); MyFriendClass friendObj; friendObj.printValue(obj); return 0; } ``` 在这个示例代码中,我们声明了一个类模板`MyTemplateClass`,并且声明了一个友元类`MyFriendClass`。通过`friend class MyFriendClass;`语句,我们将`MyFriendClass`声明为`MyTemplateClass`的友元类,这意味着`MyFriendClass`可以访问到`MyTemplateClass`的私有成员或保护成员。 在`MyFriendClass`中,我们定义了一个模板函数`printValue`,该函数接受一个`MyTemplateClass`类型的对象作为参数,并打印出这个对象的私有成员`m_value`。在`main`函数中,我们实例化了一个`MyTemplateClass`对象,并创建了一个`MyFriendClass`对象`friendObj`,然后通过`friendObj.printValue(obj)`调用了`MyFriendClass`的`printValue`函数,输出了`MyTemplateClass`对象的私有成员值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

tsglz3210

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

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

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

打赏作者

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

抵扣说明:

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

余额充值