Write a C++ program using friend function within two classes. Example of Friend function.

#include<iostream>
#include<iomanip>
using namespace std;
class B;   //prototype for class B
class A
{
    public:
        int x=10;
        int y=20;
    public:
        friend void printdata(A,B);
};
class B
{
    public:
        int x=30;
        int y=40;
    public:
        friend void printdata(A,B);
};
void printdata(A obj1,B obj2)

{
    cout<<obj1.x+obj2.x<<endl;

    cout<<obj1.y+obj2.y<<endl;
}
int main()
{   
    A S1;
    B S2;

    printdata(S1,S2);

}

Comments

Most Popular Posts Of All Time