c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1

本文通过四个程序介绍了C++中的友元函数,如何使用它们来访问类的私有成员。示例中解释了不正确使用友元函数会导致的错误情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c ++查找字符串

Program 1:

程序1:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    friend void fun();
};

void fun()
{
    Sample S;

    S.A = 10;
    S.B = 20;

    cout << S.A << " " << S.B << endl;
}

int main()
{
    fun();
    return 0;
}

Output:

输出:

10 20

Explanation:

说明:

Here, we created a class Sample that contains private data members A and B.

在这里,我们创建了一个类Sample ,其中包含私有数据成员AB。

As we know that, we cannot access the private members outside the class. Here, we defined a non-member function as a friend function that can access private members.

众所周知,我们无法访问课程之外的私人成员。 在这里,我们将非成员函数定义为可以访问私有成员的朋友函数。

Then, we set the values into A and B and printed.

然后,将值设置为AB并打印。

Program 2:

程式2:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    void fun();
};

friend void fun()
{
    Sample S;

    S.A = 10;
    S.B = 20;

    cout << S.A << " " << S.B << endl;
}

int main()
{

    fun();

    return 0;
}

Output:

输出:

main.cpp:9:1: error: ‘friend’ used outside of class
 friend void fun()
 ^~~~~~
main.cpp: In function ‘void fun()’:
main.cpp:13:7: error: ‘int Sample::A’ is private within this context
     S.A = 10;
       ^
main.cpp:5:9: note: declared private here
     int A, B;
         ^
main.cpp:14:7: error: ‘int Sample::B’ is private within this context
     S.B = 20;
       ^
main.cpp:5:12: note: declared private here
     int A, B;
            ^
main.cpp:16:15: error: ‘int Sample::A’ is private within this context
     cout << S.A << " " << S.B << endl;
               ^
main.cpp:5:9: note: declared private here
     int A, B;
         ^
main.cpp:16:29: error: ‘int Sample::B’ is private within this context
     cout << S.A << " " << S.B << endl;
                             ^
main.cpp:5:12: note: declared private here
     int A, B;
            ^

Explanation:

说明:

This code will generate an error because we are accessing the private members of class Sample in the function fun() but did not define fun() as a friend within the class. We need to define fun() as a friend inside the class.

该代码将产生错误,因为我们正在访问fun()函数中的Sample类的私有成员,但未将fun()定义为该类中的朋友 。 我们需要将fun()定义为类中的朋友。

class Sample
{
	int A,B;
	friend void fun();
};

Program 3:

程式3:

#include <iostream>
using namespace std;

class Sample {
    int A, B;
    friend void fun();
};

friend void fun()
{
    Sample S;
    S.A = 10;
    S.B = 20;
    cout << S.A << " " << S.B << endl;
}

int main()
{
    fun();
    return 0;
}

Output:

输出:

main.cpp:9:1: error: ‘friend’ used outside of class
 friend void fun()
 ^~~~~~

Explanation:

说明:

This code will generate an error because we used friend keyword in the definition of the function fun(). We cannot use a friend keyword outside the class.

该代码将产生错误,因为我们在函数fun()的定义中使用了friend关键字。 我们不能在课程外使用friend关键字。

Program 4:

计划4:

#include <iostream>
using namespace std;

class Sample1 {
    int A, B;

public:
    friend class Sample2;
};

class Sample2 {
    int X, Y;

    void fun1()
    {
        Sample1 S;
        S.A = 10;
        S.B = 20;

        cout << S.A << " " << S.B << endl;
    }
};

int main()
{
    Sample2 S;
    S.fun1();
    return 0;
}

Output:

输出:

main.cpp: In function ‘int main()’:
main.cpp:27:12: error: ‘void Sample2::fun1()’ is private within this context
     S.fun1();
            ^
main.cpp:14:10: note: declared private here
     void fun1()
          ^~~~

Explanation:

说明:

This code will generate an error, we are accessing the private member function fun1() in the main() function.

此代码将产生错误,我们正在访问main()函数中的私有成员函数fun1()

翻译自: https://www.includehelp.com/cpp-tutorial/friend-function-find-output-programs-set-1.aspx

c ++查找字符串

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值