Docsity

OOPs - Assignment - Create a class called time, Assignments of Object Oriented Programming

Toggle sidebar
bg1
Q1. (Marks 5)
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 constructor should initialize it to fixed values.
Make void print() to display time in 23:59:59 format.
Make void setTime(int,int,int) to set hour, minute, second.
include a tick() member function that increments the time stored in a Time object by one second.
Make an add member function that should add two objects of type time passed as arguments.
Be sure to test the following cases:
Incrementing into the next minute.
Incrementing into the next hour.
Incrementing into the next day (i.e., 23:59:59 to 00:00:00).
Make 1000 times loop in a main function. Call tick and print functions in that loop for an object. Also make two
objects and add them to a third object and print their values.
Answer
#include <iostream>
#include <iomanip>
using namespace std;
class Time
{
public:
Time(int = 0, int = 0, int = 0);
void setTime(int, int, int);
void setHour(int);
void setMinute(int);
void setSecond(int);
int getHour();
int getMinute();
int getSecond();
void print();
void Tick();
private:
int hour;
int minute;
int second;
};
Time::Time(int hour, int minute, int second)
{
setTime(hour, minute, second);
}
void Time::setTime(int h, int m, int s)
{
setHour(h);
setMinute(m);
setSecond(s);
}
void Time::setHour(int h)
{
hour = (h >= 0 && h < 24) ? h : 0;
}
void Time::setMinute(int m)
{
minute = (m >= 0 && m < 60) ? m : 0;
}
Object Oriented Programming Page 1 of 2
pf3

Partial preview of the text

Download OOPs - Assignment - Create a class called time and more Assignments Object Oriented Programming in PDF only on Docsity!

Q1. (Marks 5) 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 constructor should initialize it to fixed values. Make void print() to display time in 23:59:59 format. Make void setTime(int,int,int) to set hour, minute, second. include a tick() member function that increments the time stored in a Time object by one second. Make an add member function that should add two objects of type time passed as arguments. Be sure to test the following cases: Incrementing into the next minute. Incrementing into the next hour. Incrementing into the next day (i.e., 23:59:59 to 00:00:00). Make 1000 times loop in a main function. Call tick and print functions in that loop for an object. Also make two objects and add them to a third object and print their values. Answer #include #include using namespace std; class Time { public: Time(int = 0, int = 0, int = 0); void setTime(int, int, int); void setHour(int); void setMinute(int); void setSecond(int); int getHour(); int getMinute(); int getSecond(); void print(); void Tick(); private: int hour; int minute; int second; }; Time::Time(int hour, int minute, int second) { setTime(hour, minute, second); } void Time::setTime(int h, int m, int s) { setHour(h); setMinute(m); setSecond(s); } void Time::setHour(int h) { hour = (h >= 0 && h < 24)? h : 0; } void Time::setMinute(int m) { minute = (m >= 0 && m < 60)? m : 0; }

void Time::setSecond(int s) { second = (s >= 0 && s < 60)? s : 0; } int Time::getHour() { return hour; } int Time::getMinute() { return minute; } int Time::getSecond() { return second; } void Time::print() { if(hour<10) cout<<"0"; cout<<hour<<":"; if(minute<10) cout<<"0"; cout<<minute<<":"; if(second<10) cout<<"0"; cout<<second<<endl; } void Time::Tick() { second++; if (second == 60) { second = 0; minute++; } if (minute >= 60) { minute = 0; hour++; } } int main () { Time t; t.setTime(11,59,59); t.print(); for(int i=0; i<=1000; i++) { t.Tick(); t.print(); }