In: Computer Science
(Problem is from the textbook Data Structures using C++)
Add a function to the dateType class.
The function's name is compareDates.
Its prototype is
int compareDates(const dateType& otherDate;
The function returns -1 if otherDate is greater than than this date
The function returns 0 if otherDate equals this date
The function returns 1 if otherDate is less than this date
Examples
dateType d1(1, 1, 2019);
dateType d2(11,1, 2019)
d1.compareDates(d2) returns -1
d2.compareDates(d1) returns 1
d2.compareDates(d2) returns 0
<<<<< dateType.h >>>>>>
#ifndef H_addressType
#define H_addressType
#include
#include
using namespace std;
class addressType
{
public:
void setStreet(string myStreet);
void setCity(string mCity);
void setState(string myState);
void setZIP(int myZIP);
string getStreet() const;
string getCity() const;
string getState() const;
int getZIP() const;
void printAddress() const;
addressType(string = "", string = "", string = "",
int = 0);
private:
string street;
string city;
string state;
int ZIP;
}
#endif
void addressType::setStreet(string myStreet)
{
street = myStreet;
} // end function setStreet
void addressType::setCity(string myCity)
{
city = myCity;
} // end function setCity
void addressType::setState(string myState)
{
state = myState;
} // end functioni setState
void addressType::setZIP(int myZIP)
{
ZIP = myZIP;
} // end function setZIP
string addressType::getStreet() const
{
return street;
} // end function getStreet
string addressType::getCity() const
{
return city;
} // end function getCity
string addressType::getState() const
{
return state;
} // end function getState
int addressType::getZIP() const
{
return ZIP;
} // end function getZIP
void addressType::printAddress() const
{
cout << "\n\t" << getStreet() << ", " << getCity()
<< "\n\t" << getState() << " - " << getZIP();
} // end function printAddress
addressType::addressType(string myStreet, string myCity,
string myState, int myZIP)
{
setStreet(myStreet);
setCity(myCity);
setState(myState);
setZIP(myZIP);
} // end constructor addressType
hi i have answered your question hope this will be helpful to you as i need to run the program and verify hence i have make a seprate class and function there so you too can verify the function if it working as expected ,hope you will understand , kindly give a thumbs up , and if need any clarification you can comment i will reply accordingly.
code:-
#include<iostream>
using namespace std;
class dateType
{
int day;
int month;
int year;
public:
int compareDates(const dateType& otherDate); //Function
Prototype to compare date
dateType(int d,int m,int y) { // constructor to set the values of
the variable so we ever a object will be creat this will set the
value
this->day= d;
this->month= m;
this->year=y;
}
};
int dateType :: compareDates(const dateType& obj) { // Function
to compare the dates comapring each day month and year
if(this->day== obj.day && this->month == obj.month
&& this->year == obj.year)
return 0;
else if(this->year <= obj.year && this->month
<= obj.month && this->day < obj.day)
return -1;
else
return 1;
}
int main()
{
dateType d1(1,1,2019),d2(11,1,2019); //Creating objectes of the
datetypes class
int res = d1.compareDates(d2);
cout<<"res is "<<res<<endl;
res = d2.compareDates(d1);
cout<<"res is "<<res<<endl;
res = d2.compareDates(d2);
cout<<"res is "<<res<<endl;
return 0;
}
output:-
\\\
happy to help :).