How does one access value of variables from one class in another class using friendship?
May 30, 2016 at 2:13pm UTC
I want to be able to use the value of the x and y variable I type in from the Regner class in the extra class. I so far have the following but it's not working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
#include <iostream>
using namespace std;
class extra;
class Regner {
friend class extra;
public :
void getInput();
void calculate();
private :
double x;
double y;
};
class extra{
public :
void bruger(){
Regner one;
z=one.x;
w=one.y;
}
void udfrabruger(){
cout<<"z/w = " <<z/w<<endl;
}
private :
int z,w;
};
int main()
{
Regner find; // construct a default object
extra find2;
find.getInput(); // ask for user input
find.calculate(); // use it
find2.bruger();
find2.udfrabruger();
}
void Regner::getInput()
{
cout << "Enter x: " ;
cin >> x;
cout << "Enter y: " ;
cin >> y;
}
void Regner::calculate()
{
cout << "x + y = " << x + y << endl;
cout << "x - y = " << x - y << endl;
cout << "x * y = " << x * y << endl;
}
May 30, 2016 at 3:02pm UTC
1 2 3
Regner one;
z=one.x;
w=one.y;
What's the value of z after this? What's the value of w after this? (Trick question; you don't know because they haven't been set)
Last edited on May 30, 2016 at 3:17pm UTC
May 30, 2016 at 4:58pm UTC
So what would I have to do? I want z to have the value of x and w to have the value of y which I get form the other class
Topic archived. No new replies allowed.