Oops Lab 5 Assignment
Oops Lab 5 Assignment
CODE:
#include <iostream>
#include <string.h>
OUTPUT:
Q2.
Write a program to create a class to store details of a student (name, roll and 3 subject marks ).
Input details for 2 students and assign all the details of that student who is having higher average
mark to a new student.
i) use member function
ii) use friend function
CODE:
#include <iostream>
#define MAX 2
using namespace std;
class Student
{
private:
char name[30];
int roll;
float marks[3];
float avg;
public:
void get()
{
avg = 0;
cout << "Enter name: ";
cin >> name;
cout << "Enter Roll: ";
cin >> roll;
for (int i = 0; i < 3; i++)
{
cout << "Enter marks (" << i + 1 << ") : ";
cin >> marks[i];
avg += marks[i];
}
avg /= 3;
}
void display()
{
cout << "Name: " << name << "\tRoll no.: " << roll << "\tAverage marks: ";
cout << avg << endl;
}
Student maximum(Student s2)
{
if (avg > s2.avg)
return *this;
return s2;
}
friend Student maximum(Student, Student);
};
Student maximum(Student s1, Student s2)
{
if (s1.avg > s2.avg)
return s1;
return s2;
}
int main()
{
Student s_arr[MAX];
for (int i = 0; i < MAX; i++)
{
cout << "Details of Student no.(" << i + 1 << ") :\n";
s_arr[i].get();
}
cout << "\nMax: (Member fn):\n";
Student s3 = s_arr[0].maximum(s_arr[1]);
s3.display();
cout << "\nMax: (Friend fn):\n";
Student s4 = maximum(s_arr[0], s_arr[1]);
s4.display();
return 0;
}
OUTPUT:
Q3.
Write a program to create a class to store details of an employee like name , age, job_id and
department name(common to all). Input display details of 3 employees using array of objects
Concept.
CODE:
#include <iostream>
#include <string>
using namespace std;
class employee
{
public:
string name;
int age;
int job_id;
string d_name;
};
int main()
{
employee *emp = new employee[3];
for (int i = 0; i < 3; i++)
{
cout << "Enter employee name: ";
getline(cin, emp[i].name);
cout << "Enter employee age: ";
cin >> emp[i].age;
cout << "Enter employee job id: ";
cin >> emp[i].job_id;
cin.ignore();
cout << "Enter employee department: ";
getline(cin, emp[i].d_name);
cout << endl;
}
for (int i = 0; i < 3; i++)
{
cout << "Employee name: " << emp[i].name << endl;
;
cout << "Employee age: " << emp[i].age << endl;
cout << "Employee job id: " << emp[i].job_id << endl;
cout << "Employee department: " << emp[i].d_name << endl;
cout << endl;
}
}
OUTPUT: