Concept explainers
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 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:
Want to see more full solutions like this?
Chapter 7 Solutions
Absolute C++
Additional Engineering Textbook Solutions
Computer Science: An Overview (12th Edition)
C Programming Language
Java How to Program, Early Objects (11th Edition) (Deitel: How to Program)
Absolute Java (6th Edition)
Starting Out with Python (4th Edition)
Modern Database Management (12th Edition)
- Write a class named Patient that has member variables for first name, middle name, last name, phone number, name and phone number of emergency contact. The Patient class should have a constructor that accepts and argument for each member variable. The Patient class should also have accessor and mutator functions for each member variable. Write a class named Procedure that has member variables for name of procedure, date of procedure, name of physician, and charges for the procedure. This Procedure class represents a medical procedure that is performed on a patient. The Procedure class should have a constructor that accepts an argument for each member variable. The Procedure class should also have accessor and mutator functions for each member variable. Then write a program that creates an instance of the Patient class initialized with sample data that is entered by the user. Then, create an instance of the Procedure class for each procedure that is being performed on the patient. There…arrow_forwardRewrite the calculator program using a class called calculator. Your program will keep asking the user if they want to perform more calculations or quit and will have a function displayMenu to display the different functions e.g .(1 - addition, 2- subtraction, 3- multiplication, 4- division) Your program must have appropriate constructors and member functions to initialize, access, and manipulate the data members as well as : A member function to perform the addition and return the result A member function to perform the subtraction and return the result A member function to perform the multiplication and return the result A member function to perform the division and return the resultarrow_forwardPROGRAMMING LANGUAGE: C++ Write a program that has a base class named FlightCrew. This class shouldhave three data members: an integer to store the ID of the crew member, aninteger to store the number of years of service and another integer to storethe total salary of the member. Provide a parameterized constructor in theclass to set the values of the data members.Derive a class Pilot from FlightCrew to contain two additional data members,an integer to store the number of hours of flight and a boolean to storewhether the Pilot has military experience or not. Provide a paramete rizedconstructor in the Pilot class. Provide a function bonus() in the class wherethe bonus of a pilot is his number of flight hours times the 10% of his salary.Likewise, provide a function isEligible() in the class to find out if the pilot iseligible for promotion or not. A pilot is eligible for promotion to the nextrank if he has at least 5 years of experience and the number of total flighthours is greater…arrow_forward
- Program SpecificationUsing python, design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool . It will be set to true if the customer wishes to be on a mailing list, or false if the customer does not wish to be on a mail-ing list. Write appropriate accessor and mutator functions for these member variables. Next write a program which demonstrates an object of the CustomerData class in a program. Your program MUST use exception handling. You can choose how to implement the exception handling. Start your program with a welcome message Make sure…arrow_forwardWrite a full class definition for a class named Counter , and containing the following members: A data member counter of type int . A constructor that takes one int argument and assigns its value to counter . A function called increment that accepts no parameters and returns no value. increment adds one to the counter data member. A function called decrement that accepts no parameters and returns no value. decrement subtracts one to the counter data member. A function called getValue that accepts no parameters. It returns the value of the instance variable counter .arrow_forwardWrite a class Travel having two private data members (distanceCovered , Time) The class has three constructors which are • having no parameter – for setting values to zero or null. • having two parameters for assigning values to both data members. • Overload the above constructor and use this keyword to set the values of data members. • Provide getters/setters for data members. • A function which calculates and display speed of traveling. • Write a function show() displays values. Write test Application that demonstrates the Run class by calling all the three constructors and method, creating a Create Travel objects, and then display the traveling with speed. Calculate highest speed. Programming language : javaarrow_forward
- C++ Write a class named Car with member variables: yearModel, Make, Speed as private.Use a Constructor that accepts year model and make as arguments. It should also assign 0 to thespeed use accelerate and brake functions that add and subtract 5 from speed each time it is called.Demonstrate the class in a program that creates a Car object, and then calls the acceleratefunction 5 times. After each call to the accelerate function, get the current speed of the car anddisplay it. Then, call the brake function five times. After each call to the brake function, get thecurrent speed of the car and display itarrow_forwardSUBJECT: OOPPROGRAMMING LANGUAGE: C++ ALSO ADD SCREENSHOTS OF OUTPUT. Write a class Distance to measure distance in meters and kilometers. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen. Write another class Time to measure time in hours and minutes. The class should have appropriate constructors for initializing members to 0 as well as user provided values. The class should have display function to display the data members on screen. Write another class which has appropriate functions for taking objects of the Distance class and Time class to store time and distance in a file. Make the data members and functions in your program const where applicablearrow_forwardin c++ Write a class named Mobile that holds data about a mobile in a retail store. The class should have the following member variables: Name/brand. A string that holds name of the brand of phone. unitsOnHand. An int that holds the number of units currently in inventory. price. A double that holds the retail price. Write a constructor that accepts arguments for each member variable, appropriate functions that store values in these member variables, and accessor functions that return the values in these member variables. Once you have written the class, Write another class Store (link list) having Mobile as a node then provide function to insert new mobile in list and to Delete a mobile and to display all mobiles. Write a main program that shows all the functionality. Note: you have to implement all scenarios (insert and delete at start, end, in between).arrow_forward
- Create an Employee class that includes three private data members— firstName (type string), lastName (type string), and monthlySalary (type int ).It also includes several public member functions.1. A constructor initializes the three data members. 2. A setFirstName function accepts a string parameter and does not return any data. It sets the firstName.3. A getFirstName function does not accept any parameter and returns a string. It returns the firstName.4. A setLastName function accepts a string parameter and does not return any data. It sets the lastName.5. A getLastName function does not accept any parameter and returns a string. It returns the lastName.6. A setMonthlySalary function accepts an integer parameter and does not return any data. It sets the monthlySalary. If the monthly salary is less than or equal zero, set it to 1000 and it displays the employee’s first name, last name and the inputted salary with a statement “**==The salary is set to $1000.”7. A getMonthlySalary…arrow_forwardDesign 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…arrow_forwardThere are two types of data members in a class: static and non-static. Provide an example of when it might be useful to have a static data member in the actual world.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education