Lab 5 Oop
Lab 5 Oop
Lab 5 Oop
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;
}
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.
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){
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;
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;
}
Total 40 Signature