Labmdterm
Labmdterm
Sahiwal Campus
(Department of Computer Science)
Q.no.1 Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a
50 cent toll. Mostly they do, but sometimes a car goes by without paying.
The tollbooth keeps track of the number of cars that have gone by, and of the total
amount of money collected.
Model this tollbooth with a class called tollBooth. The two data items are a type
unsigned int to hold the total number of cars, and a type double to hold the total
amount of money collected. A constructor initializes both of these to 0. A member
function called payingCar() increments the car total and adds 0.50 to the cash total.
Another function, called nopayCar(), increments the car total but adds nothing to the
cash total. Finally, a member function called display() displays the two totals. Make
appropriate member functions const.
Include a program to test this class. This program should allow the user to push one
key to count a paying car, and another to count a nonpaying car. Pushing the Esc key
should cause the program to print out the total cars and total cash and then exit.
Q2. Write a class travel that has attributes of kilometers and hours. A constructor with
no parameters initializes both data members to 0.
A member function get() inputs the values and function show() display the values.
It has a member function add() that takes an object of type Travel to add the
kilometres and hours of calling object and parameter and returns an object with the
added value. All member functions should be defined outside class and declared
inside the class
Q3. These methods of telling time are sometimes called civilian time and military
time. Our time12 class will represent civilian time, as used in digital clocks and airport
flight departure displays. We’ll assume that in this context there is no need for
seconds, so time12 uses only hours (from 1 to 12), minutes, and an “a.m.” or “p.m.”
designation. Our time24 class, which is for more exacting applications such as air
navigation, uses hours (from 00 to 23), minutes, and seconds.
Page 1 of 2
12-Hour Time 24-Hour Time
12:00 a.m. (midnight) 00:00
12:01 a.m. 00:01
1:00 a.m. 01:00
6:00 a.m. 06:00
11:59 a.m 11:59
12:00 p.m. (noon) 12:00
12:01 p.m. 12:01
6:00 p.m. 18:00
11:59 p.m. 23:59
Create a class time12 that has three attributes bool pm, int hrs and int mins. A default
constructor that assigns value pm to true, hrs to 0 and mins to 0. A parameterized
constructor that assigns values to data members. A member function display() show
the time in civilian format (1:00 a.m.). Write another class time24 that has three
attributes hours, minutes and seconds. A default constructor that assigns value 0 to
hours, minutes to 0 and seconds to 0. A parameterized constructor that assigns values
to data members. A member function display() show the time in military time format
(01:00). A conversion operator time12 converts time24 into time12.
Convert time24 object into time12 object using conversion operator time12.
Output
Page 2 of 2