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 ++查找字符串

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

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

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample1 { int A, B; f ...

  2. c ++查找字符串_C ++数组| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { char* STR[] = ...

  3. c ++查找字符串_C ++类和对象| 查找输出程序| 套装4

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  4. c ++查找字符串_C ++类和对象| 查找输出程序| 套装3

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; publi ...

  5. c ++查找字符串_C ++类和对象| 查找输出程序| 套装5

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { int X; int* ...

  6. c ++查找字符串_C ++类和对象| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; class Sample { private int ...

  7. c ++查找字符串_C ++结构| 查找输出程序| 套装2

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { typedef struct ...

  8. c ++查找字符串_C ++结构| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> #include <math.h> using namespace std; str ...

  9. c ++查找字符串_C ++异常处理| 查找输出程序| 套装1

    c ++查找字符串 Program 1: 程序1: #include <iostream> using namespace std; int main() { try { int num1 ...

最新文章

  1. android如何根据机顶盒区分用户,Android 上手机跟机顶盒应用开发的区别
  2. 文件寄生——寄生虫自体繁衍的道路
  3. 深度解读 OpenYurt:从边缘自治看 YurtHub 的扩展能力
  4. springboot 拦截器_Spring Boot入门系列(十)如何使用拦截器,一学就会!
  5. STM32F103:一.(1)MDK的配置
  6. python八角图形绘制_(Python)从零开始,简单快速学机器仿人视觉Opencv—第四节:OpenCV处理鼠标事件...
  7. pureftp在企业中的应用及配置
  8. 轻量级Java EE企业应用实战(第4版):Struts 2+Spring 4+Hibernate整合开发(含CD光盘1张)
  9. 迈达斯导出html计算书,动态计算书模块功能-midas Civil 2011
  10. word表格删除空白行java_在Word中怎样批量删除空行,这些点主要注意
  11. 视频剪辑制作教学:分享十种剪辑技巧,打好基础很重要
  12. 青年教师计算机培训美篇,小学生停课不停学美篇 小学生停课不停学美篇开头语...
  13. 使用宏将xlsx格式文件批量转为xls格式文件
  14. mysql修改表的内容_sql怎么修改表内容
  15. Python运算符优先级与结合性
  16. ISP图像处理—紫边Purple Fringing
  17. java ctr_分组密码_计数器(CTR)模式_原理及java实现
  18. LeetCode - 263 - Ugly Number
  19. Ubuntu 上下左右键变成ABCD
  20. 制作mac os x风格的win7

热门文章

  1. java订单类_基于Java创建一个订单类代码实例
  2. 爱特php文件管理器2.8_查找「超级蜘蛛池开发者中心 抠:44564876易」安卓应用 - 豌豆荚...
  3. 存储器是计算机的记忆装置,存储器(Memory)
  4. 小程序的点赞功能能和浏览次数功能_扫码点餐小程序好用吗?小程序还能实现哪些功能?...
  5. js post中文乱码 php,AJAX之POST数据中文乱码如何解决
  6. 计算机安全事故由谁整改,信息安全检查整改方案 整改方案 .doc
  7. php引号变量_下列PHP数据库insert语句中变量前后的点和双引号有什么作用?
  8. ModuleNotFoundError: No module named ‘torch.utils.serialization‘解决
  9. ELK 日志处理开发指南
  10. MySQL单机多实例部署详解之------多实例分别定义不同的配置文件