Friend Class and Friend Function

◀ Const Object and Const Member Function▶ Generic Class
Amazon The sole goal of having friends is to allow direct access to private data members of a class object. Friends come in two forms: friend classes and friend functions.

Let’s first discuss friend classes. Inside a class, you can declare other classes to be its friends. Doing so allows those classes to access its private members directly. Here is a sample program illustrating this concept:
#include <iostream>
using namespace std;

class student {
friend class teacher; /* now teacher is a friend of student’s */ private: int id; double gpa; char *name; public: student() { id = 0; gpa = 3.0; name = "null"; } int getid() { return id; } double getgpa() { return gpa; } char* getname() { return name; } void changeid(int newid) { id = newid; } void changegpa(double newgpa) { gpa = newgpa; } void changename(char *newname) { name = newname; }
}; class teacher { private: student *students; int size; public: teacher(int num) { students = new student[num]; size = num; } void renewid(); void changegpa(int id, double newgpa); double getgpa(int id); }; void teacher::renewid() { int i; for(i=0; i<size; i++) students[i].id = i; /* accessing student’s private data */ }
void teacher::changegpa(int id, double newgpa) { int i; for(i=0; i<size; i++) { if(students[i].id == id) { /* accessing student’s private data: id */ students[i].gpa = newgpa; /* accessing student’s private data */ return; } } } double teacher::getgpa(int id) { int i; for(i=0; i<size; i++) if(students[i].id == id) /* accessing student’s private data: id */ return students[i].gpa; /* accessing student’s private data */
return –1; } int main(){ int i; teacher amr(100); for(i=0; i<100; i++) amr.renewid(); amr.changegpa(50, 3.99); cout << "ID#50 has " << amr.getgpa(50) << " GPA.\n"; return 0; }
Here is the program’s output:
ID#50 has 3.5 GPA.
As we can see, without declaring teacher to be a friend of student’s, you are not allowed to use the dot notation to access student’s private members. You can comment out the first line inside student and see what happens when you compile. An alternative to being able to use the dot notation to access student’s private members is to simply declare its data members to be public but it is not recommended.


After we see how friend classes work, let’s now see how friend functions work. Here is a sample program using this feature:
#include <iostream>
using namespace std;

class student {
friend int main();  /* main() is a friend of student’s */
friend void showgpa(student s);  /* showgpa() is another friend */
private:				 
  int id;
  double gpa;
  char *name;
public:
  student() {
    id = 0;
gpa = 3.0; name = "null"; } int getid() { return id; } double getgpa() { return gpa; } char* getname() { return name; } void changeid(int newid) { id = newid; } void changegpa(double newgpa) { gpa = newgpa; } void changename(char *newname) { name = newname; } }; void showgpa(student s) { /* accessing student’s private data: name, gpa */ cout << "The gpa of " << s.name << " is " << s.gpa << ".\n";
} int main() { student mike, john; mike.id = 101; mike.gpa = 3.91; mike.name = "Michael Wen"; /* accessing student’s private data: id, gpa */ cout << mike.name << "'s id is " << mike.id << " and gpa is " << mike.gpa << ".\n"; john.id = 102; john.gpa = 3.95; john.name = "John Wang"; showgpa(john); return 0; }
As we can see, both showgpa() and main() can access the private members of student by using the dot notation. To make a function a friend to a class, you need to give the name, the return type, and the parameter types of the function, as the example shows. If you comment out the first two lines inside student and recompile, the compiler will complain.


To sum up, a friend function or a class has the same access privileges as a member function of the class it is friends with.
◀ Const Object and Const Member Function▶ Generic Class

fShare
Questions? Let me know!