In this post, we will write a C++ program to multiply three numbers using the friend function.
C++ program to multiply three number using friend function.
#include<iostream>
#include<conio.h>
class y;
class z;
class x
{
private:
int data;
public:
x()
{
data = 3;
}
friend int fun(x,y,z);
};
class y
{
private:
int data;
public:
y()
{
data = 2;
}
friend int fun(x,y,z);
};
class z
{
private:
int data;
public:
z()
{
data = 5;
}
friend int fun(x,y,z);
};
int fun(x a, y b, z c)
{
return(a.data*b.data*c.data);
}
int main()
{
system("cls");
x a;
y b;
z c;
std::cout<<"The result is = "<<fun(a,b,c);
getch();
return 0;
}
Output
The result is = 30