//Implementation of derived class named FancyDateClass by inheriting the DateCla
ID: 3620419 • Letter: #
Question
//Implementation of derived class named FancyDateClass by inheriting the DateClass.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
class DateClass
{
private:
int day;
int month;
int year;
static int objectCount;
public:
int setDate(int theDay, int theMonth, int theYear);
void getDate(int &theDay, int &theMonth, int &theYear);
void displayDate();
DateClass();
DateClass(int theDay, int theMonth, int theYear);
DateClass(const DateClass &dateObject);
~DateClass();
class FancyDateClass
{
public:
string toString();
string getMonth();
int getDayOfWeek();
int julianDate();
bool isLeapYear;
int subtract(FancyDateClass &aDateObj);
FancyDateClass();
FancyDateClass(int theDay, int theMonth, int theYear);
FancyDateClass(const FancyDateClass &aDateObj);
~FancyDateClass();
};
};
int main()
{
cout << " ";
cout << "The date ";
DateClass();
cout << " ";
cout << "The month for ";
DateClass();
cout << " is ";
FancyDateClass (aDateObj.getMonth());
cout << " ";
cout << "The day of the week for ";
DateClass.displayDate();
cout << " is ";
cout << DateClass.getDayOfWeek();
cout << " ";
cout << "This date ";
Date.displayDate();
Date.isLeapYear();
cout << " ";
cout << aDateObj.subtract(FancyDateClass);
cout << " Closing down the program ";
return 0;
}
string FancyDateClass::toString()
{
std::stringstream ss;
int month = 10;
string str;
ss << month;
ss >> str;
return str;
}
int FancyDateClass::getDayOfWeek()
{
int day = julianDate() % 7;
std::stringstream ss;
string str;
switch(day){
case 0: ss << "Monday";
ss >> str;
break;
case 1: ss << "Tuesday";
ss >> str;
break;
case 2: ss << "Wednesday";
ss >> str;
break;
case 3: ss << "Thursday";
ss >> str;
break;
case 4: ss << "Friday";
ss >> str;
break;
case 5: ss << "Saturday";
ss >> str;
break;
case 6: ss << "Sunday";
ss >> str;
break;
}
return str;
string FancyDateClass::getMonth()
{
std::stringstream ss;
string str;
switch(month)
{
case 1: ss << "Jan";
ss >> str;
break;
case 2: ss << "Feb";
ss >> str;
break;
case 3: ss << "Mar";
ss >> str;
break;
case 4: ss << "Apr";
ss >> str;
break;
case 5: ss << "May";
ss >> str;
break;
case 6: ss << "Jun";
ss >> str;
break;
case 7: ss << "Jul";
ss >> str;
break;
case 8: ss << "Aug";
ss >> str;
break;
case 9: ss << "Sep";
ss >> str;
break;
case 10: ss << "Oct";
ss >> str;
break;
case 11: ss << "Nov";
ss >> str;
break;
case 12: ss << "Dec";
ss >> str;
break;
};
return str;
}
int FancyDateClass::julianDate()
{
int theJulianDate = ( 1461 * ( year + 4800 + ( month - 14 ) / 12 ) ) / 4 + ( 367 *
( month - 2 - 12 * ( ( month - 14 ) / 12 ) ) ) / 12
- ( 3 * ( ( year + 4900 + ( month - 14 ) / 12 ) / 100 ) ) / 4 + day - 32075;
return theJulianDate;
}
bool FancyDateClass::isLeapYear()
{
bool leapYear = (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
if (leapYear == true)
cout << "Leap year";
else
cout << "Not a Leap year";
return leapYear;
}
FancyDateClass::FancyDateClass(int theDay, int theMonth, int theYear){
setDate(theDay, theMonth, theYear);
}
int FancyDateClass::subtract(FancyDateClass &2ndDate){
2ndDate.setDate(25, 2, 2008);
return (this->julianDate() - other.julianDate());
}
DateClass::DateClass(){
day = 0;
month = 0;
year = 0;
objectCount++;
}
DateClass::DateClass(int theDay, int theMonth, int theYear){
setDate(theDay, theMonth, theYear);
objectCount++;
}
DateClass::DateClass(const DateClass &dateObject) {
day = dateObject.day;
month = dateObject.month;
year = dateObject.year;
objectCount++;
}
DateClass::~DateClass() {
objectCount--;
cout << "Number of DateClass objects instantiated " << objectCount << endl;
}
int DateClass::setDate(int theDay, int theMonth, int theYear){
if(theYear < 1)
dateCode = 3;
if(theMonth < 1 || theMonth > 12)
dateCode = 2;
int maxDays = 31;
switch (theMonth) {
case 2:
maxDays = 28;
break;
case 4:
case 6:
case 9:
case 11:
maxDays = 30;
break;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
maxDays = 31;
break;
}
if (theDay < 1 || theDay > maxDays)
dateCode = 1;
month = theMonth;
day = theDay;
year = theYear;
return dateCode;
}
void DateClass::getDate(int &theDay, int &theMonth, int &theYear){
theDay = day;
theMonth = month;
theYear = year;
}
void DateClass::displayDate() {
cout << month << "/" << day << "/" << year;
}
Explanation / Answer
I'm not sure exactly what your question is, but you need to modify the FancyDateClass so that it actually inherits the DateClass. To do this, you have to write something like class FancyDateClass : public DateClass { . This makes a new class called FancyDateClass which inherits all of DateClass's methods and variables, and makes everything that was public in DateClass also public in FancyDateClass.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.