Jumat, 08 Juli 2011

To find the mean value of a given number using friend function.

ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as Base with data members and member functions.
STEP 3: The function get() is used to read the 2 inputs from the user.
STEP 4: Declare the friend function mean(base ob) inside the class.
STEP 5: Outside the class to define the friend function and do the following.
STEP 6: Return the mean value (ob.val1+ob.val2)/2 as a float.
STEP 7: Stop the program.

PROGRAM:
01#include<iostream.h>
02#include<conio.h>
03class  base
04{
05    int val1,val2;
06   public:
07    void get()
08    {
09       cout<<"Enter two values:";
10       cin>>val1>>val2;
11    }
12    friend float mean(base ob);
13};
14float mean(base ob)
15{
16   return float(ob.val1+ob.val2)/2;
17}
18void main()
19{
20    clrscr();
21    base obj;
22    obj.get();
23    cout<<"\n Mean value is : "<<mean(obj);
24    getch();
25}  

Output:
Enter two values: 10, 20
Mean Value is: 15

Tidak ada komentar:

Posting Komentar