Friend Class Example in C++


Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful to allow a particular class to access private members of other class.

Example Program:

Here is the simple program friend class in C++.

Source Code

#include <iostream>
//Friend Class
using namespace std;
class Your;
class My
private :
    int a;
protected :
    int b;
    int c;
    friend Your;
class Your
    void fun()
        My t;
        cout<< "A : "<<t.a<<endl;
        cout<< "B : "<<t.b<<endl;
        cout<< "C : "<<t.c<<endl;
int main()
    Your o;;
    return 0;
To download raw file Click Here


A : 10
B : 20
C : 30

Basic Programs

Flow Control

IF Statement Examples

Switch Case

Goto Statement

Break and Continue

While Loop

Do While Loop

For Loop

Friend Function in C++

String Examples

Array Examples

Structure Examples

Structure & Pointer Examples

Structure & Functions Examples

Enumeration Examples

Template Examples


List of Programs

Pointer Examples

Memory Management Examples

Pointers and Arrays

Virtual Function Examples

Learn All in Tamil © Designed & Developed By Tutor Joes