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 20Explanation:
說明:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bst 删除节点_在BST中删除大于或等
- 下一篇: python 发送邮件connect n