Program for finding maximum number using Friend function for two classes.
#include<iostream.h>
#include<conio.h>
#include<limits.h>
class C2;
class C1
{
int v1,v2,v3;
public:
void getdata1()
{
cout<<"Enter 3 values of Class C1 : ";
cout<<"\nEnter value 1 : ";
cin>>v1;
cout<<"Enter value 2 : ";
cin>>v2;
cout<<"Enter value 3 : ";
cin>>v3;
}
friend void find_max(C1,C2);
};
class C2
{
int v4,v5,v6;
public:
void getdata2()
{
cout<<"\nEnter 3 values of Class C2 : "<<endl;
cout<<"Enter value 4 : ";
cin>>v4;
cout<<"Enter value 5 : ";
cin>>v5;
cout<<"Enter value 6 : ";
cin>>v6;
}
friend void find_max(C1,C2);
};
void find_max(C1 obj1, C2 obj2)
{
int max1;
int max2;
if(obj1.v1>obj1.v2 && obj1.v1>obj1.v3)
{
max1 = obj1.v1;
}
else if(obj1.v2>obj1.v1 && obj1.v2>obj1.v3)
{
max1 = obj1.v2;
}
else
{
max1 = obj1.v3;
}
if(obj2.v4>obj2.v5 && obj2.v4>obj2.v6)
{
max2 = obj2.v4;
}
else if(obj2.v5>obj2.v4 && obj2.v5>obj2.v6)
{
max2 = obj2.v5;
}
else
{
max2 = obj2.v6;
}
cout<<endl<<"Maximum number of values given in Class C1 : "<<max1<<endl;
cout<<"Maximum number of values given in Class C2 : "<<max2<<endl;
}
void main()
{
clrscr();
C1 c1_obj;
c1_obj.getdata1();
C2 c2_obj;
c2_obj.getdata2();
cout<<"\nFinding Maximum number using Friend Function...";
find_max(c1_obj,c2_obj);
getch();
}
OUTPUT :
No comments:
Post a Comment