Question

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 to "January."

A constructor that accepts the name of the month as an argument. It should set name to the value passed as the argument and set monthNumber to the correct value.

• A constructor that accepts the number of the month as an argument. It should set monthNumber to the value passed as the argument and set name to the correct month name.

• Appropriate set and get functions for the name and monthNumber member variables.

• Prefix and postfix overloaded ++ operator functions that increment monthNumber and set name to the name of next month. If monthNumber is set to 12 when these functions execute, they should set monthNumber to 1 and name to "January".

• Prefix and postfix overloaded -- operator functions that decrement monthNumber and set name to the name of previous month. If monthNumber is set to 1 when these functions execute, they should set monthNumber to 12 and name to "December".  

• Make operator overloading for asigning (=) an object to another.

• Overload the comparetor operators ==, > and <.

Also, you should overload cout's << operator and cin's >> operator to work with the Month class. Demonstrate the class in a program (main.cpp).

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

//Month class
#ifndef MONTH_H
#define MONTH_H
#include
#include
using namespace std;
class Month
{
private:
   string name;
   int monthNumber;
public:
   Month() ;
   Month(string) ;
   Month(int x) ;
   void setName(string month_pointer) ;
   void setNumber(int x) ;
   string getName() ;
   int getNumber() ;
   Month operator++() ;
   Month operator++(int) ;
   Month operator--() ;
   Month operator--(int) ;
  
   friend ostream &operator << (ostream &strm, Month &obj) ;
   friend istream &operator >> (istream &strm, Month &obj) ;


};
#endif

---------------------------------------------------------------------


//Month.cpp
//include header files
#include
#include
//include Month.h
#include "Month.h"
using namespace std;
Month::Month()
{
   monthNumber = 1;
   name = "January";
}
Month::Month(string monthName)
{
   setName(monthName);
}
Month::Month(int x)
{
   setNumber(x);
}
   void Month::setName(string month_pointer)
   {
       name = month_pointer;
   if (month_pointer == "January")
       monthNumber = 1;
   else if(month_pointer == "February")
       monthNumber = 2;
   else if(month_pointer == "March")
       monthNumber = 3;
   else if(month_pointer == "April")
       monthNumber = 4;
   else if(month_pointer == "May")
       monthNumber = 5;
   else if(month_pointer == "June")
       monthNumber = 6;
   else if(strcmp(month_pointer.c_str(),"July")==0)
       monthNumber = 7;
   else if(month_pointer == "August")
       monthNumber = 8;
   else if(month_pointer == "September")
       monthNumber = 9;
   else if(month_pointer == "October")
       monthNumber = 10;
   else if(month_pointer == "November")
       monthNumber = 11;
   else if(month_pointer == "December")
       monthNumber = 12;
   }
   void Month::setNumber(int x)
   {
       monthNumber = x;
   switch (monthNumber)
   {
   case 1:
       name = "January";
       break;
   case 2:
       name = "February";
       break;
   case 3:
       name = "March";
       break;
   case 4:
       name = "April";
       break;
   case 5:
       name = "May";
       break;
   case 6:
       name = "June";
       break;
   case 7:
       name = "July";
       break;
   case 8:
       name = "August";
       break;
   case 9:
       name = "September";
       break;
   case 10:
       name = "October";
       break;
   case 11:
       name = "November";
       break;
   case 12:
       name = "December";
       break;
   }
   }
   string Month::getName()
   {
       return name;
   }
   int Month::getNumber()
   {
       return monthNumber;
   }
   Month Month::operator++()
   {
       Month temp(monthNumber);
       ++monthNumber;
       if(monthNumber == 13)
           monthNumber = 1;
       temp.setNumber(monthNumber);      
       name=temp.getName();
       return temp;
   }
   Month Month::operator++(int)
   {
       Month temp(monthNumber);
       monthNumber++;

       if(monthNumber == 13)
           monthNumber = 1;

       temp.setNumber(monthNumber);
      
       name=temp.getName();
      
       return *this;
   }
   Month Month::operator--()
   {
       Month temp(monthNumber);
       --monthNumber;
       if(monthNumber == 0)
           monthNumber = 12;
       temp.setNumber(monthNumber);      
       name=temp.getName();
       return *this;
   }
   Month Month::operator--(int)
   {
       Month temp(monthNumber);
       monthNumber--;
       if(monthNumber == 0)
           monthNumber = 12;
       temp.setNumber(monthNumber);      
       name=temp.getName();
       return temp;
   }
ostream &operator << (ostream &strm, Month &obj)
{
   strm << obj.getNumber() << " is the number of the month." << endl;
   strm << obj.getName() << " is the name of the month." << endl;
   return strm;
}
istream &operator >> (istream &strm, Month &obj)
{
   int x;
   cout << "Enter the month number: ";
   strm >> x;
   obj.setNumber(x);
   return strm;
}

---------------------------------------------------------------------


// Month class tester
//iclude header files
#include
#include
//include Month.h
#include "Month.h"
using namespace std;
int main()
{
   // Create a Month object
   Month m("April");
   cout << m.getName() << endl;
   cout << m.getNumber() << endl << endl;
   m.setName("December");
   cout << m.getName() << endl;
   cout << m.getNumber() << endl << endl;
   m.setNumber(2);
   cout << m.getName() << endl;
   cout << m.getNumber() << endl << endl;;
  
   cout<<"Incrementing the month number"<    //pre incrementing m object
   cout << ++m << endl;  
   cout<<"Incrementing the month number"<    //post incrementing m object
   cout << m++ << endl;
   cout << m << endl << endl;
   // pre decrement operator
   cout << --m << endl;
   //post decrement operator
   cout << m-- << endl;
   cout << m << endl << endl;

   system("pause");
   return 0;
}

---------------------------------------------------------------------

Sample output:

Add a comment
Know the answer?
Add Answer to:
Design a class named Month. The class should have the following private members:
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
  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named month...

    C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: • A default constructor that sets the monthNumber field to 1. • A constructor that accepts the number of month as an argument and sets...

  • 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....

  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods. A no- arg constructor that sets the monthNumber field to 1. A constructor that accepts the number of the month as an argument. It should...

  • All files require Javadoc documentation comments at the top to include a description of the program...

    All files require Javadoc documentation comments at the top to include a description of the program and an @author tag with your name  only.    -------------------------------------- Develop an exception class named InvalidMonthException that extends the Exception class.   Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December -------------------------------------- Develop a class named Month.  The class should define an integer...

  • (Classes and Objects) Design a class named Box. The class should have the following private members:...

    (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables to 0. b)...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the...

    Long Answer 2: (Classes and Objects) Design a class named Box. The class should have the following private members: width - A double member variablethat holds the width of the box. length – A double member variable for holding the length of the box. height – A double member variable of type double that holds the height of the box. In addition, provide the following member functions with full (inline) implementation. a) A default constructor that sets all member variables...

  • 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 named NumDays, to store a value that represents a number of hours and...

    Design a class named NumDays, to store a value that represents a number of hours and convert it to a number of days. For example, 24 hours would be converted to 1 day, 36 hours would be converted to 1.5 days, and 54 hours would be converted to 2.25 days. This class has two private member variables with data type double: one is named as hours; another is named as days. This class has a constructor that accepts a value...

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