日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

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

發布時間:2025/3/11 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。