CS Electrical And Electronics
@cselectricalandelectronics

Write a Cpp program to display two numbers using friend function?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to display two numbers using friend function?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

1 Answers
CS Electrical And Electronics Staff answered 4 years ago

Code:

#include <iostream>
using namespace std;
class sample
{
int a;
float b;
friend void print(sample);
};
void print(sample s)
{
s.a = 10;
s.b = 20;
cout<<“a:”<<s.a<<endl;
cout<<“b:”<<s.b<<endl;
}
int main()
{
sample s;
print(s);
return 0;
}

Output:

a:10
b:20