Please add these directions to my current code (yes it works). So here is the wo
ID: 3531267 • Letter: P
Question
Please add these directions to my current code (yes it works). So here is the working stripped version of my code and here are the directions. Please I really need your help. All you have to do is add this to my current code. Plug it in please. It looks like alot but its not. It confuses me and Ive mess up everytime. Directions and my code is below. Thank You. Please Help...
**********************************************************************************************
Make the following changes/additions to my program please:
1) Move the code you currently have in the "Display function" into the class as a "friend function". Change all of the references to your class members so that you directly reference the class private variables.So instead of using "dHold.getMonth()" you would now code it "dHold.month"
2) Overload the ++ operator. This function will now add 1 to the current year in the class.
- Overload the -- operator. This function will now subtract 1 from the current year in the class.
- Overload the == operator. This function will compare the years from two classes and return a boolean, true if they are equal and false if they are not.
- Overload the < operator. This function will compare the years from two classes and return a boolean, true if the first year is less than the second year and false if it is not.
- Overload the > operator. This function will compare the years from two classes and return a boolean, true if the first year is greater than the second year and false if it is not.
3) Create a private static member variable called MilitaryTime that is an integer data type. static int MilitaryTime;
Create get and set member functions for this variable. On the set member function code the statement that will only allow the values of 12 and 24 to be valid. Tell the user if they have entered an invalid value.
4) Create 2 instances of the class in the main function
5) Call the friend function Display to display the contents of the two classes
- Call the overloaded ++ operator for class 1
- Call the overloaded -- operator for class 2
- Call the friend function Display to display the contents of the two classes
Write the code that will compare class 1 to class 2 using the overloaded comparison operators. Display on the screen the results of the comparison.
Explanation / Answer
#include "stdafx.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class date
{
private:
int month;
int day;
int year;
public:
date() { }
date(int m, int d, int y);
void setmonth(int m);
void setday(int d);
void setyear(int y);
int getmonth()
{ return month; }
int getday()
{ return day; }
int getyear()
{ return year; }
bool leapYear();
string getMonthName();
};
bool date::leapYear()
{
if ((year % 400 == 0 || year % 100 != 0) && (year % 4 == 0))
return true;
else
return false;
}
void date::setmonth(int m)
{
if (m > 12 || m < 1)
cout << "Error";
else
month = m;
}
void date::setday(int d)
{
if (d > 31 || d < 1)
cout << "Error";
else
day = d;
}
void date::setyear(int y)
{
if (y > 2013 || y < 1200)
cout << "Error";
else
year = y;
}
date::date(int m, int d, int y)
{
month = m;
day = d;
year= y;
}
string date::getMonthName()
{
switch (month)
{
case 1 : return "January"; break;
case 2 : return "Febraury"; break;
case 3 : return "March"; break;
case 4 : return "April"; break;
case 5 : return "May"; break;
case 6 : return "June"; break;
case 7 : return "July"; break;
case 8 : return "August"; break;
case 9 : return "September"; break;
case 10 : return "October"; break;
case 11 : return "November"; break;
case 12 : return "December"; break;
}
}
void Display(date &dHold)
{
cout << dHold.getmonth() << "/" << dHold.getday() << "/" << dHold.getyear() << endl;
cout << dHold.getMonthName() << " " << dHold.getday() << ", " << dHold.getyear() << endl;
cout << dHold.getday() << " " << dHold.getMonthName() << " " << dHold.getyear() << endl;
cout << dHold.getyear();
if (dHold.leapYear()) cout << " is a leap year. ";
else cout << " is not a leap year. ";
}
int main()
{
int month;
int day;
int year;
cout << "Enter Month between 1 and 12" << endl;
cin >> month;
cout << "Enter day from 1 to 31)" << endl;
cin >> day;
cout << "Enter Year)"<< endl;
cin >> year;
date newDate(month, day, year);
Display(newDate);
date D2;
D2.setday(15);
D2.setmonth(6);
D2.setyear(2000);
Display (D2);
system ("PAUSE");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.