在C 的程序中使用2类,并最多找到2个数字
Program in c++ to use 2 classes and find maximum of 2 numbers
我想找到2个数字的最大值,但是我需要使用2个类和朋友函数,而不是简单的方法。如何实施它?我正在使用以下代码,但代码不起作用。
#include<iostream>
using namespace std;
class one
{
int a;
public:
friend int cal(one a);
};
class two
{
int b;
public:
friend int cal(one a,two b);
};
cal(int f,int g)
{
int ans=(x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cal(10,20);
}
#include<iostream>
using namespace std;
class biggest
{
private:
int a,b;
public:
void input();
void display();
};
void biggest::input()
{
cout<<"Enter 2 nos.:";
cin>>a>>b;
}
void biggest::display()
{
if(a>b)
cout<<"Biggest no.:"<<a;
else
cout<<"Biggest no.:"<<b;
}
int main()
{
biggest b;
b.input();
b.display();
}
输出
输入2号:133 21
样本输出
最大号:133
通过将函数设置为"朋友",您可以使其访问类的私人成员。例子看起来真的很奇怪,但我想这会做到。这两个课程都使私人会员访问" CAL"功能。
#include<iostream>
using namespace std;
class one;
class two;
class one
{
int a = 10;
public:
friend int cal(one a,two b);
};
class two
{
int b = 20;
public:
friend int cal(one a,two b);
};
int cal(one x,two y)
{
return (x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cout << cal(x,y);
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 求出有多少个数字是完美平方,而sqrt()是L,R范围内的素数
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 如何知道n!是否可以表示为三个连续数字的乘法?
- C++通过数字比较两个数字
- 在C++中随机生成 20 个非重复数字
- 将向量之间的数字放在另一个向量之间<vector>>如果两个数字的差值为 1
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 如果我不知道每个列表中有多少个数字,我如何将给定数量的数字列表作为输入?
- 获取第 n 个最大数字
- 获取 2 个数字之间的差异百分比
- 在 txt 文件中显示前两个数字的程序
- 两个有符号数字之间的距离
- 形成一个斐波那契三角形,使得每个数字是上面左对角线或右对角线上两个数字的总和
- 在C++中编程,将 3 个数字发送到一个函数,然后计算这 3 个数字的平均函数
- 将数字表示为两个三角形数字的总和
- 两个数字的对称配对功能
- 将最多 50 位数字的 2 个数字相乘
- 无法理解代码背后的逻辑,这是在两个给定数字之间生成素数的优化问题
- 查找中间两个数字的正则表达式的匹配项