#include <iostream>
usingnamespace std;
class mike;
class jeff;
// jeff class
class jeff
{
private:
int n_one;
int n_two;
public:
jeff(int, int);
friendclass mike;
void check();
};
// mike class
class mike
{
private:
int n_three;
int n_four;
public:
mike( int,int );
friendclass jeff;
};
// jeff functions
jeff::jeff(int a,int b)
{
n_one = a;
n_two = b;
}
void jeff::check()
{
if ( n_one == n_three )
{
cout << "nice job" << endl;
}
else
{
cout << "better luck next time" << endl;
}
}
// mike functions
mike::mike( int a,int b )
{
n_three = a;
n_four = b;
}
// start main
int main()
{
jeff j1(3,4);
mike m1(13,14);
j1.check();
return 0;
}
So I'm trying to use the check() function to see if n_one from jeff and n_three from mike are the same but I get the error at line 39 that n_three was not declared in this scope. How do I get a function in jeff to see the variables in mike?
Thanks guys!
If you wanted the function in jeff to see variables in mike, you would probably want to derive jeff from mike. You see no where did you tell your program to add anything to either class other than the declare variables.
class first{
protected:
int number;
public:
void set_number(int);
int get_number();
};
void first::set_number(int toset){
number = toset;
}
int first::get_number(){
return number;
}
class second : public first{
protected:
//NOTHING???!!
public:
//Yep, nothing.
};
int main(){
second instance;
instance.set_number(3);
int x = instance.get_number();
//x now holds 3 =]
}
Now friends are a different topic entirely. Friend classes can access private and protected variables and methods inside the host class. So if we had a class root with a method (probably templated) to grab the protected x and y coordinates from another class, we would want to make the root class a friend of any class that we want it to be able to access.
I previously read the tutorial on inheritance and friends. I understand inheritance and I can get that to work. I just want to know how to do it by just using friend classes. What does it mean when you said "if we had a class root with a method (probably templated)"? Is there no way of just using the private variables from one class in another?
You shouldn't expect to be able to use inheritance and friends interchangeably. They are different features with different applications.
You say you understand inheritance. All that declaring a function or class as a friend of a class does is that it allows the friend function or class to access the protected and private members of the other class.
You're right Xander, and for future use, I need to learn how to use each one correctly. However in this case, I first tried to understand passing variables with the friends c++ tutorial and then realized I understood it easier using inheritance. I just also wanted to learn how to pass private variables using classes like the tutorial demonstrated.
#include <iostream>
usingnamespace std;
class mike;
class jeff;
// jeff class: public mike
class jeff
{
private:
int n_one;
int n_two;
public:
jeff(int, int);
//friend class mike;
void check( mike a );
};
// mike class
class mike
{
private:
int n_three;
int n_four;
public:
mike( int,int );
friendclass jeff;
};
// jeff functions
jeff::jeff(int a,int b)
{
n_one = a;
n_two = b;
}
void jeff::check( mike a )
{
if ( n_one == a.n_three)
{
cout << "nice job" << endl;
}
else
{
cout << "better luck next time" << endl;
}
}
// mike functions
mike::mike( int a,int b )
{
n_three = a;
n_four = b;
}
// start main
int main()
{
jeff j1(3,4);
mike m1(3,14);
j1.check(m1);
return 0;
}
I first had to pass the m1 varaible from line 62 to the check() function. Then in line 16 I had to make a mike variable to send to the check() function and in line 39 I was able to get the private variable from mike. That's what I was looking to do. I apologize if I was unclear with my question.