Friend Class Example in C++


Description:

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;
public:
    int c;
    friend Your;
};
class Your
{
public:
    void fun()
    {
        My t;
        t.a=10;
        t.b=20;
        t.c=30;
        cout<< "A : "<<t.a<<endl;
        cout<< "B : "<<t.b<<endl;
        cout<< "C : "<<t.c<<endl;
    }
};
int main()
{
    Your o;
    o.fun();
    return 0;
}
To download raw file Click Here

Output

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


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples



Learn All in Tamil © Designed & Developed By Tutor Joes