C++ 友元函数的优缺点

C++ 友元函数的优缺点

我们已经创建了友元函数来访问C++面向对象编程系统中的不同修饰符,如Protected、Private和Public。友元函数或友元类通常在类之外定义,但仍可以访问类代码中定义的私有、保护和公共成员。

优点 缺点
声明可以在代码的任何位置 不会传递给派生类
不需要创建对象来调用它。 它们没有存储指定的类。
可以使用友元函数访问类的非公共成员。 当多个类彼此关联时,友元函数非常方便。
可以添加额外的功能。 允许将私有和受保护的成员显示为类的信息。
使编程体验比以往更高效。 在定义了友元函数的同一个类中,可以同时拥有公共、私有和受保护的成员。

C++代码

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
#include
using namespace std;


// the below class declaration is a forward declaration
class JTP;
class XYZ
{
    int x;
    public:
        void set_data(int a)
        {
            x=a;
        }

        friend void max(XYZ, JTP);
};


class JTP {
    int y;
    public:
        void set_data(int a)
        {
            y=a;
        }   

    friend void max(XYZ, JTP);
};

void max(XYZ t1,JTP t2)
{
    if(t1.x>t2.y)
cout<

输出:

/tmp /mB gjSey FC Z.o
3135

C++ 代码

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
#include
using namespace std;

// defining class A from here
class A
{
    int x;
        public:

    A()
    {
        x=3979;
    }
    // the below small code snippet is enough to define the 
    // friend class for our code to demonstrate the usage of class B
    friend class B; 
};
// defining class B from here
class B
{
    public:
        void display(A &t)
        {
cout<

输出:

/tmp /mB gjSey FC Z.o
After performing the operations, the value for X we have got is 3979

C++代码

// below, we are writing down the C++ programming language code to 
// demonstrate the concept of Advantage and disadvantage friend function C++
// which depicts how the friend function can act as a bridge between classes
// and to find the largest of two numbers using the friend function method

#include
using namespace std;

class Largest
{
    // declaring the variables a,b and m for performing the operations
    int a,b,m;
    public:
        void set_data();
        friend void find_max(Largest);  
};

// writing the void function to take the inputs from the user without 
// writing them again and again from the int main function
void Largest::set_data()
{
    // string display to ask the user to give the input
    cout<<"Dear User, please enter the value for the variable first no: ";
    cin>>a;
    cout<<"Dear User, please enter the value for the variable second no: ";
    cin>>b;
}


// function, which calculates the largest among the inputs entered by the 
// user
void find_max(Largest t)
{
    if(t.a>t.b)
        t.m=t.a;
    else
        t.m=t.b;

Cout <<"Hey! User, the maximum number among the inputs entered by you is: "<

输出:

/tmp /mB gjSey FC Z.o
Dear User, please enter the value for the variable first no: 35
Dear User, please enter the value for the variable second no: 365
Hey! User, the maximum number among the inputs entered by you is: 365

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程