Skip to main content

C++ Program On Friend Class-Find Mean Of Two Numbers.


This is a sample program On using Friend Classes. Using Friend class, here we try to find the mean of two numbers.


#include <iostream.h>
#include <conio.h>
class num
{
    int a,b;

    public:

       void read()
         {
            cout<<"Enter Two Numbers: ";
            cin>>a>>b;
         }

       friend void mean(num x)
         {
            int k;
            k=(x.a+x.b)/2;
            cout<<"Arithmetic Mean: "<<k;
         }

};

void main()
{
    clrscr();
    num n;
    n.read();
    mean (n);
    getch();
}


OUTPUT:

Enter Two Numbers:
18
12

Arithmetic Mean: 15



Comments