Lab 5 Oop

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 05
(Spring 2018)

Course: Object Oriented Programming Lab Date:


Course Code: CSL - 210 Max Marks: 40
Faculty’s Name: Aasim Ali Lab Engineer: Shoaib Khan

Name: Mohammad Jasim Waqar Enroll No: 03-135172-055

Task 1:
Create a class called time that has separate int member data for hours, minutes, and seconds.
One constructor should initialize this data to 0, and another should initialize it to fixed values.
Another member function should display it, in 11:59:59 format. The final member function
should add two objects of type time passed as arguments.
Main( ) program should create two initialized time objects and one that isn’t initialized. Then
it should add the two initialized values together, leaving the result in the third time variable.
Finally it should display the value of this third variable. (NOTE: Make appropriate member
functions const.)
ANSWER:
#include<iostream>
using namespace std;

class time {

int hours;
int minutes;
int second;

public :

time(){
hours=0;
minutes=0;
second=0;
}

time(int h , int m , int s ){


hours=h;
minutes=m;
second=s;
}

int gethours() const {


return hours;
}
int getminutes()const {
return minutes;
}

Department of Computer Science, BULC


int getsecond() const {
return second;
}
void setHour(int h){
hours=h;
}
void setMinutes(int m){
minutes=m;
}
void setSecond(int s){
second=s;
}
void display(){
cout<<hours<<":"<<minutes<<":"<<second<<endl;
}
};

time addtime(const time t1 ,const time t2) {


time t3;
int h=t1.gethours() + t2.gethours();
if (h>23){
h-=24;
}
int m=t1.getminutes() + t2.getminutes();
if(m>59){
m-=60;
h++;
}
int s=t1.getsecond() + t2.getsecond();
if(s>59){
s-=60;
m++;
}
t3.setHour(h);
t3.setMinutes(m);
t3.setSecond(s);
return t3;
}

int main(){
time t;
time t1(2,37,22);
time t2(3,22,21);
time t3;
t3=addtime(t1,t2);
t3.display();
return 0;
}

Task 2:
 Write a program that defines a class Area , with data members PI=3.1419 and radius r.
Radius is input by user. The program should use data members and the functions listed
to accomplish the following.
 Prompt the user to input the value of a double variable r, which stores the radius of a
sphere.

 Create a function to calculate Surface area of Sphere

Department of Computer Science, BULC


 Create a function to print Surface area of the sphere. 4*PI*R.

 (Note: Make proper variables and functions constant).


#include<iostream>
#include<string>
using namespace std;
class sphere{
private:
float PI;
float R;
public:
void getr(const double r){
PI=3.14;
R=r;
}

void calculate()const {
double area =4*PI*R*R;
}

void display()const {
cout<<"AREA = "<<4*PI*R*R<<endl;
}

};

int main(){
double r;
cout<<"Enter value : ";
cin>>r;
sphere obj;
obj.getr(r);
obj.calculate();
obj.display();
return 0;
}

Task 3:
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.
#include<iostream>
#include<string>
using namespace std;
class Age{
private:
string name;
int dob;
int age;

public:
void set_name(const string n){

Department of Computer Science, BULC


name=n;
}
void calculate(const int y) {
age=2018-y;
}

void display() const {


cout<<name<<" your age is "<<age<<endl;
}
};

int main(){

int a,b,c;
string n;
cout<<"Enter Your name :: ";
cin>>n;
cout<<"Enter your Date of Birth :: Date : month : year :: ";
cin>>a>>b>>c;
Age obj;
obj.set_name(n);
obj.calculate(c);
obj.display();
return 0;
}

Task 4:
A book shop maintains the inventory of books that are being sold at the shop. The list includes
details such as author, title, price, publisher and stock position. Whenever a customer wants a
book, the sales person inputs the title and author and the system searches the list and displays
weather it available or not. If it is not, an appropriate message is displayed. If it is, then the
system displays the book details and request for the number of copies required. If the
requested copies are available, the total cost of requested copies is displayed; otherwise the
message “Required copies not in the stock” is displayed. Design a system using a class called
books with suitable member functions and constructors. Use new operator in constructors to
allocate memory space if required. Provide these extra facilities in the program:

 The price of the books should be updated as and when required. Use a private member
function to implement this.
 The stock value of each book should be automatically updated as soon as a transaction
is completed.
The number of successful and unsuccessful transactions should be recorded for the purpose of
statistical analysis. Use static data members to keep count of transactions.
ANSWER:
#include<iostream>
#include<string>
using namespace std;

class book{
private :
string author, title, publisher, stock,name;
int price, copies;

Department of Computer Science, BULC


public :
book (string n,string au){
name="computer";
author="David";
if (n == name && au == author ){
cout<<"\nIt is available ";
display();
}
else
cout<<"\nIts is not avialabe";
}

void display(){
cout<<"Book Name : "<<name<<endl;
cout<<"Authar name : "<<author<<endl;
cout<<"Price = 350"<<endl;
cout<<"palce on the 3rd floor "<<endl;
rate();
}

void rate(){
static int p;
int s;
cout<<"How many copies you want ";
cin>>s;
if (s>100)
cout<<"We have not that much books sorry ";
else
cout<<"total price which you have to pay for the book"<<s<<" Books = "<<name<<"
= "<<s*350<<endl;
cout<<"Tansacition number : "<<p;
p++;
}
};

int main(){
int a;
string b,n;
cout << ".................WELCOME TO OUR LIBRARY.........................."<<endl;
cout<<"\n\nEnter Book name : ";
cin>>b;
cout<<"Enter Authour name : ";
cin>>n;
book obj(b,n);
return 0;
}

Lab Grading Sheet :


Max Obtained
Task Comments(if any)
Marks Marks
1. 10
2. 10
3. 10
4. 10

Total 40 Signature

Department of Computer Science, BULC


Note : Attempt all tasks and get them checked by your Lab Instructor.

Department of Computer Science, BULC

You might also like