Question

C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming...

C++

3. Day of the Year Modification

Modify the DayOfYear class, written in an earlier Programming Challenge, to add a

constructor that takes two parameters: a string representing a month and an integer in the

range 0 through 31 representing the day of the month. The constructor should then initialize

the integer member of the class to represent the day specified by the month and day of month

parameters. The constructor should terminate the program with an appropriate error

message if the number entered for a day is outside the range of days for the month given.

Add the following overloaded operators:

++ prefix and postfix increment operators. These operators should modify the DayOfYear

object so that it represents the next day. If the day is already the end of the year, the

new value of the object will represent the first day of the year.

-- prefix and postfix decrement operators. These operators should modify the DayOfYear

object so that it represents the previous day. If the day is already the first day of the

year, the new value of the object will represent the last day of the year.

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

DayofYear.cpp

#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
class DayOfYear
{
private:
int year;
int day;
string month;
int count=0;
public:
DayOfYear& operator ++();
DayOfYear& operator --();
public:
DayOfYear(char month[], int day)
   {
       for(int i=0;i<12;i++)
       {
           if(month_names(i) == month)
           {
               if( day > 0 && day <= month_days(i))
               {
                   this->month = month;
                   this->day = day;
                   year =0;
                   for(int j=0;j<count;j++)
                   {
                       year=year+ month_days(j);
                   }
                   year=year+day;
                   cout<<"The Day Of The Year is:"<<year<<endl;
               }
               else
               {
                   cout<<"Incorrect date or Day"<<endl;
                   exit(0);
               }
              
           }
           count++;
       }
   }
   public:
   string month_names(int month_number)
   {
       string month_names[] ={"January","February","March","April", "May","June","July","August","September","October","November","December"};
       return month_names[month_number];
   }
   public:
   int month_days(int month_number)
   {
       int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
       return month_days[month_number];
   }
};
int main()
{
   char month[12];
   int day;
   cout<<"-------------------------------------"<<endl;
   cout<<"Day of The Year Modification"<<endl;
   cout<<"Enter The Month of Year:"<<endl;
   cin>>month;
   cout<<"Enter the Day Of The Month"<<endl;
   cin>>day;
   DayOfYear obj(month,day);
   cout<<"------------End---------------"<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ 3. Day of the Year Modification Modify the DayOfYear class, written in an earlier Programming...
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
  • Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to...

    Using simple, college level c++ programming Design a class called NumDays. The class’s purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing...

  • Design a class called NumDays. The class’ purpose is to store a value that represents a...

    Design a class called NumDays. The class’ purpose is to store a value that represents a number of work hours and convert it to a number of days. For example, 8 hours would be converted to 1 day, 12 hours would be converted to 1.5 days, and 18 hours would be converted to 2.25 days. The class should have a constructor that accepts a number of hours, as well as member functions for storing and retrieving the hours and days....

  • Assuming that a year has 365 days, write a class named DayOfYear that takes an integer...

    Assuming that a year has 365 days, write a class named DayOfYear that takes an integer representing a day of the year and translates it to a string consisting of the month followed by day of the month. For example, Day 2 would be January 2. Day 32 would be February 1. Day 365 would be December 31. The constructor for the class should take as parameter an integer representing the day of the year, and the class should have...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • Take the Pizza class from assignment # 1 and modify it in the following way: -...

    Take the Pizza class from assignment # 1 and modify it in the following way: - Rename it to DeluxePizza. - Add a stuffedWithCheese (boolean) attribute to the class. - Add a veggie Topping (integer) attribute to the class. This attribute keeps track of the number of vegetables toppings excluding mushrooms. Add a static attribute numberOfPizzas (integer) which will keep track of the number of DeluxePizza objects created Update the existing non-default constructor such that it increments the numberOfPizzas by...

  • Write a class Date with: Three instance variable : day, month and year all of type...

    Write a class Date with: Three instance variable : day, month and year all of type int. Define the following public methods for the class Date: A constructor Date() without parameter, make the date 1-1-2000   A constructor Date(int d, int m, int y) with parameters that provide the values attributes. Include accessor methods: getDay getMonth getYear Include mutator methods: setDay: make sure that day between 1 to 30 setMonth: make sure that month between 1 to 12 setYear A method...

  • Please create a C++ class called myDate. It should have the following operations: myDate() – default...

    Please create a C++ class called myDate. It should have the following operations: myDate() – default constructor. This will set the date to May 10, 1959. myDate(int M, int D, int Y) – overloaded constructor. This will set the date to the values passed in through the parameter list represented by Month, Day and Year. void display() – display the date in the following format (May 11, 1959) Do NOT print a linefeed after the date. void incrDate(int N) –...

  • House Class Specification The House class represents a house. A house has an address (address instance...

    House Class Specification The House class represents a house. A house has an address (address instance variable), a year it was built (built instance variable), and the names of residents of the house (residents instance variable). The declaration of each variable follows. private String address; private int built; private StringBuffer residents; The class methods are: 1. Constructor - Takes a string (representing the address) and an integer (representing the year the house was built) as parameters, and initializes the corresponding...

  • Create a class called Date212 to represent a date. It will store the year, month and...

    Create a class called Date212 to represent a date. It will store the year, month and day as integers (not as a String in the form yyyymmdd (such as 20161001 for October 1, 2016), so you will need three private instance variables. Two constructors should be provided, one that takes three integer parameters, and one that takes a String. The constructor with the String parameter and should validate the parameter. As you are reading the dates you should check that...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

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