You are on page 1of 8

INDEX

S.No Name of the Experiment Date Teacher


Sign
Practical 6

Aim: Write a program to accept five different numbers by creating a class called friendfunc1
and friendfunc2 taking 2 and 3 arguments respectively and calculate the average of these
numbers by passing object of the class to friend function.

Description: Create two class friendfun1 and friendfun2 and a function named average
which is friend of both the class that take two objects as input.

Algorithm:
1. First declare a class named friendfun1. And then create another class friendfun2. 2.
In friendfun2 class declare 3 variables as public and a function to take input. Also
create a friend function name average that takes two objects as input one object of
friendfun1 and another of friendfun2.
3. Define the friendfun1 class. 2 variables as public and a function that takes input.
Also, a friend function average.
4. Define the average function that find the average of all the 5 numbers.

Source Code:

#include<iostream>
using namespace std;
class friendfun2;
class friendfun1 {
public:
float a,b;
friendfun1(float a, float b) {
this->a = a;
this->b = b;
}
friend void average(friendfun1 f1, friendfun2 f2);
};
class friendfun2 {

public:
float x,y,z;
friendfun2(float x, float y, float z) {
this->x = x;
this->y = y;
this->z = z;
}
friend void average(friendfun1 f1, friendfun2 f2);
};

void average(friendfun1 f1, friendfun2 f2) { float


average = (f1.a + f1.b + f2.x + f2.y + f2.z)/5;
cout<<"Average of the 5 number is "<< average<<endl;
}

int main() {
cout<<"Enter the two number first \n";
float a, b;
cin>>a>>b;
friendfun1 f1 = friendfun1(a,b);
cout<<"Enter next three number \n";
float x, y, z;
cin>>x>>y>>z;
friendfun2 f2 = friendfun2(x,y,z);
average(f1,f2);
return 0;
Output:

Findings and Learnings:


From this program we find and learn how to use friend function to access the class members
and do the required work.
Practical 7

Aim: Write a program to accept the student detail such as name and 3 different marks by
get_data() method and display the name and average of marks using display() method. Define
a friend class for calculating the average of marks using the method mark_avg().

Description: Create a class named student and declare variables such as three float
numbers, string name and a float average number.

Algorithm:
1. Declare a string name to store the name of the student.
2. Declare three float variables to store the student marks also declare another float
variable average to store the average of all the marks.
3. Create two function get_data() to take input name and marks display() to display all
the details.
4. Create a friend class fstud.
5. In class fstud declare a function average() that calculates the average of the marks of
class student.

Source Code:

#include<iostream>
using namespace std;

class student {
string name;
float a,b,c,avg;
public:
void getdata() {
cout<<"Enter the name of student: ";
cin>>name;
cout<<endl;
cout<<"Enter the marks obtained in 3 subjects: ";
cin>>a>>b>>c;
cout<<endl;
}
void display() {
cout<<"Name: "<<name<<endl;
cout<<"Marks obtained in 3 subjects are: "<<a<<" "<<b<<" "<<c<<" "<<endl;
cout<<"Average is: "<<avg<<endl;

}
friend class fstud;
};

class fstud {
public:
void totalavg(student &s) {
s.avg = (s.a + s.b+ s.c)/3.0;
}
};

int main() {
student s;
fstud f;
s.getdata();
f.totalavg(s);
s.display();
return 0;
}
Output:

Findings and Learnings:


1. With the help of this program, we will understand how to use friend class.
2. With the help of friend class, we can use other class variable.
3. We can also store the info as permanent with the help of friend class.

You might also like