如何将对象作为参数传递?

How to pass objects as arguments?

我已经更改了这里的代码 所有变量都必须保持私有,并且必须使用友元函数。 这里有两个 类 A 和 B,我应该接受 5 个数字(void enter())。函数 average 是为了能够访问所有变量并给出平均值。 我知道目前行

 obj2.average(a,b,c,d,e);

会报错,因为变量不可访问 所以我的问题是,如何在最后一行传递变量?

#include<iostream.h>
#include<conio.h>

class A
{
 int a;
 int b;

 public:
 friend class B;
 int enter1()
 {
  cout<<"enter the value of a and b"<<endl;
  cin>>a>>b;
  return(a,b);
 }
};

class B
{
 int c;
 int d;
 int e;

 public:
 void average(int a, int b, int c, int d, int e)
 {
 float avg;
 avg=((a+b+c+d+e)/5);
 cout<<"average is "<<avg<<endl;
 }

 int enter()
 {
 cout<<"enter the value of c,d,e"<<endl;
 cin>>c>>d>>e;
 return(c,d,e);
 }
 };

void main()
{
 A obj1;
 B obj2;
 obj1.enter1();
 obj2.enter();
 obj2.average(obj1.a,obj1.b,obj2.c,obj2.d,obj2.e);
}

Here there are two classes A and B and I am supposed to accept 5 numbers(void enter()).

显示的两个 classes 有两个成员函数(enter1enter),都返回一个 int(而不是 void,如果我理解了问题陈述),但可能是“错误的”。

int enter()
{
    cout << "enter the value of c,d,e" << endl;
    cin >> c >> d >> e;
    // The variable c, d and e here, are the private members of class B
    // If we assume that all the values are correctly extracted from the input stream 
    // (we shouldn't, but for the sake of simplicity let's do it), those member variables
    // are now set and there's no need to "return" them.
    
    return (c,d,e);
    //     ^^^^^^^     The ',' here, is the comma operator(1). 
    //                 This is equivalent to 
    //    return e;
}

all the variables have to remain private and using friend functions is a requirement.

在发布的代码段中,整个 class B 被声明为 class Afriend,而不仅仅是一些函数。这可能是完成任务的一种方式,但似乎不符合要求。

计算平均值的函数声明为 class B

的 public 成员
void average(int a, int b, int c, int d, int e)
{
    float avg;
    avg = ((a + b + c + d + e) / 5);
    //                         ^      This is an INTEGER division, beeing the result of
    // the sum and the literal 5 both integral types, it produces a TRUNCATED integer result
    // which is only after converted to a float.
    // ...
}

它在 main 中被(错误地)调用为这样

obj2.average(obj1.a, obj1.b, obj2.c, obj2.d, obj2.e);
//           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   Those are all PRIVATE members,
//                                                    invisible from 'main'

How to pass objects as arguments?

满足所有要求的一种方法是将 average 声明为自由函数,两个 classes 的友元(可以访问它们的私有成员),期望两个类型的参数const 引用 class A 和 B.

Here是一种可能的实现,不一定是最好的设计。

(1) The built-in comma operator

您不能在主函数中访问成员变量,因为它们是私有的。 您可以将第一个实例作为参数发送给方法,然后在方法内部使用 classA 成员变量。

#include<iostream>
using std::cin;
using std::cout;
using std::endl;

class A
{
 int a;
 int b;

 public:
 friend class B;
 int enter1()
 {
  cout<<"enter the value of a and b"<<endl;
  cin>>a>>b;
  return(a,b);
 }
};

class B
{
 int c;
 int d;
 int e;

 public:
 void average(A obj)
 {
 float avg;
 avg=((obj.a+obj.b+c+d+e)/5);
 cout<<"average is "<<avg<<endl;
 }

 int enter()
 {
 cout<<"enter the value of c,d,e"<<endl;
 cin>>c>>d>>e;
 return(c,d,e);
 }
 };

int main()
{
 A obj1;
 B obj2;
 
 obj1.enter1();
 obj2.enter();
obj2.average(obj1);
return 0;
}