Question

Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...

Programming Exercise 11-8

PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some applications might require the date to be printed in another form, such as March 24, 2019. Derive the class extDateType so that the date can be printed in either form.

Add a member variable to the class extDateType so that the month can also be stored in string form. Add a member function to output the month in the string format, followed by the year—for example, in the form March 2019.

Write the definitions of the functions to implement the operations for the class extDateType.

GIVEN FILES ARE: dateType.h, dateTypeImp.cpp, extDateType.h

FILE NEEDED: extDateTypeImp.cpp

~~~~~dateType.h~~~~~

#ifndef dateType_H

#define dateType_H

  

class dateType

{  

public:

    void setDate(int, int, int);

    void setMonth(int);

    void setDay(int);

    void setYear(int);

    void print() const;

    int numberOfDaysPassed();    

    int numberOfDaysLeft();

    void incrementDate(int nDays);

    int getMonth() const;

    int getDay() const;

    int getYear() const;

    int getDaysInMonth();

bool isLeapYear();

dateType(int = 1, int = 1, int = 1900);

private:

    int dMonth;

    int dDay;

    int dYear;

};

#endif

~~~~~~dateTypeImp.cpp~~~~~~

#include <iostream>

#include "dateType.h"

using namespace std;

void dateType::setDate(int month, int day, int year)

{

    if (year >= 1)

        dYear = year;

    else

        dYear = 1900;

    if (1 <= month && month <= 12)

        dMonth = month;

    else

        dMonth = 1;

    switch (dMonth)

    {

    case 1:

    case 3:

    case 5:

    case 7:

    case 8:

    case 10:

    case 12:

        if (1 <= day && day <= 31)

            dDay = day;

        else

            dDay = 1;

            break;

    case 4:

    case 6:

    case 9:

    case 11:

        if (1 <= day && day <= 30)

            dDay = day;

        else

            dDay = 1;

            break;

    case 2:

        if (isLeapYear())

        {

            if (1 <= day && day <= 29)

                dDay = day;

            else

                dDay = 1;

        }

        else

        {

            if (1 <= day && day <= 28)

                dDay = day;

            else

            dDay = 1;

        }

    }

}

void dateType::setMonth(int  m)

{

    dMonth = m;

}

void dateType::setDay(int d)

{

    dDay = d;

}

void dateType::setYear(int y)

{

    dYear = y;

}

void dateType::print() const

{

    cout << dMonth << "-" << dDay << "-" << dYear;

}

int dateType::getMonth()  const

{

    return dMonth;

}

int dateType::getDay()  const

{

    return dDay;

}

int dateType::getYear()  const

{

    return dYear;

}

int dateType::getDaysInMonth()

{

    int noOfDays;

    switch (dMonth)

    {

    case 1:

    case 3:

    case 5:

    case 7:

    case 8:

    case 10:

    case 12:

        noOfDays = 31;

        break;

    case 4:

    case 6:

    case 9:

    case 11:  

        noOfDays = 30;

        break;

    case 2:

        if (isLeapYear())

            noOfDays = 29;

        else

            noOfDays = 28;

    }

    return noOfDays;

}

bool dateType::isLeapYear()

{

    if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)

        return true;

    else

        return false;

}

dateType::dateType(int month, int day, int year)

{

    setDate(month, day, year);

}

int dateType::numberOfDaysPassed()

{

    int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,

                        31, 31, 30, 31, 30, 31};

    int sumDays = 0;

    int i;

    for (i = 1; i < dMonth; i++)

        sumDays = sumDays + monthArr[i];

    if (isLeapYear() && dMonth > 2)

        sumDays = sumDays + dDay + 1;

    else

        sumDays = sumDays + dDay;

    return sumDays;

}

int dateType::numberOfDaysLeft()

{

    if (isLeapYear())

        return 366 - numberOfDaysPassed();

    else

        return 365 - numberOfDaysPassed();

}

void dateType::incrementDate(int nDays)

