Absolute C++,NO CODE INCLUDED (6th Edition)
Absolute C++,NO CODE INCLUDED (6th Edition)
6th Edition
ISBN: 9780134227078
Author: SAVITCH, Walter; Mock, KENRICK
Publisher: PEARSON
bartleby

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 7, Problem 1PP

Define a class called Month that is an abstract data type for a month. Your class will have one member variable of type int to represent a month (1 for January, 2 for February, and so forth). Include all the following member functions: a constructor to set the month using the first three letters in the name of the month as three arguments, a constructor to set the month using an integer as an argument (1 for January, 2 for February, and so forth), a default constructor, an input function that reads the month as an integer, an input function that reads the month as the first three letters in the name of the month, an output function that outputs the month as an integer, an output function that outputs the month as the first three letters in the name of the month, and a member function that returns the next month as a value of type Month. Embed your class definition in a test program.

Expert Solution & Answer
Check Mark
Program Plan Intro

Program plan:

1 . The following variables are used in the program:

  • monthvariable of integer data type is used to store the month.

2. The following methods are used in the program:

  • Default constructor.
  • Constructor having one integer argument.
  • Constructor having three characters arguments.
  • input() function to take the month number from the user.
  • inputMonthLetter() function to take the month's first three letters from the user.
  • getMonth() function to return the month number.
  • outputMonthLetter() function to display the month first three-letter.
  • nextMonth() function to return the next month.

Program description:

The main purpose of the program is to create the constructor to initialize the class member variable using the integer argument and the first three letters of the month. Also, display the next month. Test all the class member functions in a test program.

Explanation of Solution

Program:

//including essential header file 
#include <iostream>


//using standard namespace
using namespace std;  

//Month class
class Month 
{ 
    public:   

    int month;

    //default constructor     
    Month()        
    {            
        //initialize the month to 1 
       month = 1;        
    }

     //constructor to set the month by number      
    Month(int monthNumber)     
    {             
//set the month to the monthNumber 
        month = monthNumber;      
    }

    //constructor to set the month number using the Month letters         
    Month (char monthLetter1, char monthLetter2, char monthLetter3)         
    {             
        //when the monthLetter is jan
        if ((monthLetter1 == 'j') && (monthLetter2 == 'a') && (monthLetter3 == 'n'))              
        {                 
            //set the month to 1
            month = 1;             

        }              

//when the monthLetter is feb
        if ((monthLetter1 == 'f') && (monthLetter2 == 'e') && (monthLetter3 == 'b'))  
        {                 //set the month to 2 
            month = 2;           
        }              

//when the monthLetter is mar 
        if ((monthLetter1 == 'm') && (monthLetter2 == 'a') && (monthLetter3 == 'r')) 
        {                 //set the month to 3
            month = 3;

        }             

//when the monthLetter is apr
        if ((monthLetter1 == 'a') && (monthLetter2 == 'p') && (monthLetter3 == 'r'))     
        {   
              //set the month to 4 
            month = 4;

            Month(month); getMonth();
        }              
//when the monthLetter is may 

if ((monthLetter1 == 'm') && (monthLetter2 == 'a') && (monthLetter3 == 'y'))   
        {                 
//set the month to 5
            month = 5;             

        }               

//when the monthLetter is jun 
        if ((monthLetter1 == 'j') && (monthLetter2 == 'u') && (monthLetter3 == 'n'))  
        { 
                //set the month to 6 
            month = 6;            
        }

        //when the monthLetter is jul
        if ((monthLetter1 == 'j') && (monthLetter2 == 'u') && (monthLetter3 == 'l'))
        {
            //set the month to 7
            month = 7;             

        }

        //when the monthLetter is aug 
        if ((monthLetter1 == 'a') && (monthLetter2 == 'u') && (monthLetter3 == 'g'))  
        {
            //set the month to 8 
            month = 8;             

        } 

        //when the monthLetter is sep 
       if ((monthLetter1 == 's') && (monthLetter2 == 'e') && (monthLetter3 == 'p')) 
        {                 //set the month to 9 
            month = 9;            
        }

        //when the monthLetter is oct 
        if ((monthLetter1 == 'o') && (monthLetter2 == 'c') && (monthLetter3 == 't'))            
        {                 //set the month to 10
            month = 10;            
        }

        //when the monthLetter is nov 
        if ((monthLetter1 == 'n') && (monthLetter2 == 'o') && (monthLetter3 == 'v'))    
        {
         //set the month to 11 
            month = 11;             

        }

        //when the monthLetter is dec 
        if ((monthLetter1 == 'd') && (monthLetter2 == 'e') && (monthLetter3 == 'c')) 
        {                 //set the month to 12
            month = 12;            
        }    

    }   

    void input()       
    { 
        cout<<endl<< "Enter the month number: "; 
        cin>> month; 
        while (month < 1 || month > 12)        
        { 
            cout<< "Invalid month number. Month number must be between 1 to 12." <<endl;
            cout<< "Enter the month number again: ";
            cin>> month; 
        }         
    } 

