0% found this document useful (0 votes)
49 views6 pages

Oops Lab 5 Assignment

This document contains the solutions to 3 questions asked as part of an OOPS lab assignment. Question 1 involves creating a class to store book details and displaying books in a given price range using a friend function. Question 2 involves creating a student class and finding the student with the highest average marks using member and friend functions. Question 3 involves creating an employee class and inputting and displaying details of 3 employees using an object array.

Uploaded by

Mondal Thakral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views6 pages

Oops Lab 5 Assignment

This document contains the solutions to 3 questions asked as part of an OOPS lab assignment. Question 1 involves creating a class to store book details and displaying books in a given price range using a friend function. Question 2 involves creating a student class and finding the student with the highest average marks using member and friend functions. Question 3 involves creating an employee class and inputting and displaying details of 3 employees using an object array.

Uploaded by

Mondal Thakral
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

OOPS LAB 5 ASSIGNMENT

ROLL NO. 2005802

NAME: ISHAAN THAKRAL


BRANCH: CSE
DATE: 26/08/21
Q1.
Create a class which stores name, author and price of a book. Store information for n number
of books. Display information of all the books in a given price range using
friend function.

CODE:

#include <iostream>
#include <string.h>

using namespace std;


class Store
{
string name;
string author;
float price;
public:
void getbooks()
{
cout << "Enter Name of the Book -> " << endl;
cin >> name;
cout << "Enter Author's Name -> " << endl;
cin >> author;
cout << "Enter Book Price -> " << endl;
cin >> price;
}
friend void check(Store b[], int n);
};
void check(Store b[], int n)
{
int i;
int min, max;
cout << "Enter Minimum Range -> " << endl;
cin >> min;
cout << "Enter Maximum Range -> " << endl;
cin >> max;
cout << "The Books in the Range " << min << " to " << max << " are: " << endl;
for (i = 0; i < n; i++)
{
if (b[i].price >= min && b[i].price <= max)
{
cout << "Title :- " << b[i].name << endl;
cout << "Author :- " << b[i].author << endl;
cout << "Price :- " << b[i].price << endl;
}
else
{
cout << "No Books are avaliable in this price range !" << endl;
}
}
}
int main()
{
int n, i;
cout << "Enter No. of Books -> " << endl;
cin >> n;
Store b[n];
for (i = 0; i < n; i++)
{
b[i].getbooks();
}
check(b, n);
return 0;
}

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:

You might also like