Vous êtes sur la page 1sur 4

ASSIGNMENT NO.

3:DATE:PROBLEM STATEMENT:-Create class employee whose private data members name,


e_id, salary. Public member of this class is input () to take the details of an employee and
display () to display details of an employee. Now calculate heighst salary of 5 members
using friend function heighst () and show the details of that employee.

ALGORITHIM:heighst( )
step 1:- Read the array of object of employee
step 2:- Take the i/p i,j,val= emp[0].salary.
step 3:- Start for loop ( i=1 to 5 by 1)
step 4:check if(val<emp[i].salary) then goto step6
step 5:- val=emp[i].salary;
step 6:- Start for loop ( j=0 to5 by 1)
step 7:- check if(val==emp[j].salary) and print followings
step 8:-emp[j].name;
step 9:-emp[j].em_id;
step 10:- emp[j].salary;

Program Code:#include<iostream>
using namespace std;
class employee
{
private:
int salary;
int em_id;
char name[50];
public:
void insert();
void display();
friend void heighst(employee emp[]);
};
void employee::insert()
{
cout<<"\nenter the name";
cin>>name;
cout<<"\nenter the id";
cin>>em_id;
cout<<"\nenter the salary";
cin>>salary;
}
void employee::display()
{

cout<<"\nenter the name";


cout<<name;
cout<<"\nenter the id";
cout<<em_id;
cout<<"\nenter the salary";
cout<<salary;
}
void heighst(employee emp[])
{
int val=emp[0].salary;
for(int i=0;i<5;i++)
{
if(val<emp[i].salary)
{
val=emp[i].salary;
}
}
for(int j=0;j<5;j++)
{
if(val==emp[j].salary)
{
cout<<"\n"<<emp[j].name;
cout<<"\n"<<emp[j].em_id;
cout<<"\n"<<emp[j].salary;
}

}
int main()
{
employee emp[5],emp1;
for(int i=0;i<5;i++)
{
emp[i].insert();
}
for(int j=0;j<5;j++)
{
emp[j].display();
}
heighst(emp);
}

Output:enter the namemrinmoy


enter the id56
enter the salary2000
enter the namerohit

enter the id58


enter the salary23000
enter the namesupratim
enter the id57
enter the salary52300
enter the namekakuli
enter the id52
enter the salary52222
enter the namearijit
enter the id42
enter the salary22222
enter the namemrinmoy
enter the id56
enter the salary2000
enter the namerohit
enter the id58
enter the salary23000
enter the namesupratim
enter the id57
enter the salary52300
enter the namekakuli
enter the id52
enter the salary52222
enter the namearijit
enter the id42
enter the salary22222
supratim
57
52300

DISCUSSION:-

1. Friend function:-c++ allows the common function to be made


friendly with both the classes, thereby allowing the function to have
access to the private data of these classes. Such a function need not
be a member of any of these classes. The function can be defined
anywhere in the program. Like in our program the friend function
heighst.
2. Array of object:-in c++ we can also have array of variables that are of
the type class, such variables are called arrays of objects. These
arrays of objects are stored inside the memory in the same way as
the multi-dimensional array.

Vous aimerez peut-être aussi