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

c ++查找字符串

Program 1:

程序1:

#include <iostream>
using namespace std;
class Sample1 {
int A, B;
friend class Sample2;
};
class Sample2 {
int X, Y;
public:
Sample2()
{
X = 5;
Y = 5;
}
void fun()
{
Sample1 S;
S.A = 10 * X;
S.B = 20 * Y;
cout << S.A << " " << S.B << endl;
}
};
int main()
{
Sample2 S;
S.fun();
return 0;
}

Output:

输出:

50 100

Explanation:

说明:

Here, we created two classes Sample1 and Sample2, we made Sample2 class as a friend of Sample1 class. Then Sample2 can access the private members of Sample1.

在这里,我们创建了两个类样本1样本2,我们做了样品2类作为样本1类的朋友。 然后Sample2可以访问Sample1的私有成员。

Here, we access the private members A and B of Sample1 class in the member function fun() of Sample2 class and then we set the value to the members and print values.

在这里,我们访问Sample2类的成员函数fun()Sample1类的私有成员AB ,然后将值设置为成员并打印值。

Program 2:

程式2:

#include <iostream>
using namespace std;
class Sample1 {
int A, B;
public:
friend class Sample2;
void fun1()
{
Sample2 S;
S.X = 10;
S.Y = 20;
cout << S.X << " " << S.Y << endl;
}
};
class Sample2 {
int X, Y;
public:
friend class Sample1;
Sample2()
{
X = 5;
Y = 5;
}
void fun2()
{
Sample1 S;
S.A = 10 * X;
S.B = 20 * Y;
cout << S.A << " " << S.B << endl;
}
};
int main()
{
Sample2 S;
S.fun();
return 0;
}

Output:

输出:

main.cpp: In member function ‘void Sample1::fun1()’:
main.cpp:12:9: error: ‘Sample2’ was not declared in this scope
Sample2 S;
^~~~~~~
main.cpp:13:9: error: ‘S’ was not declared in this scope
S.X = 10;
^
main.cpp: In function ‘int main()’:
main.cpp:43:7: error: ‘class Sample2’ has no member named ‘fun’; did you mean ‘fun2’?
S.fun();
^~~

Explanation:

说明:

It will generate an error because we are accessing the Sample2 from Sample1 but Sample2 is declared below, so it is not accessible. Then it will generate an error.

因为我们访问来自样本1样品2,样品2如下声明,所以它不能访问它会产生一个错误。 然后它将产生一个错误。

Program 3:

程式3:

#include <iostream>
class SampleB;
class SampleA {
public:
void show(SampleB&);
};
class SampleB {
private:
int VAL;
public:
SampleB() { VAL = 10; }
friend void SampleA::show(SampleB& x);
};
void SampleA::show(SampleB& B)
{
std::cout << B.VAL;
}
int main()
{
SampleA A;
SampleB OB;
A.show(OB);
return 0;
}

Output:

输出:

10

Explanation:

说明:

Here, we create two classes SampleA and SampleB.  And, we created show() function as a friend of SampleB, then it can access private members of class SampleB.

在这里,我们创建两个类SampleASampleB 。 并且,我们以SampleB的朋友的身份创建了show()函数,然后它可以访问SampleB类的私有成员。

Then, we defined show() function, where we accessed private member VAL inside function show() of SampleA class and print the value VAL.

然后,我们定义了show()函数,在此我们访问SampleA类的函数show()内部的私有成员VAL并打印值VAL

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

c ++查找字符串

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545352.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

英语笔记:词组句子:0806

Aim at doing Aim to 动词原形 Vary from … to … 因……而已 Range from … to … 在……范围内波动、变化 In prospect 即将可能发生的 In place 在正确位置&#xff1b;准备妥当 In favor of 支持&#xff0c;赞同 People in control 学校的主管人员 Keep in to…

附录:更多有序集合操作命令

查询有序集合的总个数 语法:zcard key 示例: 127.0.0.1:6379> zcard zset1 (integer) 4查询 score 区间内的元素个数 语法:zcount key min max 示例: 127.0.0.1:6379> zcount zset1 0 10 (integer) 4累加元素的 score 值 语法:zincrby key increment member 示…

侧边菜单栏 android-menudrawer

这是github上的一款开源项目&#xff0c;类似于人人网可滑动的侧边菜单栏-----android-menudrawer。使用方法也很简单。1.将下载的包解压放入你的工作目录下2.利用Eclipse通过new-->project...->Android Project from Existing Code&#xff0c;找到项目目录&#xff0c;…

软件工程质量管理体系要求_软件质量管理| 软件工程

软件工程质量管理体系要求软件质量管理体系 (Software quality management system) A quality management system frequently mentioned as a standard system is a principal scheme used by organizations to ensure that the products they develop have the desired qualit…

英语笔记:写作:Recreational activities

Recreational activities 娱乐活动 Nowadays, in an era of information and technology, abundant recreational activities areavailable for people to release their stain and stress, ranging from sports tosurfing the internet. There is a hot debate going on argu…

Redis 如何实现限流功能?

