Question

- Classes Write a C++ program that creates a class called Date that includes three pieces...

- Classes

Write a C++ program that creates a class called Date that includes three pieces of information as data

members, namely:

 month (type int)

 day (type int)

 year (type int).

Program requirements:

- Provide set and a get member functions for each data member.

- For the set member functions assume that the values provided for the year and day are correct,

but in the setMonth member function ensure that the month value is in the range 1–12:

o If the month is less than 1 set it to 1.

o If the month is greater than 12 set it to 12.

- Your class should have a constructor with three parameters (one for each data member) and it

must use the set member functions to initialize the three data members.

- Provide a member function displayDate that displays the month, day and year separated by

forward slashes (/).

Write a test program that demonstrates class Date’s capabilities, by:

- Creating an object of type Date with today’s date (correct date).

- Creating a second object of type Date with the incorrect month (-1/1/2021).

- Creating a third object of type Date with the incorrect month (13/1/2021).

- Call the displayDate member function for all three objects.

The following is an example of how your program should run:

First date is 11/21/2016

Second date is 1/1/2021

Third date is 12/1/2021

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

Hi, Please find my code.

Please let me know in case of any issue.

############## Date.h ########################


class Date{

private:
   int year;
   int month;
   int day;

public:
   Date(int d, int m, int y);
   void setDay(int d);
   void setMonth(int m);
   void setYear(int y);

   int getDay();
   int getMonth();
   int getYear();

   void displayDate();
};

############## Date.cpp ########################

#include <iostream>
#include "Date.h"
using namespace std;

Date::Date(int d, int m, int y){
   setDay(d);
   setMonth(m);
   setYear(y);
}

void Date::setDay(int d){
   day = d;
}

void Date::setMonth(int m){
   if(m < 1)
       month = 1;
   else if(m > 12){
       month = 12;
   }
   else
       month = m;
}

void Date::setYear(int y){
   year = y;
}

int Date::getDay(){
   return day;
}

int Date::getMonth(){
   return month;
}

int Date::getYear(){
   return year;
}

void Date::displayDate(){
   cout<<month<<"/"<<day<<"/"<<year<<endl;
}

############## DateTest.cpp ########################


#include <iostream>
#include "Date.h"
using namespace std;

int main(){

   // day, month, year
   Date date(11, 23, 2013);
   date.displayDate();

   Date date1(1, -1, 1213);
   date1.displayDate();

   Date date2(3, 13, 2013);
   date2.displayDate();


   return 0;
}

secondweek secondweek g+ DateTest.cpp Date.cpp secondweek ./a.out 12/11/2013 1/1/1213 12/3/2013 secondweek

Add a comment
Know the answer?
Add Answer to:
- Classes Write a C++ program that creates a class called Date that includes three pieces...
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
  • In JAVA 1. Create a class called Rectangle that has integer data members named - length...

    In JAVA 1. Create a class called Rectangle that has integer data members named - length and width. Your class should have a default constructor (no arg constructor) Rectangle() that initializes the two instance variables of the object Rect1. The second overloading constructor should initializes the value of the second object Rect2. The class should have a member function named GetWidth() and GetLength() that display rectangle's length and width. OUTPUT: Rectl width: 5 Rectl length: 10 Enter a width :...

  • The following program contains the definition of a class called List, a class called Date and...

    The following program contains the definition of a class called List, a class called Date and a main program. Create a template out of the List class so that it can contain not just integers which is how it is now, but any data type, including user defined data types, such as objects of Date class. The main program is given so that you will know what to test your template with. It first creates a List of integers and...

  • Create a class named Date in C++ Class has 3 private data items (name the data...

    Create a class named Date in C++ Class has 3 private data items (name the data items month, day, year) int month int day int year Member functions ‘getters’   a getter for each data item Use ‘get’ and the data items name The data item name must be capitalized Return the data item Example: int getMonth()        {return month;} ‘setters’   a setter for each data item Use ‘set’ and the data items name The data item name must be capitalized Change...

  • Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and...

    Please help with this assignment. Thanks. 1. Write a C++ program containing a class Invoice and a driver program called invoiceDriver.cpp. The class Invoice is used in a hardware store to represent an invoice for an item sold at the store. An invoice class should include the following: A part number of type string A part description of type string A quantity of the item being purchased of type int A price per item of type int A class constructor...

  • dateInformation class. visual basic. Crate a class called DateInformation that includes three pieces of information as...

    dateInformation class. visual basic. Crate a class called DateInformation that includes three pieces of information as intense variable- a month(type Integer), a day(type Integer) and year (type Integer). Your class should provide properties that enables a client of the class to get and set the month, day, year values.The set acessor should perform validation and throw exception for invalid values. You class should have a constructor that initializes the three instances variables and uses the class's property to set each...

  • Please write the program in C++ Problem Description: Design a class called Date. The class should...

    Please write the program in C++ Problem Description: Design a class called Date. The class should store a date in three integers: month, day, and year.     Create the necessary set and get methods.       Design member functions to return the date as a string in the following formats:                                     12/25/2012                                     December 25, 2012                                     25 December 2012 Input Validation: Do not accept values for day greater than 30 or less than 1. Do not accept values for month...

  • 12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1)...

    12.2 LAB 12.2: Classes (Date) C++ Rewrite the class definition of Date class (From Lab 12.1) including 4 added member functions: bool isLeapYear(): this function will return true if the year of the date is a leap year. int daysPassed(): This function will return how many days has been passed in this year. int daysLeft(): This function will return how many days has left of this year. int diff(Date AnotherDate): This function will return the difference between the calling object...

  • HW_8b - Famous People - One class used by another class - This exercise requires the...

    HW_8b - Famous People - One class used by another class - This exercise requires the declaration of two classes: o Famous People O Date - 5 Files are required: o Date.h o Date.cpp o Famous People.h o Famous People.cpp o main.cpp File: Date.h Private data members: O _month o _day 0 year - Public member functions: o Default constructor • Assign O's to all three data members. o Overloaded constructor • Three integer values are passed as arguments from...

  • The class dateType is designed to implement the date in a program, but the member function...

    The class dateType is designed to implement the date in a program, but the member function setDate and the constructor do not check whether the date is valid before storing the date in the data members. Rewrite the definitions of the function setDate and the constructor so that the values for the month, day, and the year are checked before storing the date into the data members. Add a function member, isLeapYear, to check whether a year is a leap...

  • Design and implement a C++ class called Date that has the following private member variables month...

    Design and implement a C++ class called Date that has the following private member variables month (int) day (nt) . year (int Add the following public member functions to the class. Default Constructor with all default parameters: The constructors should use the values of the month, day, and year arguments passed by the client program to set the month, day, and year member variables. The constructor should check if the values of the parameters are valid (that is day 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
ADVERTISEMENT