Professional Documents
Culture Documents
Exp 4 - Prog 15-17
Exp 4 - Prog 15-17
Exp 4 - Prog 15-17
PROGRAM 15
/*To implement a class ‘A’ that has two float-type data members ‘num1’ and ‘num2’, a
member function ‘SetValue( )’ that assigns value to these data members and a friend
function ‘Mean( )’ that calculates the mean of these data members.*/
CODE:
#include <iostream>
class A
{
float num1 , num2;
public:
void SetValue(float x , float y)
{
num1 = x;
num2 = y;
}
float Mean(A a)
{
return ((a.num1 + a.num2) / 2);
}
int main()
{
float x , y;
A a1;
cout<<"Enter two numbers: ";
cin>>x>>y;
a1.SetValue(x,y);
cout<<"Mean value of the given numbers is "<<Mean(a1);
return 0;
}
OUTPUT:
Enter two numbers: 5 6
Mean value of the given numbers is 5.5
PROGRAM 16
/*To implement two classes ‘A’ and ‘B’, each with a data member ‘x’ and a member function
‘GetData( )’ that assigns value to their respective data member, and a friend function ‘Add( )’
that adds up the data members of these classes.*/
CODE:
#include <iostream>
class B;
class A
{
float x;
public:
void GetData(float p)
{ x = p; }
class B
{
float x;
public:
void GetData(float p)
{ x = p; }
float Add(A a , B b)
{
return (a.x + b.x);
}
int main()
{
float p1 , p2;
A a1;
B b1;
a1.GetData(p1);
b1.GetData(p2);
return 0;
}
OUTPUT:
Enter the data member for class A: 7
Enter the data member for class B: 9
Sum of the data members of classes A and B = 16
PROGRAM 17
/*To swap the values of the private data members of two classes. (Take into consideration
just one data member in each class.)*/
CODE:
#include <iostream>
class B;
class A
{
float x;
public:
void GetData(float p)
{ x = p; }
void ShowData()
{ cout<<x<<"\t"; }
class B
{
float y;
public:
void GetData(float q)
{ y = q; }
void ShowData()
{ cout<<y<<"\t"; }
int main()
{
float p1 , q1;
A a1;
B b1;
return 0;
}
OUTPUT:
Enter the data member for class A: 5
Enter the data member for class B: 10
Data members of classes A and B before swapping -> 5 10
Data members of classes A and B after swapping -> 10 5