A function is not a member of a class, but it has access to the private members of the class

C++ Tutorial - Friend Functions and Friend Classes - 2020

A function is not a member of a class, but it has access to the private members of the class

A function is not a member of a class, but it has access to the private members of the class






bogotobogo.com site search:




Friend Functions

The private member data of a class can be accessed only by member functions of that class.

Well, there is one exception. A friend function will be friendly with a class even though it is not a member of that class and can access the private members of the class.

#include <iostream>
using namespace std;

class Rectangle {
	int width, height;

public:
	Rectangle(int w = 1, int h = 1):width(w),height(h){} 
	friend void display(Rectangle &);
};

void display(Rectangle &r) {
	cout << r.width * r.height << endl;
}

int main () {
	Rectangle rect(5,10);
	display(rect);
	return 0;
}

We make a function a friend to a class by declaring a prototype of this external function within the class, and preceding it with the keyword friend

	friend void display(Rectangle &);

The friend function display(rect) has an access to the private member of the Rectangle class object though it's not a member function. It gets the width and height using dot: r.width and r.height. If we do this inside main, we get an error because they are private members and we can't access them outside of the class. But friend function to the class can access the private members.

But what's the point of friend functions. In the above example, we could have made "display" as a member function of the class instead of declaring it as a friend function to the class.

Why do we need friend functions?

A friend function can be friendly to 2 or more classes. The friend function does not belong to any class, so it can be used to access private data of two or more classes as in the following example.

#include <iostream>
using namespace std;

class Square;  // forward declaration

class Rectangle {
	int width, height;

public:
	Rectangle(int w = 1, int h = 1):width(w),height(h){} 
	friend void display(Rectangle &, Square &);
};

class Square {
	int side;

public:
	Square(int s = 1):side(s){} 
	friend void display(Rectangle &, Square &);
};

void display(Rectangle &r, Square &s) {
	cout << "Rectangle: " << r.width * r.height << endl;
	cout << "Square: " << s.side * s.side << endl;
}

int main () {
	Rectangle rec(5,10);
	Square sq(5);
	display(rec,sq);
	return 0;
}

Output is:

Rectangle: 50
Square: 25

The friend functions can serve, for example, to conduct operations between two different classes. Generally, the use of friend functions is out of an object-oriented programming methodology, so whenever possible it is better to use members of the same class to perform operations with them as in the following example getting exactly same output.

#include <iostream>
using namespace std;

class Rectangle {
	int width, height;

public:
	Rectangle(int w = 1, int h = 1):width(w),height(h){} 
	void display() {
		cout << "Rectangle: " << width * height << endl;
	};
};

class Square {
	int side;

public:
	Square(int s = 1):side(s){} 
	void display() {
		cout << "Square: " << side * side << endl;
	};
};

int main () {
	Rectangle rec(5,10);
	Square sq(5);
	rec.display();
	sq.display();
	return 0;
}

Summary:

  1. Friend functions are not members of any class but they can access private data of the class to which they are a friend.
  2. Because they are not members of any class, you should not call them using the dot operator.

Friend Classes

Just like functions are made friends of classes, we can also make one class to be a friend of another class. Then, the friend class will have access to all the private members of the other class.

#include <iostream>
using namespace std;

class Square;

class Rectangle {
	int width, height;

public:
	Rectangle(int w = 1, int h = 1):width(w),height(h){} 
	void display() {
		cout << "Rectangle: " << width * height << endl;
	};
	void morph(Square &);
};

class Square {
	int side;

public:
	Square(int s = 1):side(s){} 
	void display() {
		cout << "Square: " << side * side << endl;
	};
	friend class Rectangle;
};

void Rectangle::morph(Square &s) {
	width = s.side;
	height = s.side;
}

int main () {
	Rectangle rec(5,10);
	Square sq(5);
	cout << "Before:" << endl;
	rec.display();
	sq.display();

	rec.morph(sq);
	cout << "\nAfter:" << endl;
	rec.display();
	sq.display();
	return 0;
}

We declared Rectangle as a friend of Square so that Rectangle member functions could have access to the private member, Square::side

In our example, Rectangle is considered as a friend class by Square but Rectangle does not consider Square to be a friend, so Rectangle can access the private members of Square but not the other way around.






bogotobogo.com site search:


C++ : Where friends have access to your private members.
- Gavin Russell Baker



Which type of function is not a member of a class but has access to the private members of the class?

A friend function is a function that isn't a member of a class but has access to the class's private and protected members.

Which type of function is not a member of a class but has access to the private members of the class quizlet?

A friend function is not a member of a class, but has access to the private members of the class. Why is it not always a good idea to make an entire class a friend of another class? Because every member function of the friend class would have access to the class's private member variables.

How do you access a private member function of a class?

How to access a private member function of a class? Explanation: Even the private member functions can be called outside the class. This is possible if address of the function is known. We can use the address to call the function outside the class.

Can a class access its own private members?

Private: The class members declared as private can be accessed only by the functions inside the class. They are not allowed to be accessed directly by any object or function outside the class. Only the member functions or the friend functions are allowed to access the private data members of a class.