    void inputMonthLetter()    
    { 
        //char letter1, letter2, letter3;
        char letter[4];
        cout<<endl<< "Enter the first three letters of the month: "; 
        cin>> letter;
        Month (letter[0], letter[1], letter[2]);    
    }  

    int getMonth()   
    {
        return month;  
    }        

    void outputMonthLetter()    
    { 
        if (month == 1)     
        { cout<< "The month is Jan";             } 
        if (month == 2)        
        { cout<< "The month is Feb";             } 
        if (month == 3)             
        { cout<< "The month is Mar";             }  
        if (month == 4)             
        { cout<< "The month is April";             } 
        if (month == 5)             
        { cout<< "The month is May";             }  
        if (month == 6)             
        { cout<< "The month is June";             }  
        if (month == 7)             
        { cout<< "The month is July";             }  
        if (month == 8)             
        { cout<< "The month is August";             } 
        if (month == 9)             
        { cout<< "The month is Sep";             }  
        if (month == 10)             
        { cout<< "The month is Oct";             }  
        if (month == 11)             
        { cout<< "The month is Nov";             }  
        if (month == 12)             
        { cout<< "The month is Dec";             }       
    } 

    //member function to return the next month as Month type     
    Month nextMonth()      
    {
        int m = ((month % 12 ) + 1 );
        return Month(m);     
    } 

}; 

int main()
{    
    //create objects of the class
    Month m1, m3;

    m1.input();
    m1.outputMonthLetter();

    m3=m1.nextMonth();
    cout<<endl<< "The next month is: "<<m3.getMonth(); 


    Month m2, m4;
    m2.inputMonthLetter();
    cout<<endl<< "month is: " << m2.getMonth();

    m4 = m2.nextMonth();
    cout<<endl<< "The next month is: " << m4.getMonth();

    return 0; 
}

Explanation:  

In the above program, a default constructor is created which initializes the variable month. Another constructor that takes an integer parameter also initializes the variable month. A third constructor takes three-character arguments. This constructor matches these arguments with the first three letters of each month and then initializes the variable month accordingly.

Inside the main() method, objects of the class Month are created. The input() method is used to take user input for the month in the form of an integer. The outputMonthLetter() is used to output the first three letters of the month corresponding to the user input. The nextMonth() function returns the month in the form of the object of the class Month. The inputMonthLetter() method is used to take user input for the first three letters of the name of the month. This method calls the parameterized constructor that takes three-character arguments.The getMonth() method returns the variable month. The nextMonth() function returns the month in the form of the object of the class Month.

Sample output:

Absolute C++,NO CODE INCLUDED (6th Edition), Chapter 7, Problem 1PP

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Q1) Write the Code the following scenario. Write an abstract function named receivePay in interface with return type double and no parameters. Write another incomplete function, name Show with return type void and an argument of type int in that interface. Build a class with name Bill that implements the above interface. Bill class with name BillId , itemsquantity and itemsprice with proper datatypes with private access . Define two constructors,one default and other is parameterized to initialize the class members . Now receivePay has to beimplemented in such a way that its displays product of itemsquantity and itemsprice and Display function will display the BillId.Can we do same task with abstract class instead of interface? NOTE:SUBJECT:CSHARP (VISUAL PROGRAMMING)
Write the definitions of the member functions of the class calendarType (designed in Programming Exercise 9) to implement the operations of the class calendarType. Write a test program to print the calendar for either a particular month or a particular year. For example, the calendar for September 2019 is: (check the picture)This is exercise 9: Using the classes extDateType (Programming Exercise 8) and dayType (Chapter 10, Programming Exercise 5), design the class calendarType so that, given the month and the year, we can print the calendar for that month. To print a monthly calendar, you must know the first day of the month and the number of days in that month. Thus, you must store the first day of the month, which is of the form dayType, and the month and the year of the calendar. Clearly, the month and the year can be stored in an object of the form extDateType by setting the day component of the date to 1 and the month and year as specified by the user. Thus, the class…
Create a program that has the following classes: Point - Just like in the lecture, with attributes x and y set to 0 by default. Circle - Contains two attributes, center and radius. center is of type Point defined above (initialize a Point instance for this), while radius is set to 0 by default. The program must have the function point_in_circle(c, p) where c is a Circle and p is a Point. The function returns True if the point is within the circle, and False otherwise. You may use the math module (and nothing else) if necessary. You may also create other functions that can help you code the main function much easier. Example usage   # Initialize a circle with center at (0, 0) and radius 1 (unit circle). c = Circle() c.center.x, c.center.y, c.radius = 0, 0, 1   # Initialize a point located at (1, 1) p = Point() p.x, p.y = 1, 1   # This returns False since (1, 1) is outside the circle point_in_circle(c, p)

Chapter 7 Solutions

Absolute C++,NO CODE INCLUDED (6th Edition)

Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY