You are on page 1of 8

2.

A class Time has the following members:


Data member
hour of type integer
minute of type integer
Member function
readTime(int h, int m);
showTime(); //to display the data member
addTime(Time t1, Time t2);
Write a program to input two different objects ft and st, print their sum(assuming 24 hours clock
time)

Ans.
#include<iostream.h>
#include<conio.h>
class Time
{private:
int hours;
int minutes;
public:
void readTime(int h,int m)
{hours=h;
minutes=m;
}
void showTime()
{cout<<"\nHours : "<<hours;
cout<<"\nMinutes : "<<minutes;
}
friend Time addTime(Time t1, Time t2);
};
Time addTime(Time t1,Time t2)
{Time t3;
int temp_min;
temp_min=t1.minutes+t2.minutes;
t3.minutes=temp_min%60;
t3.hours=t1.hours+t2.hours+temp_min/60;
return(t3);
}
void main()
{Time obj1,obj2,obj3;
int hrs,min;
clrscr();
cout<<"Enter the time in hh mm format\n";
cin>>hrs>>min;
obj1.readTime(hrs,min);
cout<<"\nEnter the time in hh mm format\n";
cin>>hrs>>min;
obj2.readTime(hrs,min);
cout<<"\nFirst time is : \n";
obj1.showTime();
cout<<"\n\nSecond time is : \n";
obj2.showTime();
obj3=addTime(obj1,obj2);
cout<<"\n\nResultant time is : \n";
obj3.showTime();
}
Output:
Enter the time in hh mm format
12
30

Enter the time in hh mm format


11
50

First time is :

Hours : 12
Minutes : 30

Second time is :

Hours : 11
Minutes : 50

Resultant time is :

Hours : 24
Minutes : 20
5. Imagine a ticket selling booth at a fair. People visiting the fair ground are required to buy ticket for
Rs. 2.50. and thereby the booth keeps track of the number of people who have visited the fair ground
and the total amount of money collected.
Model this ticket selling booth with a class called Ticket that includes the following members:

Data members:
Number of people visited
Total amount of money collected
Member functions:
To assign initial values
To increment people total as well as amount total when a ticket is sold out
To display the two totals

Include this class in a program to test it.

Ans:
#include<iostream.h>
#include<conio.h>
class ticket
{
int nops;
float amts;
public:
void record(int n)
{nops=n;
amts=n*2.5;
}
int increment(int);
void display(int);
};
int ticket::increment(int n)
{int i;
while(i!=-1)
{cout<<"Enter -1 to stop or the no. of tickets bought"<<endl;
cin>>i;
n+=i;
}
return n;
}
void ticket::display(int n)
{cout<<"No.of people entered : "<<n+1<<endl;
cout<<"Total amt of money collected : Rs "<<(n+1)*2.5<<endl;
}
void main()
{ticket ob1,ob2,ob3;
int nop,amt;
clrscr();
cout<<"Enter the no. of people who have visited the ground"<<endl;
cin>>nop;

ob1.record(nop);
int r=ob2.increment(nop);
ob3.display(r);
}
Output:
Enter the no. of people who have visited the ground
10
Enter -1 to stop or the no. of tickets bought
5
Enter -1 to stop or the no. of tickets bought
-1
No.of people entered : 15
Total amt of money collected : Rs 37.5
1. The monthly telephone bill is to be computed as follows:
Minimum Rs.200 for upto 100 calls.
plus Rs.0.60 per call for next 50 calls
plus Rs.0.50 per call for next 50 calls
plus Rs.0.40 per call for any call beyond 200 calls.
The input contains name of the customer and number of calls made and the desired output is the name
and telephone number to be paid by the customer. Create a class to represent a telephone bill. It
should include the following:
Data members:
Name
Number of calls
Bill amount
Member functions:
To input data
To compute bill
To output the desired information
Using this class, write a program to accomplish the intended task.

Ans:
#include <iostream>
#include <conio.h>
#include <iomanip>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::getline;
using std::fixed;
using std::setprecision;
class Bill
{
string name;
int noOfCalls;
double amount;
public:
void enter();
void calc();
void show();
};

void Bill::enter()
{
cout << "Enter the customer's name: "; getline(cin, name);
cout << "\nEnter the number of calls: "; cin >> noOfCalls;
}

void Bill::calc()
{
int extra = noOfCalls - 100; //since the lowest slab is for 0-100 calls bill is Rs 200
if(extra <= 0) //extra is the no of calls over & above 100
amount = 200;
else if(extra >= 1 && extra <= 50) //no of calls is 101 - 150; so extra calls = 1 to 50
amount = 200 + extra * 0.60;
else if(extra >= 51 && extra <= 100) //no of calls is 151 - 200; so extra calls = 51 to 100
amount = 200 + 50 * 0.60 + (extra - 50) * 0.50;
else if(extra > 100)
amount = 200 + 50 * 0.60 + 50 * 0.50 + (extra - 100) * 0.40;
}

void Bill::show()
{
cout << fixed << setprecision(2);
cout << endl << name << " 's total bill is Rs. " << amount << endl;
}

int main()
{
Bill bl;
bl.enter();
bl.calc();
bl.show();
getch();
return 0;
}

Output:
Enter the customer's name:Padam
Enter the number of calls:50
Padam’s total bill is Rs.200
3. Declare a class called Movie with the following members:
Data members:
Name of the movie
Director’s name
Producer’s name
Grading
Member Functions:
To initialize the data members
To display the movie details

Ans:
Write a program to test this class creating an array of 10 objects.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class Movie
{ private:
char name_mov[25];
char dir_name[25];
char prod_name[25];
public:
void initialize(void)
{ cout<<"Enter the name of the movie:";
gets(name_mov);
cout<<"Enter the director's name:";
gets(dir_name);
cout<<"Enter the producer's name:";
gets(prod_name);
}
void display()
{
cout<<"Movie name:"<<name_mov<<endl;
cout<<"Director's name:"<<dir_name<<endl;
cout<<"Producer's name:"<<prod_name<<endl;
}
};
Movie name[10];
Movie order[10];
void main()
{
for(int i=0;i<10;i++)
{ cout<<"\nEnter details of movie"<<i+1<<":";
name[i].initialize();
}

for(i=0;i<10;i++)
{ cout<<"Movie name"<<endl;
order[i].display();
}
getche() ;
}
Output:
Movie name:k3g
Director’s name:kjo
Producer’s name:kjo
:
:
:
:
:
:
:
Movie name:kank
Director’s name:kjo
Producer’s name:kjo

You might also like