**This program needs to be written in c++ programming language** Assignment Goal
ID: 3813692 • Letter: #
Question
**This program needs to be written in c++ programming language**
Assignment Goals: To learn to use class inheritance and/or composition to create new classes.
A. In Lab 4, you defined and implemented a class called Time. You will make a minor modification to the lab 4 program so that it meets the following specifications:
The class Time consists of three private member variables of type int: hour, minute, and second.
The class Time also includes the following public member functions:
1. print to print the hour, minute, and second in the format of HH-MM-SS. (Hint: consider using output manipulators setw and setfill).
2. setTime to accept three int arguments and use them to set the hour, minute and second member variables. A valid value for hour is between 0 and 23, inclusively. A valid value for minute and second is between 0 and 59. For any invalid values, use 0 instead.
3. getHour to return the value of hour.
4. getMinute to return the value of minute.
5. getSecond to return the value of minute.
6. An equalTime function that compares two Time objects. Return true if they are equal in hour, minute, and second, otherwise return false. equalTime has a formal parameter which is a Time object.
7. A constructor that accepts three int arguments and use them to initialize hour, minute and second. The three parameters use default value 0. Data validation should be considered. (Hints: Call the setTime member function with 3 arguments.)
8. A copy constructor that creates a Time object by initializing it with another Time object.
B. a class called Date that meets the following specifications:
The class Date consists of three private member variables: year of type int, month of type int, and day of type int.
The class Date also includes the following public member functions:
1. print to output the year, month, and day in the format of YYYY-MM-DD.
2. setDate to set the year, month and day according to the parameters.
Data validation should be provided. 1) A valid year should be between 1900 and 2017 inclusively, otherwise using 2001 for the year. 2) A valid month should be between 1 and 12 inclusively, otherwise using 1 for the month. 3) A valid day should be between 1 and 28 when the month is 2, i.e., simply assume there are 28 days in February. 1 and 31 when the month is 1, 3, 5, 7, 8, 10, or 12, which means there are 31 days in January, March, May, July, August, October, and December . 1 and 30 when the month is 4, 6, 9, or 11 for April, June, September, and November. Use 1 for any invalid value.
3. getYear to return the year.
4. getMonth to return the month.
5. getDay to return the day.
6. equalDate to compare two Date objects. Return true if they are the same, otherwise, return false.
7. A constructor to initialize year, month, and day with default parameters. The default values for the year, month, and day are 2001, 1, and 1, respectively.
8. A copy constructor to initialize year, month, and day with another Date object.
C. Define a new class called Event using class composition.
The class Event consists of four private member variables: string name string location Date date Time time
The class Event should also include the following public member functions:
1. print to print the even name, location, date, and time information.
2. setEvent to set the event name, location, date, and time using two string parameters, one Date parameter, and one Time parameter.
3. setEventName to set the event name according to the parameter.
4. getEventName to return the event name.
5. setLocation to set the event location according to the parameter.
6. getLocation to return the event location.
7. setDate to set the event date using a Date object.
8. getDate to return the date, a Date object.
9. setTime to set the event time using a Time object.
10. getTime to return the time, a Time object.
11. equalEventDate to compare two event’s dates. Return true if they are the same, otherwise, return false.
12. A default constructor to initialize name and location to “None” and invoke the default constructor of Date and Time.
13. An overloaded constructor that initializes name, location, date, and time according to the parameters (two string parameters, one Date parameter, and one Time parameter).
D.In the client program, you should write statements to test your class implementation including the following functions in the Event class: default constructor, overloaded constructor, mutator functions, accessor functions, and equalEventDate function. You can use the following sample data for the events, as well as other data at your preference.
Explanation / Answer
#ifndef TIME_H
#define TIME_H
class Time
{
private:
int hour, minute, second;
public:
Time(int h, int m, int s);
Time (const Time &time);
~Time();
void printTime();
bool setTime(int h, int m, int s);
int getHour();
int getMinute();
int getSecond();
bool equalTime(Time obj);
};
#endif // TIME_H
#include "Time.h"
#include<iostream>
using namespace std;
Time::Time(int h=0, int m=0, int s=0)
{
if( setTime(h, m, s)!=true)
cout<<"Error is creating object due to wrong values"<<endl;
}
Time::Time(const Time&obj)
{
this->hour = obj.hour;
this->minute = obj.minute;
this->second = obj.second;
}
Time::~Time()
{
}
bool Time::equalTime(Time obj)
{
if(this->hour==obj.hour && this->minute==obj.minute && this->second==obj.second)
return true;
return false;
}
bool Time::setTime(int h, int m, int s)
{
if(h>=0 && h<=24 && m>=0 && m<=59 && s>=0 && s<=59)
{
hour = h;
minute = m;
second = s;
return true;
}
return false;
}
void Time::printTime()
{
cout<<hour<<":"<<minute<<":"<<second<<endl;
}
int Time::getHour()
{
return hour;
}
int Time::getMinute()
{
return minute;
}
int Time::getSecond()
{
return second;
}
#ifndef DATE_H
#define DATE_H
class Date
{
private:
int year, month, day;
public:
Date(int y, int m, int d);
Date(const Date&obj);
~Date();
void printDate();
bool setDate(int y, int m , int d);
int getYear();
int getMonth();
int getDay();
bool equalDate(Date obj);
};
#endif // DATE_H
#include<iostream>
#include "Date.h"
using namespace std;
Date::Date(int y=2001, int m=1 , int d=1)
{
if( setDate(y, m, d)!=true)
cout<<"Error is creating object due to wrong values"<<endl;
}
Date::Date(const Date&obj){
this->year = obj.year;
this->month = obj.month;
this->day = obj.day;
}
Date::~Date()
{
}
void Date::printDate(){
cout<<year<<"-"<<month<<"-"<<day<<endl;
}
bool Date::setDate(int y, int m , int d){
if(y>=1900 && y<=2017 && m>=1 && m<=12)
{
if(m==2)
{
if(d>=1 && d<=28)
{
year = y;
month = m;
day = d;
return true;
}
return false;
}
else if(m==1 || m==3 || m==5 || m==7 || m==8 || m==10 || m==12 )
{
if(d>=1 && d<=31)
{
year = y;
month = m;
day = d;
return true;
}
return false;
}
else if(m==4 || m==6 || m==9 || m==11)
{
if(d>=1 && d<=30)
{
year = y;
month = m;
day = d;
return true;
}
return false;
}
}
return false;
}
int Date::getYear(){
return year;
}
int Date::getMonth(){
return month;
}
int Date::getDay(){
return day;
}
bool Date::equalDate(Date obj){
if(this->year==obj.year && this->month==obj.month&& this->day==obj.day)
return true;
return false;
}
#ifndef EVENT_H
#define EVENT_H
#include "Date.h"
#include "Time.h"
#include <string>
using namespace std;
class Event
{
private:
Date dobj;
Time tobj;
string location;
string name;
public:
Event();
Event(string ename, string eloc, Date &dparam, Time &tparam);
~Event();
void printEvent();
void setEvent(string ename, string eloc, Date dparam, Time tparam);
void setEventName(string name);
string getEventName();
void setLocation(string loc);
string getLocation();
void setDate();
int getDate();
void setTime(int h, int m, int s);
int getTime();
bool equalEventDate(Event obj);
};
#endif // EVENT_H
#include "Event.h"
#include<iostream>
using namespace std;
Event::Event()
{
string name = nullptr;
string location=nullptr;
}
Event::Event(string ename, string eloc, Date &dparam, Time &tparam)
{
name = ename;
location = eloc;
dobj = dparam;
tobj = tparam;
}
Event::~Event()
{
}
void Event::printEvent(){
cout<<name<<" "<<location<<" "<<dobj.printDate()<<" "<<tobj.printTime()<<endl;
}
void Event::setEvent(string ename, string eloc, Date dparam, Time tparam){
name = ename;
location = eloc;
dobj = dparam;
tobj = tparam;
}
void Event::setEventName(string ename){
name = ename;
}
string Event::getEventName(){
return name;
}
void Event::setLocation(string loc){
location = loc;
}
string Event::getLocation(){
return location;
}
void Event::setDate(int y, int m, int d){
dobj.setDate(y, m, d);
}
Date Event::getDate(){
return dobj;
}
void Event::setTime(int h, int m, int s){
tobj.setTime(h, m, s);
}
Time Event::getTime(){
return tobj;
}
bool Event::equalEventDate(Event obj){
if(this->location==obj.location && this->name==obj.name && dobj==obj.dobj && tobj==obj.tobj)
return true;
return false;
}
#include "ClientApp.h"
#include "Event.h"
#include "Date.h"
#include "Time.h"
#include <string>
using namespace std;
int main()
{
// Event(string ename, string eloc, Date dparam, Time tparam)
Date dparam(2010,10, 10);
Time tparam(11,50,25);
Event eve("Singing", "Gurgaon", dparam, tparam);
}
// please check for compilation
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.