LAB TASK 2
LAB TASK 2
LAB TASKS
TASK 3
Write a class Book with three data members book id, Pages and Price. It also contains the
following member function: • The get () function is used Io input values. • The show ()
function is used Io display values. • The set () function is used to set values of data members
using parameters The get Price () function is used to return the value of Price The program
should create two objects of the class and input values tor these objects. The program
should display the details of most costly book.
#include<iostream>
using namespace std;
class Book {
private:
int book_id;
int pages;
float price;
public:
void get() {
void show() {
book_id = id;
pages = p;
price = pr;
float getPrice() {
return price;
};
int main() {
book1.get();
book2.get();
}
TASK 4
Write a class Result that contains roll no, name and marks of three subjects. The marks
are stored in an array of integers. The class also contains the following member functions:
The program should create an object of the class and call the member functions.
#include<iostream>
using namespace std;
class Result {
private:
int roll_no;
string name;
int marks[3];
public:
void input() {
cout << "Enter Roll No: ";
cin >> roll_no;
cin.ignore();
cout << "Enter Name: ";
getline(cin, name);
cout << "Enter marks for three subjects:\n";
for (int i = 0; i < 3; i++) {
cout << "Marks of subject " << i + 1 << ": ";
cin >> marks[i];
}
}
void show() {
cout << "\nRoll No: " << roll_no << endl;
cout << "Name: " << name << endl;
cout << "Marks in three subjects: ";
for (int i = 0; i < 3; i++) {
cout << marks[i] << " ";
}
cout << endl;
}
int total() {
int total_marks = 0;
for (int i = 0; i < 3; i++) {
total_marks += marks[i];
}
return total_marks;
}
float avg() {
return total() / 3.0;
}
};
int main() {
Result student;
student.input();
student.show();
return 0;
}