C++ friend functions for adding two numbers: This CPP example tutorial is used to calculate and adds the 2 numbers that are given by the user. This source code is getting 2 input values and based on that, it performs an operation and finally provides an actual output. This code is available for free of use. Anyone can obtain this code for their personal purposes. Feel free to share about us in the Social networking websites.
C++ friend function example:
Q: Write a simple friend function program in C++ Or simple example of friend function in C++ for adding two numbers.
[Useful: C Program Tutorials] & [IT Companies Details]
// Header files #include<iostream.h> #include<conio.h> class Green; //Forward declaration class Red { int x; // Variable declaration public : void get() //method { cout<<"\n \t Give input value for Red: "; // © http://students3k.com - Karthikh Venkat cin>>x; //Getting input } friend void add(Red,Green); // Friend function }; class Green { int y; public : void get() { cout<<"\n \t Give input value for Green {Second Value}: "; cin>>y; // 2nd input value } friend void add(Red,Green); // Friend function }; // © http://students3k.com - Karthikh Venkat void add(Red inp1, Green inp2) // Addition Function { // Friend function to Add 2 Numbers. cout<<"\n\t"<<inp1.x<<" + "<<inp2.y<<" = "<<inp1.x+inp2.y; } void main() { Red inp1; Green inp2; clrscr(); cout<<"\n\t Friend Function Program in C++ \n "; // © http://students3k.com - Karthikh Venkat cout<<"\n\t Input Values \n"; inp1.get(); inpp2.get(); cout<<"\n\t Output Values \n"; add(inp1,inp2); getch(); }