0% found this document useful (0 votes)
10 views

Lab 4 Classes

Uploaded by

Tamara Enad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 4 Classes

Uploaded by

Tamara Enad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Object Oriented Programming I with C++

Lab#4 Classes

Date 3/23/2021 (5PM-8PM)

Question:

Create a class named Time that contains integer fields for hours and minutes. Store the hours in military
time, that is, 0 through 23.

Add a function “DisplayTime” that displays the fields, using a colon to separate hours and minutes.
(Make sure the minutes display as two digits. For example, 3 o’clock should display as 3:00, not 3:0.)

Add another function “UpdateTime” that takes an argument that represents minutes to add to the
time. The function updates the time based on the number of minutes added. For example, 12:30 plus 15
is 12:45, 14:50 plus 20 is 15:10, and 23:59 plus 2 is 0:01.

The Time constructor requires an argument for hours. The argument for minutes is optional; the value
defaults to 0 if no argument is supplied. The constructor ensures that the hours field is not greater than
23 and that the minutes field is not greater than 59; default to these maximum values if the arguments
to the constructor are out of range.

Create a main() function that instantiates an array of at least three Time objects and demonstrates that
they display correctly both before and after varying amounts of time have been added to them. Save the
file as Time.cpp. You will use the class in other exercises
#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time (int=0, int=0);
void DisplayTime();
void UpdateTime (int);
};

Time::Time(int h, int m)
{
if (h<24)
hours=h;
else
hours =23;

if (m<60)
minutes=m;
else
minutes=59;
}

void Time::DisplayTime( )
{
cout<<"The Time is ";
if (hours<10)
cout<<"0"<<hours;
else
cout<<hours;
if (minutes<10)
cout<<":0"<<minutes;
else
cout<<":"<<minutes;
}

void Time::UpdateTime(int m)
{
int NumofHours;
minutes +=m;
if (minutes>59)
{
NumofHours =minutes/60;
minutes=minutes%60;
hours+= NumofHours;
}
if(hours>23)
hours%=24;
}

int main()
{
int AddedMinutes;
Time t1(23,5);
cout<<"The Time before updating: \n";
t1.DisplayTime();
cout<<"\nEnter the Minutes to add to the time\n";
cin>>AddedMinutes;
t1.UpdateTime(AddedMinutes);
cout<<"\nThe Time after updating: \n";
t1.DisplayTime();
return 0;
}

Array of Objects

#include<iostream>
class Time
{
private:
int hours;
int minutes;
public:
Time (int=0, int=0);
void DisplayTime();
void UpdateTime (int);
void setTime (int, int);
};

Time::Time(int h, int m)
{
if (h<24)
hours=h;
else
hours =23;

if (m<60)
minutes=m;
else
minutes=59;
}

void Time::DisplayTime( )
{
cout<<"The Time is ";
if (hours<10)
cout<<"0"<<hours;
else
cout<<hours;
if (minutes<10)
cout<<":0"<<minutes;
else
cout<<":"<<minutes;

void Time::UpdateTime(int m)
{

minutes +=m;
if (minutes>59)
{
hours+=minutes/60;
minutes=minutes%60;
}
if(hours>23)
hours%=24;
}

void Time::setTime(int hr, int mi)


{
hours=hr;
minutes=mi;
}

int main()
{
int h, m;
int AddedMinutes;
Time t1[2];
for (int i=0; i<2; i++)
{
cout<<"\nEnter Hours\n";
cin>>h;
cout<<"Enter Minutes\n";
cin>>m;
t1[i].setTime(h,m);
cout<<"The Time before updating: \n";
t1[i].DisplayTime();
cout<<"\nEnter the Minutes to add to the time\n";
cin>>AddedMinutes;
t1[i].UpdateTime(AddedMinutes);
cout<<"\nThe Time after updating: \n";
t1[i].DisplayTime();
}
return 0;
}

You might also like