Basically, its violating the encapsulation. But it improves the code.
Thinking in such a way that a bigger problem is broken into halves.
How the friend function is called???
The friend function is called like f(x), while a member function is called x.f().
Example:
#include
using namespace std;
class Car
{
private:
int speed;
char color[20];
public:
Car()
{
speed=20;
strcpy(color,"red");
}
friend void display(Car& c);
};
void display(Car& c)
{
cout<< c.speed;
cout<< c.color;
}
void main()
{
Car c;
display(c);
}
No comments:
Post a Comment