Oop Lab 3
Oop Lab 3
Objective(s):
Upon completion of this lab session, learners will be able to:
Array of Objects
Const Members
Lab Tasks:
Task 1:
Define a Class BOOK with the following specifications:
Data Members:
bookNo
bookTitle
price
Member Functions:
total_cost( ) A function to calculate the total cost for N no of copies where N is
passed to function as argument.
take_data( ) A function to input bookNo, bookTitle, price.
purchase_info( ) A function to ask the user to input the number of copies to be
purchased of a book. It invokes total_cost( ) and prints the total
cost paid by the user.
Write a menu driven program. Get data for five book from user.
Task 1:
#include<iostream>
#include<string>
using namespace std;
class book{
public:
int n,price;
string tittle;
void showdata()
{cout << "Book Tittle : " << tittle << endl;
cout << "Book Price : " << price << endl;
cout << "Book Id : " << n << endl;
}
void getdata()
{ cout << "Enter Book Tittle : "; cin >> tittle;
cout << "Enter Book Price : "; cin >> price;
cout << "Enter Book # : "; cin >> n;
%
Enrollment Number: ____________________________
};
int main(){
book a[5];
int ch, op, op2;
do
{cout << "1)-Admin" << endl;
cout << "2)-User" << endl;
cout << "3)-Exit" << endl;
cin >> ch;
if (ch == 1){
cout << "1)-Enter books Data" << endl;
cout << "2)-Display books Data" << endl;
cin >> op;
if (op == 1){
for (int i = 0; i <5; i++)
{ a[i].getdata();}
}
if (op == 2){
for (int i = 0; i < 5; i++)
{ a[i].showdata();}}}
else if (ch == 2){
cout << "1:-Purchase Book" << endl;
cout << "1:-Display Book Data" << endl;
cin >> op2;
if (op2 == 1){
int b, y, total;
int p = 1;
cout << "Enter the book id" << endl;
cin >> b;
for (int i = 0; i < 5; i++)
if (b == a[i].n){
cout << "Book Price is :" << a[i].price;
cout << "Mentioned copies you want to buy ?"; cin
>> y; cout << endl;
p = (a[i].price)*y;
}
cout << "Total price is " << p << endl;
}
else if (op2 == 2){
for (int i = 0; i < 5; i++)
{
a[i].showdata();
}
}
}
system("pause");
return 0;
Page 2 of 6
%
Enrollment Number: ____________________________
Task 2:
Write a program with a class that contains following data members
Id
name
marks
Create and array of objects of size 10. Create a function read() to take input from user. Create
a function to calculate percentage of each student. Then create a function that displays the
student id and name of the student with the lowest percentage.
Task 2:
#include<iostream>
#include<string>
using namespace std;
class student{
student()
{
id = 0;
marks = 0;
name = "no-name";
void read()
{
void percentage()
{
double total = 500;
Page 3 of 6
%
Enrollment Number: ____________________________
double percheck()
{
return per;
}
};
int main()
{
student a[3];
for (int i = 0; i < 3; i++)
{
a[i].read();
a[i].percentage();
double lp = a[0].percheck();
for (int i = 0; i < 3; i++)
{
if (lp>a[i].percheck())
{
lp = a[i].percheck();
}
}
system("pause");
return 0;
}
Task3:
Create a class personAge with following data members: name, dob, age. Calculate age by
taking name and dob as input from user. Make appropriate constant functions and variables.
Page 4 of 6
%
Enrollment Number: ____________________________
Task 3:
#include<iostream>
#include<string>
using namespace std;
class personAge
{
string age;
string name;
int yr,mn,dy,ageyr,agemn,agedy;
public:
personAge()
{
void takeage()
{
cout << "Enter Name :";
getline(cin, name);
cout << "Enter Date of Birth .....Enter year of birth :";
cin >> yr;
cout << "Enter month of birth in integer:";
cin >> mn;
cout << "Enter day of birth :";
cin >> dy;
ageyr = 2020 - yr;
agemn = 12 - mn;
agedy = 31 - dy;
void agee()const
{
cout << name;
cout << "age is :" << agedy << "-" << agemn << "-" << ageyr << endl;
};
int main()
{
personAge a;
a.takeage();
a.agee();
return 0;
Page 5 of 6
%
Enrollment Number: ____________________________
Note : Attempt all tasks and get them checked by your Lab Instructor.
Page 6 of 6