0

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);
}
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
G-K
  • 13
  • 3
  • 1
    `obj1.a` is not a valid function argument name in C++ (which this seems to be, despite you not tagging it as such) – UnholySheep Aug 29 '20 at 15:25
  • Also neither `obj1` nor `obj2` are declared in the scope of `enter` (and you are using the wrong operator for input after the first one) – UnholySheep Aug 29 '20 at 15:28
  • Can you extract a [mcve] from your code and post that instead? As a new user, also take the [tour] and read [ask]. – Ulrich Eckhardt Aug 29 '20 at 16:29
  • Please also post the full error message in the body of the question – Alan Birtles Aug 29 '20 at 16:47
  • `void average(A obj1.a, A obj1.b, B obj2.c, B obj2.d, B obj2.e) {}` - This looks wrong. You don't need the `obj.` – rhughes Aug 29 '20 at 17:04
  • thank you for all the suggestions.I have implemented the ones I could and have changed the code a decent amount.I am also new to friend functions so I am not sure if these errors are because of that to a certain extent. – G-K Aug 29 '20 at 18:10
  • `#include` Are you using Turbo C++ by any chance? – n. m. could be an AI Aug 29 '20 at 19:47
  • its on borland C++ version 5.02 I think – G-K Aug 30 '20 at 07:26
  • OT: Are you aware that Borland C++ 5.02 was replaced with Borland C++ Builder in **1997** and that C++ *"was initially standardized in **1998** as ISO/IEC 14882:1998, which was then amended by the C++03, C++11 and C++14 standards. The current C++17 standard supersedes these with new features and an enlarged standard library."* [(wikipedia)](https://en.wikipedia.org/wiki/C%2B%2B)? – Bob__ Aug 30 '20 at 13:13
  • It's nearly a quarter century old piece of software. If you are not coerced to use it by your school, you may want to throw it away and start anew with a modern compiler and a modern C++ book. – n. m. could be an AI Aug 30 '20 at 14:31
  • yeah we are required to use it actually – G-K Aug 30 '20 at 17:26

2 Answers2

0

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

Bob__
  • 12,361
  • 3
  • 28
  • 42
  • thank you for taking time out and answering.your suggestions worked wonderfully – G-K Sep 01 '20 at 05:23
0

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;
} 
M.A
  • 8
  • 1
  • thanks you for taking time out.I tried your answer as well and that worked out great too!! – G-K Sep 01 '20 at 05:23