15. Create a class called Date that includes three pieces of information as data members a month, a day and a year. Provide a set and a get function for each data member. Provide a member function display Date that displays the month, day and year separated by forward slashes (/).

C++ How to Program (8th Edition)

Chapter 3

Introduction to Classes, Objects and Strings

Exercise

Problem # 15:

Create a class called Date that includes three pieces of information as data members a month (type int), a day (type int) and a year (type int). Your class should have a constructor
with three parameters that uses the parameters to initialize the three data members. For the
purpose of this exercise, assume that the values provided for the year and day are correct, but ensure that the month value is in therange1–12; if it isn’t, set the month to 1. Provide a set and a get function for each data member. Provide a member function display Date that displays the month, day and year separated by forward slashes (/). Write a test program that demonstrates class Date’s capabilities.

#include<iostream>
#include <string>
using namespace std;

class Date
{
 public:
  Date( int, int, int );
  void setMonth( int );
  int getMonth();
  void setDay(int);
  int getDay();
  void setYear(int);
  int getYear();
  void displayDate();
 private:
  int month;
  int day;
  int year;
};
Date::Date( int m, int d, int y)
{
 if(m>=1&&m<=12)
  month=m;
 else
 {
  month=1;
 }
 day=d;
 year=y;
}
void Date::setMonth( int m)
{
 if(m>=1&&m<=12)
  month=m;
 else
 {
  month=1;
 }
}
void Date::setDay(int d )
{
 day = d;
}
void Date::setYear(int y )
{
 year=y;
}
int Date::getMonth()
{
 return month;
}
int Date::getDay()
{
 return day;
}
int Date::getYear()
{
 return year;
}
void Date::displayDate(){
 cout<<month<<"/"<<day<<"/"<<year<<endl;
}
int main()
{
 Date Date1(8,18,2019);
 cout<<"Date is ";
 Date1.displayDate();
 Date1.setMonth(0);
 Date1.setYear(2000);
 Date1.displayDate();
}

Let me know in the comment section if you have any question.

Similar Posts:

Chapter 3

  1. Modify class GradeBook, Include a string data member for course instructor’s name, Provide a set function and a get function to change and retrieve it, Modify display Message to output the welcome message and course name.
  2. Create an Account class that a bank might use to represent customers bank accounts. Include a data member to represent the account balance. Provide three member functions. Member function credit should add an amount to the current balance. Member function debit should withdraw money from the Account. Member function get Balance should return the current balance.
  3. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four data members a part number, a part description, a quantity of the item being purchased and a price per item. 
  4. Create a class called Employee that includes three pieces of information as data members a first name, a last name and a monthly salary. Provide a set and a get function for each data member. Create two Employee objects and display each object’s yearly salary. Then give each Employee a 10 percent raise and display each Employee’s yearly salary again.

 Chapter 2


Comments