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?
Chetan Shidling Staff asked 5 years ago

I need short information.

1 Answers
Chetan Shidling Staff answered 5 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