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.
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
{
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;
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

Friend function by sathya.docx

  • 1.
    INTEGER AND FLOATADDITION 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 AndFloat 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: voidget() { 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 thevalue 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