|
-
July 31st, 2003, 01:05 PM
#1
Junior Member
Friend Function And Inheritance
[Code]
#include<iostream.h>
class B;
class A
{
int a;
public:
A()
{
a=1;
}
friend void FRIEND(A,B);
};
class B
{
int b;
B()
{
b=1;
}
friend void FRIEND(A,B);
};
class D: private B
{
};
void FRIEND(A x, B y)
{
int c=x.a+y+b;
cout<<c;
}
void main()
{
A x;
D y;
FRIEND(x,y);
}
The Above code gives these error
1. Cannot access B::B(D)
2. Cannot find Match for FRIEND(A,D);
if i change the Class D
As
class D ublic B
{
};
the code works find
i only want to know the relationship and the concept of this friend function with inheritance
thanx in advance
-
July 31st, 2003, 06:34 PM
#2
The error you are getting is in the order of the way private, protected, and public are treated with inheritance.
If class D inherits Class B through the private statement then the main function wouldn't be able access a private member function. In order words you are trying to call a private function straight from main using this method, which is why it doesn't work. However, by switching it to public it works.
Am not sure am explaining this clearly enough. Ok 'D y' is an object of class D that inherits a function from the class B by the private notation. Main calls this function which is common to both A and B but when it reaches the part where it has to use the B object, it sees private and hence don't have access. When it's public however there's no problem. That's just how the order works. The main would not have access to it in that way but another class would which inherited it.
I hope that helps. It's kind of hard to explain by writing it... If you need some clarification don't be afraid to ask. I will try my best to clear it up for you.
Guidance....
- The mind is too beautiful to waste...
Cutty

-
July 31st, 2003, 08:22 PM
#3
Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|