Site icon T4Tutorials.com

C++ Program Friend Function to display the reverse of a number

C++ Program Friend Function to display the reverse of a number

Write a C++ Program to display the reverse of a number using the Friend function.

If we declare a function friend void show(T4Tutorials); as a friend in a class T4Tutorials then this function friend void show(T4Tutorials); can access the private and protected members of the classT4Tutorials. You must know that a global function can also be declared as a  friend function of the class.

Syntax of friend function in C++

class class_name
{

friend return_type function_name(arguments);

}

#include<iostream>
using namespace std;
class T4Tutorials
{
	private:
		int n,i;
	public:
		T4Tutorials()
		{
			cout<<"Enter Number to Display reverse: ";
			cin>>n;
		}
		friend void show(T4Tutorials);
};
void show(T4Tutorials r)
{
	 cout<<"The reverse the Entered number: ";
			for(r.i=r.n;r.i>0;r.i=r.i/10)
			{
				cout<<r.i%10;
			}
}
int main()
{
    T4Tutorials r;
    show(r);
}

Output

Enter number to reverse: 7812

The reverse of the Entered number: 2187

Exit mobile version