{

    int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,

                       31, 31, 30, 31, 30, 31};

    int daysLeftInMonth;

    daysLeftInMonth = monthArr[dMonth] - dDay;

    if (daysLeftInMonth >= nDays)

        dDay = dDay + nDays;

    else

    {

        dDay = 1;

        dMonth++;

        nDays = nDays - (daysLeftInMonth + 1);

        while (nDays > 0)

            if (nDays >= monthArr[dMonth])

            {

                nDays = nDays - monthArr[dMonth];

                if ((dMonth == 2) && isLeapYear())

                    nDays--;

                dMonth++;

                if (dMonth > 12)

                {

                    dMonth = 1;

                    dYear++;

                }

            }

            else

            {

                dDay = dDay+nDays;

                nDays = 0;

            }

    }

}

            }

           else

            {

                dDay = dDay+nDays;

                nDays = 0;

            }

    }

}

~~~~~extDateType.h~~~~~

#ifndef extDateType_H

#define extDateType_H  

#include <string>

#include "dateType.h"

using namespace std;

string months[] = {"January", "February", "March", "April",

                   "May", "June", "July", "August",

                   "September", "October", "November", "December"};

class extDateType: public dateType

{

public:

    void printLongDate();

    void setDate(int, int, int);

    void setMonth(int m);

    void printLongMonthYear();

    extDateType();

    extDateType(int, int, int);

private:

    string month;

};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <cstdlib>

#include <iostream>

using namespace std;

class dateType

{

public:

dateType(int month = 1, int day = 1, int year = 1900);

void setDate(int month, int day, int year);

int getDay() const;

int getMonth() const;

int getYear() const;

void printDate() const;

int NumOfpassedDays(int , int );

int RemainingDays(int );

int setMonth(int );

void NewDate(int ,int , int , int );

bool isLeapYear(int);

protected:

int dMonth; //variable to store the month

int dDay; //variable to store the day

int dYear; //variable to store the year

int noDays;

int Passeddays;

};

class extDateType : public dateType{

string months[12]={"January","February","March","April","May","June","July","August","September","October","November","December"};

public:

extDateType(int m = 1, int d = 1, int y = 1900) : dateType(m, d, y) {

}

void printDate() const {

int m = dateType::getMonth();

int d = dateType::getDay();

int y = dateType::getYear();

cout << months[m-1] << " " << d << ", " << y << endl;

}

};

void dateType::setDate(int month, int day, int year)

{

while(month<1 || month>12)

{

cout << "Entered month "<<month<< " is wrong"<<endl;

cout << "Enter correct month"<<endl;

cin>>month;

}

dMonth = month;

// Checking date is valid

while(day<1 || day>31)

{

cout << "Entered day "<<day<<" is wrong"<<endl;

cout<<"Enter correct day"<<endl;

cin>>day;

}

dDay = day;

int count_digits = 0;

int flag=0;

int year1;

while(flag==0)

{

year1=year;

count_digits=0;

while (year) {

year /= 10;

count_digits++;

}

if(count_digits != 4)

{

cout << "Entered year "<<year1<<" is wrong"<<endl;

cout<<"Enter correct year"<<endl;

cin>>year;

flag=0;

}

else

flag=1;

}

dYear = year1;

}

bool dateType::isLeapYear(int year)

{

if((year%4==0)&&(year%100==0)||(year %400 ==0))

{

// cout<<"leap year"<<endl;

return true;

}

else

{

// cout<<"not leap year"<<endl;

return false;

}

}

int dateType::getDay() const

{

return dDay;

}

int dateType::getMonth() const

{

return dMonth;

}

int dateType::getYear() const

{

return dYear;

}

void dateType::printDate() const

{

cout << dMonth << "-" << dDay << "-" << dYear<<endl;

}

//Constructor with parameters

dateType::dateType(int month, int day, int year)

{

setDate(month, day, year);

isLeapYear(year);

cout<<dDay <<" "<<dMonth<<" "<<dYear<<endl;

}

