Question

8. The class dateType defined in Programming Exercise 6 prints the date in numerical form. Some...

8. 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, 2017. 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 2017. Write the definitions of the functions to implement the operations for the class extDateType

[Complete Exercise 8 on page 808. You will create a class called extDateType. Make sure to test your class and include screen shots of the class test,

c++, D.S Malik, 7th edition]

Homework Answers

Answer #1

Code:

//extDateType.h:

#ifndef EXTDATETYPE_H

#define EXTDATETYPE_H

#include <string>

#include "dateType.h"

using namespace std;

class extDateType : public dateType

{

       string month;

public:

       extDateType(int m, int day, int year);

       int setMonth(int m);

       void printDateIn2Format();

       void print_format();

};

#endif

//extDateType.cpp:

#include "extDateType.h"

using namespace std;

extDateType::extDateType(int m = 0, int day = 0, int year = 0): dateType(m, day, year) {

    month = "invalid";

    switch (m) {

    case 1:

        month = "January";

        break;

    case 2:

        month = "February";

        break;

    case 3:

        month = "March";

        break;

    case 4:

        month = "April";

        break;

    case 5:

        month = "May";

        break;

    case 6:

        month = "June";

        break;

    case 7:

        month = "July";

        break;

    case 8:

        month = "August";

        break;

    case 9:

        month = "September";

        break;

    case 10:

        month = "October";

        break;

    case 11:

        month = "November";

        break;

    case 12:

        month = "December";

        break;

    }

}

int extDateType::setMonth(int m) {

    month = "invalid";

    switch (m) {

    case 1:

        month = "January";

        break;

    case 2:

        month = "February";

        break;

    case 3:

        month = "March";

        break;

    case 4:

        month = "April";

        break;

    case 5:

        month = "May";

        break;

    case 6:

        month = "June";

        break;

    case 7:

        month = "July";

        break;

    case 8:

        month = "August";

        break;

    case 9:

        month = "September";

        break;

    case 10:

        month = "October";

        break;

    case 11:

        month = "November";

        break;

    case 12:

        month = "December";

        break;

    }

    return dateType::setMonth(m);

}

void extDateType::printDateIn2Format() {

    cout << "DATE: Month Day, Year: " <<

        month << " " << getDay() << ", " << getYear() << endl;

}

void extDateType::print_format() {

    cout << "Choose the format to print the date:\n" <<

        "1.DD/MM/YYYY\n" <<

        "2.Month Day, Year\n";

    int choice;

    cin >> choice;

    while (choice > 2 || choice < 1) {

        cout << "Please choose a valid format:\n" <<

            "1.DD/MM/YYYY\n" <<

            "2.Month Day, Year\n";

        cin >> choice;

    }

    if (choice == 1) {

        printDate();

    } else {

        printDateIn2Format();

    }

}

//main.cpp:

#include<iostream>

#include "extDateType.h"

using namespace std;

int main() {

    int m, d, y;

    char choice = 'y';

    do {

        cout << "\nEnter the month as a number: ";

        cin >> m;

        cout << "Enter the day: ";

        cin >> d;

        cout << "Enter the year: ";

        cin >> y;

        extDateType date(m, d, y);

        date.print_format();

        cout << "Do you wish to continue with another date(y/n)?: ";

        cin >> choice;

    } while (choice == 'y');

    return 0;

}

//dateType.h:

#ifndef DATATYPE_H

#define DATATYPE_H

#include<iostream>

using namespace std;

class dateType {

    private:

        int dMonth;

    int dDay;

    int dYear;

    int noDays;

    int Passeddays;

    public:

        int setMonth(int month);

    void setDay(int day);

    void setYear(int year);

    int getDay() const;

    int getMonth() const;

    int getYear() const;

    void printDate() const;

    bool isLeapYear(int year);

    int totalNumOfpassedDays(int, int);

    int RemainingDays(int);

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

    dateType(int month = 0, int day = 0, int year = 0);

};

#endif

//dateType.cpp

#include "dateType.h"

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

    dMonth = month;

    dDay = day;

    dYear = year;

}

int dateType::totalNumOfpassedDays(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;

}

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

    if ((day + adddays) % month) {

        dDay = day + adddays;

        dDay = abs(dDay - 31);

        month++;

        dMonth = month;

        //set year to dYear

        dYear = year;

        if (month > 12) {

            dMonth = 1;

            dYear++;

        }

    }

}

int dateType::RemainingDays(int year) {

    int remainingDays;

    if (isLeapYear(year)) {

        remainingDays = 366 - Passeddays;

    } else

        remainingDays = 365 - Passeddays;

    return remainingDays;

}

int dateType::setMonth(int month) {

    int year1;

    year1 = dYear;

    if (month <= 12) {

        dMonth = month;

        switch (month) {

        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;

}

void dateType::setDay(int day) {

    if (day <= noDays) {

        dDay = day;

    } else {

        cout << "Invalid Day" << endl;

        dDay = 0;

    }

}

void dateType::setYear(int year) {

    dYear = year;

}

bool dateType::isLeapYear(int year) {

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

        return true;

    else

        return false;

}

void dateType::printDate() const {

    cout << "DATE: DD/MM/YYYY : " << dDay << "/" << dMonth << "/" << dYear << endl;

}

int dateType::getDay() const {

    return dDay;

}

int dateType::getMonth() const {

    return dMonth;

}

int dateType::getYear() const {

    return dYear;

}

Output:

Know the answer?
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
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT