Exp 4 - Prog 15-17

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

EXPERIMENT 4

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>

using namespace std;

class A
{
float num1 , num2;

public:
void SetValue(float x , float y)
{
num1 = x;
num2 = y;
}

friend float Mean(A a);


};

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>

using namespace std;

class B;

class A
{
float x;

public:
void GetData(float p)
{ x = p; }

friend float Add(A , B);


};

class B
{
float x;

public:
void GetData(float p)
{ x = p; }

friend float Add(A , B);


};

float Add(A a , B b)
{
return (a.x + b.x);
}
int main()
{
float p1 , p2;
A a1;
B b1;

cout<<"Enter the data member for class A: ";


cin>>p1;
cout<<"Enter the data member for class B: ";
cin>>p2;

a1.GetData(p1);
b1.GetData(p2);

cout<<"Sum of the data members of classes A and B = "<<Add(a1 , b1)<<endl;

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>

using namespace std;

class B;

class A
{
float x;

public:
void GetData(float p)
{ x = p; }

void ShowData()
{ cout<<x<<"\t"; }

friend void Swap(A & , B &);


};

class B
{
float y;

public:
void GetData(float q)
{ y = q; }

void ShowData()
{ cout<<y<<"\t"; }

friend void Swap(A & , B &);


};

void Swap(A &a , B &b)


{
float temp;
temp = a.x;
a.x = b.y;
b.y = temp;
}

int main()
{
float p1 , q1;
A a1;
B b1;

cout<<"Enter the data member for class A: ";


cin>>p1;
cout<<"Enter the data member for class B: ";
cin>>q1;
a1.GetData(p1);
b1.GetData(q1);
cout<<"Data members of classes A and B before swapping -> ";
a1.ShowData();
b1.ShowData();

cout<<endl<<"Data members of classes A and B after swapping -> ";


Swap(a1 , b1);
a1.ShowData();
b1.ShowData();

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

QUESTIONS FOR VIVA -


1) What is a friend function?
A friend function is a function that is not a member function of a class but it still has
access to all the members of the class.
Such a function is required when there is a need of a function that can work in
common for more than one classes.

2) Where should we declare a friend function in a class?


A friend function can be declared anywhere in a class declaration. Its functioning is
not bothered by the access specifiers of class.

3) How do we call a friend function in a program?


Since a friend function is not a member function of a class, it does not come under
the scope of the class. Thus, we don’t need any member selection operators like ‘.’ or
‘->’ to call a friend function for a class. It can be simply invoked like a normal
function of a program by just writing its name and passing the required parameters.

4) A friend function can be a friend for how many classes?


There is no limit for this.

You might also like