欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

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

发布时间:2025/3/11 编程问答 33 豆豆
生活随笔 收集整理的这篇文章主要介绍了 c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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 ,其中包含私有数据成员A和B。

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.

然后,将值设置为A和B并打印。

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 classfriend void fun()^~~~~~ main.cpp: In function ‘void fun()’: main.cpp:13:7: error: ‘int Sample::A’ is private within this contextS.A = 10;^ main.cpp:5:9: note: declared private hereint A, B;^ main.cpp:14:7: error: ‘int Sample::B’ is private within this contextS.B = 20;^ main.cpp:5:12: note: declared private hereint A, B;^ main.cpp:16:15: error: ‘int Sample::A’ is private within this contextcout << S.A << " " << S.B << endl;^ main.cpp:5:9: note: declared private hereint A, B;^ main.cpp:16:29: error: ‘int Sample::B’ is private within this contextcout << S.A << " " << S.B << endl;^ main.cpp:5:12: note: declared private hereint 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 classfriend 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 contextS.fun1();^ main.cpp:14:10: note: declared private herevoid 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 ++查找字符串

总结

以上是生活随笔为你收集整理的c ++查找字符串_C ++朋友功能| 查找输出程序| 套装1的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。