This C++ program demonstrates adding integer and float values between two classes using a friend function. It defines two classes called friend1 and friend2 that each contain private integer and float data members. A friend function called calc is declared that accepts objects from each class and calculates/displays the sum of their integer and float values. The main function creates objects of each class, gets input values using get methods, and calls the calc function to output the results.
1. INTEGER AND FLOAT ADDITION USING FRIEND FUNCTION
AIM :
To create a c++ program for addition of Integer and Float using friend function.
ALGORITHM :
STEP 1 : Start the program.
STEP 2 : Declare the class friend 2.
STEP 3 : Define the class friend 1 as follows:
STEP 3.1 : Declare the data member int x and float y in private access specifier.
STEP 3.2 : Define the get() in public as follows:
STEP 3.3 : Get the values for integer x and float y.
STEP 3.4 : Declare the friend function as friend void calc (friend1 f1,friend2).
STEP 4 : Declare the class friend2 as follows:
STEP 4.1 : Declare the data member int x and float y in Private access specifier.
STEP 4.2 : Define the get() in public as follows:
STEP 4.3 : Get the values for integer x and float y.
STEP 4.4 : Declare the friend function as friend void calc(friend1 f1 , friend2 f2).
STEP 5 : Define the friend function calc(friend1 f1 , friend2 f2) as follows:
STEP 5.1 : Calculate and display the Integer Addition.
STEP 5.2 : Calculate and display the Float Addition.
STEP 6 : Define the main() as follows:
STEP 6.1 : Declare the object ‘a’ for friend1 class and object ‘b’ for friend2 class.
STEP 6.2 : Call the get() using object ‘a’ and object ‘b’.
STEP 6.3 : Call the calc (a , b) function.
STEP 7 : End the program.
2. PROGRAM :
//Integer And Float Addition using Friend Function.
#include<iostream>
#include<conio.h>
using namespace std;
class friend2;
class friend1
{
private:
int x;
float y;
public:
void get()
{
cout<<"Enter the value for Friend 1 :"<<"n";
cout<<"Enter the Integer value :";
cin>>x;
cout<<"Enter the Float value :";
cin>>y;
}
friend void calc(friend1 f1,friend2 f2);
};
class friend2
{
3. private:
int x;
float y;
public:
void get()
{
cout<<"Enter the value for Friend 2 :"<<"n";
cout<<"Enter the Integer value :";
cin>>x;
cout<<"Enter the Float value :";
cin>>y;
}
friend void calc(friend1 f1,friend2 f2);
};
void calc(friend1 f1,friend2 f2)
{
cout<<"The Addition of Friend 1 and Friend 2 :"<<"n";
cout<<"The Integer Addition is :"<<f1.x+f2.x<<"n";
cout<<"The Float Addition is :"<<f1.y+f2.y<<"n";
}
int main()
{
friend1 a;
friend2 b;
4. a.get();
b.get();
calc(a,b);
}
OUTPUT :
Enter the value for Friend 1 :
Enter the Integer value :25
Enter the Float value :68.2
Enter the value for Friend 2 :
Enter the Integer value :78
Enter the Float value :90.5
The Addition of Friend 1 and Friend 2 :
The Integer Addition is :103
The Float Addition is :158.7