void dateType::NewDate(int month,int day, int year, int adddays)

{

int daysPerMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

for (int i=0;i<adddays;i++)

{

day++;

if (day > daysPerMonth[month-1] || ( month==2 && day==29 && !isLeapYear(year) ) )

{

day = 1;

month++;

if (month==13)

{

month = 1;

year++;

}

}

}

dDay = day;

dMonth = month;

dYear = year;

cout<<dMonth<<" "<<dDay<<" "<<dYear;

}

int dateType::setMonth(int month)

{

int year1;

year1=dYear;

if(month<=12)

{

dMonth=month;

switch(month)

{

//include the case 1 also

case 1:

case 3:

case 5:

case 7:

case 8:

case 10:

case 12: noDays=31;

break;

case 4:

case 6:

case 9:

case 11:noDays=30;

break;

case 2:if(isLeapYear(year1))

noDays=29;

else

noDays=28;

}

}

else

{

cout<<"Invalid Month"<<endl;

dMonth=0;

}

return noDays;

}

int dateType::NumOfpassedDays(int month, int day)

{

int i,passedDays=0;

for(i=1; i<month;i++)

{

if(i==month)

{

noDays=noDays+1;

}

else

passedDays+=setMonth(i);

}

passedDays+=day;

Passeddays=passedDays;

return passedDays;

}

int dateType::RemainingDays(int year)

{

int remainingDays;

NumOfpassedDays(dMonth, dDay);

if(isLeapYear(year))

{

remainingDays=366-Passeddays;

}

else

remainingDays=365-Passeddays;

return remainingDays;

}

int main()

{

int dd,mm,yy,nd;

cout<<"Enter month: ";

cin>>mm;

cout<<"Enter day: ";

cin>>dd;

cout<<"Enter year: ";

cin>>yy;

extDateType d1(mm,dd,yy);

cout<<"Date is ";

d1.printDate();

return 0;

}

Output


answered by: ANURANJAN SARSAM
Add a comment
Answer #2

Please find the following answer in cpp which has derived class from base class "dateTyepe".

Note: I have added inline comments and screen shots for better understanding.

Program:

exDateTypeImp.cpp

#include <iostream>
#include "extDateType.h"

using namespace std;
//define the months array here, moved from header to cpp file.

//added extern for string months in extDateType.h file, to avoid compilation failure
string months[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};

//print the longdate in the format, Month, Day, Year (March, 3, 2020)
void extDateType::printLongDate()
{
cout <<month <<", "<<getDay()<<", "<<getYear()<<endl;
}

//set the date by calling base class's setDate method
void extDateType::setDate(int month, int day, int year)
{
dateType::setDate(month, day, year);
}
//set the extDateType class member variable month, which is the
//representation in string

void extDateType::setMonth(int mon)
{
month = months[mon-1];
}

//March, 2017, to diaplay in month, year format
void extDateType::printLongMonthYear()
{
cout <<month <<", "<<getYear()<<endl;
}
//default constructor for extDateType class
extDateType::extDateType()
{
setDate(1, 1, 1990);
month=months[0];
}
//constructor will monthm day year as arguments
extDateType::extDateType(int mon, int day, int year)
{
setDate(mon, day, year);
month=months[mon-1];
}

extDateType.h

#ifndef extDateType_H
#define extDateType_H

#include <string>
#include "dateType.h"

using namespace std;

//extern the months string array and define it in extDateImp.cpp file
extern string months[];

class extDateType: public dateType
{
public:
void printLongDate();
void setDate(int, int, int);
void setMonth(int m);
void printLongMonthYear();
extDateType();
extDateType(int, int, int);

private:
string month;
};
#endif

dateTypeImp.cpp

