I wrote this program but have a hard time implementing these instructions. I had it so messed up I took everything out. Please help and make me understand. I really tried to make the program at the bottom presentable but Im new to this site and learning how to make it better presentable. Thank You.
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
#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() << "/" << [output][/output][output][/output]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.\n\n";
else cout << " is not a leap year.\n\n";
}
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;
}
| |