Question

Write a C++ Program. You have a following class as a header file (dayType.h) and main()....

Write a C++ Program.

You have a following class as a header file (dayType.h) and main().

#ifndef H_dayType

#define H_dayType

#include <string>

using namespace std;

class dayType

{

public:

    static string weekDays[7];

    void print() const;

    string nextDay() const;

    string prevDay() const;

    void addDay(int nDays);

    void setDay(string d);

    string getDay() const;

    dayType();

    dayType(string d);

private:

    string weekDay;

};

#endif

/*

// Name: Your Name

// ID: Your ID

*/

#include <iostream>

#include <string>

#include "dayType.h"

using namespace std;

int main()

{

    dayType myDay("Monday");

    dayType temp("Sunday");

    myDay.print();

    cout << endl;

    cout << myDay.prevDay() << endl;

    cout << myDay.nextDay() << endl;

    temp.print();

    cout << endl;

    cout << temp.prevDay() << endl;

    cout << temp.nextDay() << endl;

    return 0;

}

The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType:

  1. Set the day
  2. Print the day.
  3. Return the day.
  4. Return the next day.
  5. Return the previous day.

Write the definitions of the functions to implement the operations for the class dayType as follows:

#include <iostream>

#include <string>

#include "dayType.h"  

using namespace std;

string dayType::weekDays[7] = {"Sunday", "Monday", "Tuesday",

                     "Wednesday", "Thursday", "Friday", "Saturday"};

void dayType::print() const

{

    cout << weekDay;

}

Your code here

….

dayType::dayType(string d)

{

    weekDay = d;

}

and then you MUST test various operations on this class using the main(). For example, if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.

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

Test run:

=====================================================================

//daytype.h

#ifndef H_dayType

#define H_dayType

#include <string>

using namespace std;

class dayType {

public:

    static string weekDays[7];

    void print() const;

    string nextDay() const;

    string prevDay() const;

    void addDay(int nDays);

    void setDay(string d);

    string getDay() const;

    dayType();

    dayType(string d);

private:

    string weekDay;

};

#endif

=========================================================================

//day-type.cpp

#include <iostream>
#include <string>
#include<string.h>
#include "dayType.h"

using namespace std;

int main() {

    cout << "Accepted Inputs: " << endl;
    for (int i = 0; i < 7; i++) cout << dayType::weekDays[i] << endl;
    cout << "Input (weekday names) are case sensitive" << endl;
    cout << "============================================================" << endl << endl;
    dayType d("Sunday");

    cout << "New day 'd' initialised by default to: ";
    d.print();
    cout << endl << endl;
    cout << "Enter day: ";
    string dIn;
    cin >> dIn;

    dayType dInput(dIn);

    cout << "Input day: ";
    dInput.print();
    cout << endl << endl;
    cout << "Next day: " << dInput.nextDay() << endl << endl;

    cout << "Previous day: " << dInput.prevDay() << endl << endl;

    cout << "Input number to add to the day: ";

    int n;

    cin >> n;

    cout << "Adding " << n << " days to " << dInput.getDay() << " = ";
    dInput.addDay(n);
    dInput.print();
    cout << endl << endl;
    cout << "Input day to set: ";
    cin >> dIn;
    dInput.setDay(dIn);
    cout << "Day set to: " << dInput.getDay();

    return 0;
}

void dayType::print() const {
    cout << weekDay;
}

dayType::dayType(string d) {
    weekDay = d;
}

string dayType::prevDay() const {

    for (int i = 0; i < 7; i++) {
        if (weekDay.compare(weekDays[i]) == 0) {
            return weekDays[i - 1];
        }
    }

    cout << endl << "Please check input";

    return "";
}

string dayType::nextDay() const {

    for (int i = 0; i < 7; i++) {
        if (weekDay.compare(weekDays[i]) == 0) {
            return weekDays[i + 1];
        }
    }

    cout << endl << "Please check input";

    return "";
}

void dayType::addDay(int nDays) {

    for (int i = 0; i < 7; i++) {
        if (weekDay.compare(weekDays[i]) == 0) {
            weekDay = weekDays[(i + nDays) % 7];
            break;
        }
    }
}

void dayType::setDay(string d) {
    weekDay = d;
}

string dayType::getDay() const {
    return weekDay;
}

string dayType::weekDays[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

==========================================================================================

Add a comment
Know the answer?
Add Answer to:
Write a C++ Program. You have a following class as a header file (dayType.h) and main()....
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...

  • My main() file does not call to the class created in a .hpp file. It comes...

    My main() file does not call to the class created in a .hpp file. It comes out with the error "undefined reference to "___" const. I want to solve this by only changing the main() .cpp file, not the .hpp file. .cpp file .hpp file Thank you! include <iostream> include <string> tinclude "CourseMember.hpp using namespace std int main0 CourseMember CourseMember(90, "Jeff", "Goldblum"): // initializing the constructor cout<< "Member ID is <<CourseMember.getID) << endl; 1/ calling getID) accessor cout <<"First Name...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • C++ Project Modify the Date Class: Standards Your program must start with comments giving your name...

    C++ Project Modify the Date Class: Standards Your program must start with comments giving your name and the name of the assignment. Your program must use good variable names. All input must have a good prompt so that the user knows what to enter. All output must clearly describe what is output. Using the date class, make the following modifications: Make the thanksgiving function you wrote for project 1 into a method of the Date class. It receive the current...

  • FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const...

    FILL IN THE BLANKS #include #include <> *BLANK* using namespace std; int main() {     const double HOURLY_RATE = 15;     string input_str;     cout << "Enter days of attendance: " << endl;    *BLANK* (cin, input_str);     stringstream input_stream();     int total_hours = 0;     while(!input_stream.())*BLANK*     {         int hours;         string day;         input_stream >> day;         if(day ==  *BLANK*|| day == *BLANK*)         {             hours = 5;         }         *BLANK*(day == "Tuesday" || day == "Thursday")         {             hours = 4;         }         else if(day ==...

  • Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass...

    Find Output. Just output. No explanation needed.. #include <iostream> #include <string> using namespace std; class baseClass { public: void print() const; baseClass(string s = " ", int a = 0); //Postcondition: str = s; x = a; protected: int x; private: string str; }; class derivedClass: public baseClass { public: void print() const; derivedClass(string s = "", int a = 0, int b = 0); //Postcondition: str = s; x = a; y = b; private: int y; }; int...

  • I have to type and explain in class each code in every detail filled with //...

    I have to type and explain in class each code in every detail filled with // commentary. Explains how does work in every codes. 1) What does the below print #include <iostream> using namespace std ; int main() {    int var1 = 20 ;    int var2 = 30 ;    int* ptr1 ;    int* ptr2 ;    int* temp ;    ptr1 = &var1 ;    ptr2 = &var2 ;    cout << *ptr1 << endl ;...

  • Requirements I have already build a hpp file for the class architecture, and your job is to imple...

    Requirements I have already build a hpp file for the class architecture, and your job is to implement the specific functions. In order to prevent name conflicts, I have already declared a namespace for payment system. There will be some more details: // username should be a combination of letters and numbers and the length should be in [6,20] // correct: ["Alice1995", "Heart2you", "love2you", "5201314"] // incorrect: ["222@_@222", "12306", "abc12?"] std::string username; // password should be a combination of letters...

  • -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the...

    -------c++-------- The Passport class takes a social security number and the location of a photo file. The Passport class has a Photo class member, which in a full-blown implementation would store the passport photo that belongs in that particular passport. This Photo class holds its location or file name and the pixel data of the image. In this lab the pixel data is only used to show how memory maybe used when a program uses large objects. We will not...

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