#include <iostream>
#include "dateType.h"
using namespace std;
void dateType::setDate(int month, int day, int year)
{
if (year >= 1)
dYear = year;
else
dYear = 1900;
if (1 <= month && month <= 12)
dMonth = month;
else
dMonth = 1;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (1 <= day && day <= 31)
dDay = day;
else
dDay = 1;
break;
case 4:
case 6:
case 9:
case 11:
if (1 <= day && day <= 30)
dDay = day;
else
dDay = 1;
break;
case 2:
if (isLeapYear())
{
if (1 <= day && day <= 29)
dDay = day;
else
dDay = 1;
}
else
{
if (1 <= day && day <= 28)
dDay = day;
else
dDay = 1;
}
}
}
void dateType::setMonth(int m)
{
dMonth = m;
}
void dateType::setDay(int d)
{
dDay = d;
}
void dateType::setYear(int y)
{
dYear = y;
}
void dateType::print() const
{
cout << dMonth << "-" << dDay << "-" << dYear;
}
int dateType::getMonth() const
{
return dMonth;
}
int dateType::getDay() const
{
return dDay;
}
int dateType::getYear() const
{
return dYear;
}
int dateType::getDaysInMonth()
{
int noOfDays;
switch (dMonth)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
noOfDays = 31;
break;
case 4:
case 6:
case 9:
case 11:
noOfDays = 30;
break;
case 2:
if (isLeapYear())
noOfDays = 29;
else
noOfDays = 28;
}
return noOfDays;
}
bool dateType::isLeapYear()
{
if (((dYear % 4 == 0) && (dYear % 100 != 0)) || dYear % 400 == 0)
return true;
else
return false;
}
dateType::dateType(int month, int day, int year)
{
setDate(month, day, year);
}
int dateType::numberOfDaysPassed()
{
int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int sumDays = 0;
int i;
for (i = 1; i < dMonth; i++)
sumDays = sumDays + monthArr[i];
if (isLeapYear() && dMonth > 2)
sumDays = sumDays + dDay + 1;
else
sumDays = sumDays + dDay;
return sumDays;
}
int dateType::numberOfDaysLeft()
{
if (isLeapYear())
return 366 - numberOfDaysPassed();
else
return 365 - numberOfDaysPassed();
}
void dateType::incrementDate(int nDays)
{
int monthArr[13] = {0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
int daysLeftInMonth;
daysLeftInMonth = monthArr[dMonth] - dDay;
if (daysLeftInMonth >= nDays)
dDay = dDay + nDays;
else
{
dDay = 1;
dMonth++;
nDays = nDays - (daysLeftInMonth + 1);
while (nDays > 0)
if (nDays >= monthArr[dMonth])
{
nDays = nDays - monthArr[dMonth];
if ((dMonth == 2) && isLeapYear())
nDays--;
dMonth++;
if (dMonth > 12)
{
dMonth = 1;
dYear++;
}
}
else
{
dDay = dDay+nDays;
nDays = 0;
}
}
}

dateType.h

#ifndef dateType_H
#define dateType_H


class dateType
{
public:
void setDate(int, int, int);
void setMonth(int);
void setDay(int);
void setYear(int);
void print() const;
int numberOfDaysPassed();
int numberOfDaysLeft();
void incrementDate(int nDays);
int getMonth() const;
int getDay() const;
int getYear() const;
int getDaysInMonth();
bool isLeapYear();
dateType(int = 1, int = 1, int = 1900);
private:
int dMonth;
int dDay;
int dYear;
};
#endif

main.cpp

#include <iostream>
#include "dateType.h"
#include "extDateType.h"

using namespace std;

int main()
{
//with default constructor
extDateType ext;
ext.printLongDate();
ext.setMonth(5);
ext.printLongMonthYear();
  
//without default constructor
extDateType ext1(11,6,1987);
ext1.printLongDate();
ext1.printLongMonthYear();
return 0;
}

sample Output:

January, 1, 1990
May, 1990
November, 6, 1987
November, 1987


Screen shot for extDateType.h and extDateTypeImp.cpp

extDateTypeImp.cpp

{ #include <iostream> #include extDateType.h using namespace std; 1/define the months array here string months[] = {Januar

extDateType.h

10 1 #ifndef extDateType_H 2 #define extDateType_H 3 4 #include <string> 5 #include dateType.h 6 7 using namespace std; 8 9

Add a comment
Know the answer?
Add Answer to:
Programming Exercise 11-8 PROBLEM:The class dateType defined in Programming Exercise 6 prints the date in numerical...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, I have a bug in my code, and when I run it should ask the...

    Hello, I have a bug in my code, and when I run it should ask the user to enter the month and then the year and then print out the calendar for the chosen month and year. My problem with my code is that when I run it and I enter the month, it doesn't ask me for the year and it prints all the years of the month I chose. Please help! Code: #include "calendarType.h" #include <iostream> using namespace...

  • #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;...

    #include "stdafx.h" #include <iostream> using namespace std; class dateType {    private:        int dmonth;        int dday;        int dyear;       public:       void setdate (int month, int day, int year);    int getday()const;    int getmonth()const;    int getyear()const;    int printdate()const;    bool isleapyear(int year);    dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) {    int numofdays;    if (year<=2008)    {    dyear=year;...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • Can someone please help me. i keep getting an error in my code. I need it...

    Can someone please help me. i keep getting an error in my code. I need it to be on three seperate files! attached is my assignment and my code. c++ The class date Type implements the date in a program. Write the class functions as specified in the UML class diagram. The values for the month, day, and year should be validated (in the class functions) before storing the date into the data members. Set the invalid member data to...

  • C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep...

    C++ problem 11-6 In Programming Exercise 2, the class dateType was designed and implemented to keep track of a date, but it has very limited operations. Redefine the class dateType so that it can perform the following operations on a date, in addition to the operations already defined: Set the month. Set the day. Set the year. Return the month. Return the day. Return the year. Test whether the year is a leap year. Return the number of days in...

  • public class Date { private int month; private int day; private int year; /** default constructor...

    public class Date { private int month; private int day; private int year; /** default constructor * sets month to 1, day to 1 and year to 2000 */ public Date( ) { setDate( 1, 1, 2000 ); } /** overloaded constructor * @param mm initial value for month * @param dd initial value for day * @param yyyy initial value for year * * passes parameters to setDate method */ public Date( int mm, int dd, int yyyy )...

  • n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A...

    n this programming assignment, you need to create 3 files. 1. DateType.h 2. DateType.cpp 3. A test driver for testing the class defined in the other 2 files. You can name your file in the way you like. Remember it must be a .cpp file. In DateType.h file, type these lines: // To declare a class for the Date ADT // This is the header file DateType.h class DateType { public: void Initialize(int newMonth, int newDay, int newYear); int GetYear()...

  • This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a fun...

    This is for my c++ class and im stuck. Complete the Multiplex.cpp file with definitions for a function and three overloaded operators. You don't need to change anything in the Multiplex.h file or the main.cpp, though if you want to change the names of the movies or concession stands set up in main, that's fine. Project Description: A Multiplex is a complex with multiple movie theater screens and a variety of concession stands. It includes two vectors: screenings holds pointers...

  • Need comments added please. public class TenDate {    private int NMnth;    private int NDy;...

    Need comments added please. public class TenDate {    private int NMnth;    private int NDy;    private int NYr;    private String [] MnthNm =        {"", "January", "February", "March", "April", "May", "June", "July",            "August", "September", "October", "November", "December"};    private int [] MnthD =        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};    public TenDate()    {        NMnth = 1;        NDy = 1;...

  • 1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify...

    1.) Add a data member, currentTime into the DataType.h 2.) Add a member function, GetCurrentTime(). Modify the initialize function. ESIGN PAGE LAYOUT REFERENCES an-E4_]Ka"!Aa.le' | =.=-'E- 恒栏1순↓ | T Font Paragraph // To declare a class for the Date ADT // This is the header file DateType.h class DateType public: void Initialize (int newMonth, int newDay, int newYear) int GetYear) const; int GetMonth() const int GetDay) const private: int year; int month; int day; search

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT