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

LAB TASK 2

The document outlines two programming tasks involving class creation in C++. The first task requires creating a 'Book' class with methods for inputting, displaying, and managing book details, while the second task involves a 'Result' class to handle student information and marks. Both tasks include sample code demonstrating the implementation of the classes and their functionalities.

Uploaded by

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

LAB TASK 2

The document outlines two programming tasks involving class creation in C++. The first task requires creating a 'Book' class with methods for inputting, displaying, and managing book details, while the second task involves a 'Result' class to handle student information and marks. Both tasks include sample code demonstrating the implementation of the classes and their functionalities.

Uploaded by

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

LAB 1 task

NAME: SARMAD ADEEL MOAVIA

STUDENT ID: F2024408246

SUBMITTED TO: Mr. Jawad Akhtar

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:

// Function to input values

void get() {

cout << "Enter Book ID: ";

cin >> book_id;

cout << "Enter number of pages: ";

cin >> pages;

cout << "Enter price of the book: ";

cin >> price;

// Function to display values

void show() {

cout << "Book ID: " << book_id << endl;

cout << "Number of Pages: " << pages << endl;


cout << "Price: " << price << endl;

// Function to set values of data members using parameters

void set(int id, int p, float pr) {

book_id = id;

pages = p;

price = pr;

// Function to get the price of the book

float getPrice() {

return price;

};

int main() {

Book book1, book2;

cout << "Enter details for Book 1:\n";

book1.get();

cout << "Enter details for Book 2:\n";

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 input () function is used to input the values in data members

• The show () function is used to displays the value of data members

• The total () function returns the total marks of a student.

• The avg () function returns the average marks of a student.

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();

cout << "Total Marks: " << student.total() << endl;


cout << "Average Marks: " << student.avg() << endl;

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy