📜  C++中的类之间的相互友谊与示例

📅  最后修改于: 2021-05-30 11:56:53             🧑  作者: Mango

先决条件: C++中的Friend类
朋友类可以访问其他被声明为朋友的类的私有成员和受保护成员。有时允许特定的类访问另一个类的私有成员很有用。
下面是说明朋友类的程序:

CPP
// C++ program to illustrate friend class
#include 
using namespace std;
 
// Class A
class A {
private:
    // Private member of Class A
    int a;
 
public:
    // Constructor to initialise private
    // member of class A
    A() { a = 0; }
 
    // Make friend class B using a friend
    // keyword
    friend class B;
};
 
// Class B
class B {
private:
    // Private members of class B
    int b;
 
public:
    // Members function of class B
    void showA(A& x)
    {
        // Since B is friend of A,
        // it can access private members
        // of class A
        cout << "Accessed Private "
             << "Member of class A\n";
        cout << "A::a = " << x.a;
    }
};
 
int main()
{
    // Object of class A
    A a;
 
    // Object of class B
    B b;
 
    // Member function of friend class B
    b.showA(a);
    return 0;
}


CPP
// C++ program to illustrate mutual
// friendship of classes
#include 
using namespace std;
 
// Forward Declaration
class B;
 
// Class A
class A {
 
    // Member of class A
    int data;
 
public:
    // Make B as a friend of class A
    friend class B;
 
    // Constructor to initialise member
    // of class A
    A(int d) { data = d; }
 
    // Function to get data of friend
    // class B
    void get_data_B(B objb);
};
 
// Class B
class B {
 
    // Member of class B
    int dataB;
 
public:
    // Making A a friend of class B
    friend class A;
 
    // Constructor to initialise member of
    // class B
    B(int d) { dataB = d; }
 
    // Function to get the data of
    // friend class A
    void get_data_A(A obja)
    {
        cout << "Data of A is: "
             << obja.data;
    }
};
 
// Function for accessing friend class
// B's object
void A::get_data_B(B objb)
{
    cout << "Data of B is: "
         << objb.dataB;
}
 
// Driver Code
int main()
{
 
    // Object of class A
    A a(10);
 
    // Object of class B
    B b(20);
 
    // Print data of class A
    b.get_data_A(a);
    cout << endl;
 
    // Print data of class B
    a.get_data_B(b);
    return 0;
}


输出:
Accessed Private Member of class A
A::a = 0

班级的相互友谊:如果A级是B级的友好班级,而B级是A级的友好班级,则据说A级和B级是相互友好的。由于这两个班级之间建立了友谊,这些类可以使用自己的成员函数互相访问受保护的成员和私有成员。
通常在这两个类中声明和定义函数,将遇到一个常见的语法错误,这表明最先声明的类即使成为朋友也无法访问后续类的数据成员。
因此,友谊不是相互的。如果A类B的朋友,那么B不会自动成为A类的朋友。
但是我们可以通过避免上面的错误来建立类之间的相互友谊,在第二类之后,使用范围解析运算符定义访问第二类数据成员的第一类中的函数。
让我们假设类A先定义的程序和B类A类后确定。 A类中访问B类数据的成员函数应在B类之后定义。这样做是为了确保编译器首先读取类B ,这又将向编译器提供有关类A类B的朋友的信息,因此允许其访问其成员。
下面是说明班级相互友谊的程序:

CPP

// C++ program to illustrate mutual
// friendship of classes
#include 
using namespace std;
 
// Forward Declaration
class B;
 
// Class A
class A {
 
    // Member of class A
    int data;
 
public:
    // Make B as a friend of class A
    friend class B;
 
    // Constructor to initialise member
    // of class A
    A(int d) { data = d; }
 
    // Function to get data of friend
    // class B
    void get_data_B(B objb);
};
 
// Class B
class B {
 
    // Member of class B
    int dataB;
 
public:
    // Making A a friend of class B
    friend class A;
 
    // Constructor to initialise member of
    // class B
    B(int d) { dataB = d; }
 
    // Function to get the data of
    // friend class A
    void get_data_A(A obja)
    {
        cout << "Data of A is: "
             << obja.data;
    }
};
 
// Function for accessing friend class
// B's object
void A::get_data_B(B objb)
{
    cout << "Data of B is: "
         << objb.dataB;
}
 
// Driver Code
int main()
{
 
    // Object of class A
    A a(10);
 
    // Object of class B
    B b(20);
 
    // Print data of class A
    b.get_data_A(a);
    cout << endl;
 
    // Print data of class B
    a.get_data_B(b);
    return 0;
}
输出:
Data of A is: 10
Data of B is: 20

在上述程序中, AB类是共同的朋友。因此,访问类A的私有成员也可以从类B中的成员函数完成,访问类B的私有成员也可以从类A中的成员函数完成。

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”