How to pass objects as arguments?

818 views Asked by At

I have changed the code here all the variables have to remain private and using friend functions is a requirement. Here there are two classes A and B and I am supposed to accept 5 numbers(void enter()).The function average is to be able to access all variables and give the average. I know that currently the line

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

will give an error as the variables are not accessible so my question is, how do I pass the variables in the last line?

#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);
}
2

There are 2 answers

1
M.A On BEST ANSWER

You can not access member variables in the main function because they are private. You can send the first instance to the method as a parameter, then use the class A member variables inside the method.

#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;
} 
1
Bob__ On

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

The two shown classes have two member functions (enter1 and enter), both returning an int (instead of beeing void, if I've understood the problem statement), but probably the "wrong" one.

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.

In the posted snippet, the entire class B is declared as friend of class A, instead of only some functions. That may be a way to accomplish the task, but it doesn't seem to fulfill the requirements.

The function which calculates the average is declared as a public member of class B

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.
    // ...
}

It is (erroneously) called in main like this

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?

One way to meet all the requirements could be to declare average as a free function, friend of both classes (to have access to their private members), expecting two arguments of type const reference to class A and B.

Here is a possible implementation, not necessarily the best design.

(1) The built-in comma operator