🟣 OOP · Lesson 44
Friend Function and Friend Class
Friend Function and Friend Class
What is Friend Function and Friend Class?
Friend Function and Friend Class
A friend function or class can access private and protected members of another class. It should be used carefully because it reduces strict encapsulation.
A friend function or class can access private and protected members of another class. It should be used carefully because it reduces strict encapsulation.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
friend-functions.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| friend | Friend used in Friend Function and Friend Class programming. |
| private access | Private access used in Friend Function and Friend Class programming. |
| non-member function | Non-member function used in Friend Function and Friend Class programming. |
| controlled access | Controlled access used in Friend Function and Friend Class programming. |
Syntax / Pattern
friend return_type functionName(ClassName obj);
Example Program
#include <iostream>
using namespace std;
class Box{
int length = 10;
friend void show(Box b);
};
void show(Box b){ cout << b.length; }
int main(){ Box b; show(b); }
Expected Output
10
Program Explanation
- show() is not a member function.
- Because it is friend, it can access private length.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- operator overloading
- closely related classes
- utility functions
Common Mistakes
- Using friend everywhere.
- Thinking friend is inherited automatically.
Practice Tasks
- Create friend function to add private values of two objects.
- Explain why friend should be limited.
Summary
Friend Function and Friend Class is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is Friend Function and Friend Class?
Friend Function and Friend Class
A friend function or class can access private and protected members of another class. It should be used carefully because it reduces strict encapsulation.
A friend function or class can access private and protected members of another class. It should be used carefully because it reduces strict encapsulation.
Level
🟣 Object-Oriented Programming
🟣 Object-Oriented Programming
Example File
friend-functions.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| friend | Friend used in Friend Function and Friend Class programming. |
| private access | Private access used in Friend Function and Friend Class programming. |
| non-member function | Non-member function used in Friend Function and Friend Class programming. |
| controlled access | Controlled access used in Friend Function and Friend Class programming. |
Syntax / Pattern
friend return_type functionName(ClassName obj);
Example Program
#include <iostream>
using namespace std;
class Box{
int length = 10;
friend void show(Box b);
};
void show(Box b){ cout << b.length; }
int main(){ Box b; show(b); }
Expected Output
10
Program Explanation
- show() is not a member function.
- Because it is friend, it can access private length.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- operator overloading
- closely related classes
- utility functions
Common Mistakes
- Using friend everywhere.
- Thinking friend is inherited automatically.
Practice Tasks
- Create friend function to add private values of two objects.
- Explain why friend should be limited.
Summary
Friend Function and Friend Class is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.