Print Friendly and PDF
(Date Class) 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 the range 1 –1 2; if it isn’t, set the month to 1 . Provide a set and a get function for each data member. 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.Solution:
(Date Class) 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 the range 1 –1 2; 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.

Solution:

#include <iostream>
#include <cstdlib>
#include "Date. h"
using std: : cout;
using std: : endl;
using std: : cin;
int main()
{
//create Date class object
MyDate mDate( 9, 25, 2012 ) ;
//display initial date
mDate. displayDate() ;
//test program that demonstrates class Date’ s capabilities
cout << "I can guess your birthday. \n"
<< "Please complete following steps and enter final number. \n"
<< "1. Multiply the DAY of month in which you were born by 2. \n"
<< "2. Add 5 to new number. \n"
<< "3. Multiply new number by 50. \n"
<< "4. Add the number that represents the month\n(1‐January, 2‐February etc) to new number. \n"

<< "5. Enter the result: ";
int result;
cin >> result;
mDate. guessBirthday(result) ;
// to pause
char a;
cin >> a;
return 0;
}


// Date. h file
#include <cstdio>
#include <cstdlib>
#include <iostream>
class MyDate
{

public:
MyDate(int, int, int) ;
// get and set functions for month/day/year
int getDay() ;
void setDay(int) ;
int getMonth() ;
void setMonth(int) ;
int getYear() ;
void setYear(int) ;
void displayDate() ; // display function
void guessBirthday(int) ;
private:
int day;
int month;
int year;
};

// Date. cpp file
#include <iostream>
using std: : cout;
using std: : endl;
#include "Date. h"
//initialization with constructor
MyDate: : MyDate(int month, int day, int year)
{
if(month < 1| | month >12)
{
setMonth(1) ;
cout << "Month set as 1" << endl;
}

else
setMonth(month) ;
setDay(day) ;
setYear(year) ;
}

// get day function
int MyDate: : getDay()
{
return day;
}


// get month function
int MyDate: : getMonth()
{
return month;
}

// get year function

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

// set day function
void MyDate: : setDay(int d)
{
day = d;
}

// set month function
void MyDate: : setMonth(int m)
{
month = m;
}

// set year function
void MyDate: : setYear(int y)
{
year = y;
}

// display date function
void MyDate: : displayDate()
{
cout << getMonth() << "/" << getDay() << "/" << getYear() << endl;
}

// guess birthday function
void MyDate: : guessBirthday(int res)
{
int x;
x = res 250;
setDay(x / 100) ;
setMonth(x % 100) ;
cout << "Your birthday: Month: " << getMonth() << " Day: " << getDay() << endl;
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: