C++(7) 友元--友元函数&友元类

文章目录

      • 友元
        • 1. 友元操作
          • 1.1 友元概述
          • 1.2 友元 friend 操作
            • 1.2.1 友元函数
            • 1.2.2 友元函数同时使两个类的友元
            • 1.2.3 友元类

友元

1. 友元操作
1.1 友元概述

类的友元函数时定义在类外部,但是权访问类的所有私有 (private) 成员和保护 (protected) 成员。尽管友元函数的原型又在类的定义中出现过,但是友元函数并不是成员函数。

友元可以是一个函数,该函数被称为友元函数;友元也可以时一个类,该类被称为友元类,在这种情况下,整个类及其所有成员都是友元。

A 是 B 的朋友
B 认为 A 是自己的伙伴,但是 A 不一定认为 B 是自己的伙伴

A 和 B 是好朋友
A 和 A 互相认为对方是伙伴

C 是 A 和 B 的朋友
A 和 B 之间的关系不确定,但是 A 和 B 都认为 C 是自己的伙伴,C 不一定认为 A 和 B 是自己的伙伴
1.2 友元 friend 操作
1.2.1 友元函数
#include 

using namespace std;

class Person
{
public:
    Person() {}
    Person(int id, string name, int age) : id(id), name(name), age(age) {}
    ~Person() {}

    /*
    当前声明了一个 friend 修饰的函数,要求的参数是 Person 类型的引用

    @param p Person 对应的引用
    */
    friend void show(const Person & p);

private:
    int id;
    string name;
    int age;
};

/*
在类外实现类内的友元函数
    1. 不需要再次使用 friend 关键字标记,如果有 friend 会报错
    2. 当前函数不需要提供作用域限制,没有所属类声明

正常情况下, private 修饰的内容,类外无法直接使用,友元函数可以打破
权限限制。在 Person 类内使用 friend 关键字声明当前 show 函数
编译器会认为 show 函数是当前 Person 类的友元函数,可以直接操作
存在权限的内容 
*/
void show(const Person & p)
{
    /*
    show 函数是 Perosn 类的友元函数,可以直接操作使用 Person 类内的
    private 或者 protected 修饰内容。
    */
    cout << "ID : " << p.id 
        << ", Name : " << p.name
        << ", Age : " << p.age
        << endl;
}

int main(int argc, char const *argv[])
{
    Person * p = new Person(1, "布丁" , 3);

    show(*p);

    delete p;
    
    return 0;
}
1.2.2 友元函数同时使两个类的友元
#include 

using namespace std;

// 前置声明
class Student;

class Person
{
public:
    Person() {}
    Person(int id, string name, int age) : id(id), name(name), age(age) {}
    ~Person() {}

    /*
    show 函数在 Person 类内声明,同时使用 friend 修饰,
    告知编译器 show 函数是 Person 的友元函数
    */
    friend void show(const Person &p, const Student &s);

private:
    int id;
    string name;
    int age;
};

class Student
{
public:
    Student() {}
    Student(int id, string name, int age) : id(id), name(name), age(age) {}
    ~Student() {}

    /*
    show 函数在 Student 类内声明,同时使用 friend 修饰,
    告知编译器 show 函数是 Student 的友元函数
    */
    friend void show(const Person &p, const Student &s);

private:
    int id;
    string name;
    int age;
};

/*
当前 show 函数有两个声明,但是声明格式一致,分别在 Person 和 Student 类内,
同时采用 friend 关键字修饰,表示当前 show 函数是 Person 和 Student 的友元函数,
可以同时操作 Person 和 Student 的权限限制 [protected private] 内容
*/
void show(const Person &p, const Student &s)
{
    cout << "Person ID : " << p.id << ", name : " << p.name << ", Age : " << p.age << endl;
    cout << "Student ID : " << s.id << ", name : " << s.name << ", Age : " << s.age << endl;
}

int main(int argc, char const *argv[])
{
    Person *p = new Person(1, "赵六", 3);
    Student *s = new Student(2, "吴九", 5);

    show(*p, *s);

    delete p;
    delete s;
    
    return 0;
}
1.2.3 友元类
#include 

using namespace std;

// 前置声明
class Student;

class Person
{
public:
    Person() {}
    Person(int id, string name, int age) : id(id), name(name), age(age) {}
    ~Person() {}

    /*
    在 Person 类内声明 Student 类是当前 Person 的友元类
    在 Student 类中可以直接访问 Person 的权限限制成员
    */
    friend class Student;

private:
    int id;
    string name;
    int age;
};

class Student
{
public:
    Student() {}
    Student(int id, string name, int age) : id(id), name(name), age(age) {}
    ~Student() {}

    void show(const Person &p);
    friend void show2(const Student &p2);

private:
    int id;
    string name;
    int age;
};


void Student::show(const Person &p)
{
    cout << "Person ID : " << p.id
        << ", Name : " << p.name
        << ", Age : " << p.age << endl;
}

void show2(const Student &p2)
{
    cout << "Student ID : " << p2.id
        << ", Name : " << p2.name
        << ", Age : " << p2.age << endl;
}

int main(int argc, char const *argv[])
{
    Person *p = new Person(1, "赵六", 3);
    Student *s = new Student(2, "吴九", 4);

    s->show(*p);
    show2(*s);

    delete s;
    delete p;
    
    return 0;
}

你可能感兴趣的:(c++,开发语言,c语言,学习,经验分享)