“限流”这种事在生活中很常见,比如逢年过节时景点的限流,还有工作日的车辆单双号限流等,有人可能会问为什么要限流?我既然买了车子你还不让我上路开?还有我倒景点买了门票,景点不是能赚更多的钱吗?为什么要限流呢? 其实限流的主要目的就是为了保证整个系统的正常运行…

第三四五章(PTA复习)

第三四五章类的构造与继承异常线程类的构造与继承 加 final 唯一可以说明的是该类不可被继承&#xff0c;另外和其它类是一样的 abstract类是抽象类&#xff0c;必须要被继承才能实现&#xff0c;而final修饰的类不能被继承 异常 由于未捕获到异常&#xff0c;所以执行完final…

switch(封装)

#迭代器class switch(object):def __init__(self,value):self.valuevalueself.fallFalse#迭代器方法def __iter__(self):yield self.matchraise StopIterationdef match(self,*args):if self.fall or not args:return Trueelif self.value in args:self.fallTruereturn Trueels…

stl max函数_C ++ STL中带有示例的array :: max_size()函数

stl max函数C STL array :: max_size()函数 (C STL array::max_size() function) max_size() function is a library function of array and it is used to get maximum size of an array. It returns the total number of elements that an array can hold. max_size()函数是…

Redis 属于单线程还是多线程?不同的版本有什么区别?

Redis 是普及率最高的技术之一&#xff0c;同时也是面试中必问的一个技术模块&#xff0c;所以从今天开始我们将从最热门的 Redis 面试题入手&#xff0c;更加深入的学习和了解一下 Redis。 我们本文的面试题是 Redis 属于单线程还是多线程&#xff1f; 典型回答 本文的问题…

英语笔记:词组句子:0906

income 收入 mostly 大部门 particularly 尤其、特别 larger drops 更大幅度的下降 thanks to 由于 liable to 易于、可能 in terms of 在……方面 in view of 鉴于、考虑到 over time 随着时间过去 against time 争分夺秒 on time 准时 behind time 误期 widening…

【原创】STM32下波特率计算详解

波特率的计算STM32下的波特率和串口外设时钟息息相关&#xff0c;USART 1的时钟来源于APB2&#xff0c;USART 2-5的时钟来源于APB1。在STM32中&#xff0c;有个波特率寄存器USART_BRR&#xff0c;如下&#xff1a; STM32串口波特率通过USART_BRR进行设置&#xff0c;STM32的波特…

java对数组进行排序_用Java对数组进行排序所需的最少交换

java对数组进行排序Problem: 问题&#xff1a; In this problem, we would have an unordered array with consecutive distinct natural numbers [1,2,3,..n], where n is the size of the array. We have to find the minimum number of swaps required to sort the array i…

如何实现查询附近的人?

查询附近的人或者是附近的商家是一个实用且常用的功能,比如微信中“附近的人”或是美团外卖中“附近商家”等,如下图所示: 那它是如何实现的呢?我们本文就一起来看。 我们本文的面试题是,使用 Redis 如何实现查询附近的人? 典型回答 在说如何实现地理位置查询之前,首…

英语笔记:词组句子:0712

Define sth as … 定义为…… Tie sth to sth 制约 Derive from源自于 Descend from 从……承袭下来 Divide from 分开、隔开 Distinguished from 区别于 Related to 与……有联系 Refer to 查阅、涉及 Attach to 附在……上、系在……上 Associate with 与……有联系…

第五六七章(PTA复习)

第五六七章图形界面线程IO图形界面 主要的布局管理器类包括流布局(FlowLayout)、边界布局(BorderLayout)、网格布局(GridLayout)、卡 片 布 局 (CardLayout) 、 网 格 包 布 局(CardBagLayout) 线程 答案&#xff1a;B IO

Eclipse jetty和plugin 的结合使用

Jetty做为一个轻量级的J2EE Web application server&#xff0c;它不仅小巧&#xff0c;而且性能也比较稳定&#xff0c;效率也挺高&#xff0c;现在也越来越得到广泛的应用。特别是eclipse平台集成了Jetty Plugin后&#xff0c;更是对RCP整合Web Server开发提供了极大的方便。…

python布尔运算符_Python中布尔的逻辑和按位NOT运算符

python布尔运算符In python, not is used for Logical NOT operator, and ~ is used for Bitwise NOT. Here, we will see their usages and implementation in Python. 在python中&#xff0c; not用于逻辑NOT运算符&#xff0c;而〜用于按位NOT。 在这里&#xff0c;我们将看…

英语笔记:作文:What elective to choose

What elective to choose 选修课的选择 Nowadays, thereexist a wide range of selective courses in college which are ready for theundergraduates to choose from. These courses make the students’ college lifemore colorful. However, as for the purpose of choosi…

Redis 有哪些数据类型?

Redis 的数据类型可谓是 Redis 的精华所在&#xff0c;同样的数据类型&#xff0c;例如字符串存储不同的值对应的实际存储结构也是不同&#xff0c;当你存储的 int 值是实际的存储结构也是 int&#xff0c;如果是短字符串&#xff08;小于 44 字节&#xff09;实际存储的结构为…