0% found this document useful (0 votes)
108 views

Assign

The document describes an assignment to extend a Date class by adding various constructors, methods, and validation checks. It includes requirements to add a default constructor, parameterized constructor, copy constructor, destructor, and static validation methods. It also provides C++ source code implementing the Date class and tests various date examples and invalid dates.

Uploaded by

RajeevDutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views

Assign

The document describes an assignment to extend a Date class by adding various constructors, methods, and validation checks. It includes requirements to add a default constructor, parameterized constructor, copy constructor, destructor, and static validation methods. It also provides C++ source code implementing the Date class and tests various date examples and invalid dates.

Uploaded by

RajeevDutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 4: Class Constructors

REQUIREMENTS:
Problem Description:

Extend the Date Class that you created in Assignment 3

Include the following methods:


1. Default Constructor: Initializes Date object to Jan 1, 1900
2. Parameterized Constructor
3. Copy Constructor
4. Destructor
5. staticisValidYear predicate method: valid range: 1900 to current year.
6. static isValidMonth predicate method
7. static isValidDay predicate method for a given month
8. static isLeapYear predicate method
Implement a test program to demonstrate your class methods. Identify each test.

Author: Kenny Fan


Date: 9/11/16
Assignment 3: Class Construct
Testing Results:
Current Date Example:
12/25/2012
December 25, 2012
25 December, 2012
Invalid Date Example:
The month is invalid.
The day is invalid.
The year is invalid.
The date is invalid.
The date is invalid.
The date is invalid.

C++ Source Code:


#include <iostream>
#include <string>
using namespace std;
class Date
{
private:
int month, year, day; public:
Date();
Date(int, int, int);
~Date();
Date(Date);
void setDay(int);
void setMonth(int);
void setYear(int);
void getDateA(); //get date format A (12/25/2012)
void getDateB(); //get date format B (December 25, 2012)
void getDateC(); //get date format C (25 December 2012)
bool ValidDate();
static bool isvalidDay(int ,int );
static bool isValidMonth(int );
static bool isValidYear(int );
static bool isLeapYear(int );
};
Date::Date()
{
month = 1; day = 1; year = 1900;
}
Date::Date(int m, int d, int y)
{
month = 1; day = 1; year = 1900;
setMonth(m);
setDay(d);
setYear(y);
}
Date::~Date()
{
month=0;day=0;year=0;
}
Date::Date(Date D)
{
this->month=D::month;
this->date=D::date;
this->year=D::year;
}
void Date::setDay(int d)
{
if (d < 1 || d > 30)
{
cout << "The day is invalid." << endl;
}

else
{
}

day = d;

}
void Date::setMonth(int m)
{
if (!(Date::isValidMonth(m)))
{
cout << "The month is invalid." << endl;
}
else
{
month = m;
}
}
void Date::setYear(int y)
{
if (!(Date::isValidYear(y)))
{
cout << "The year is invalid." << endl;
}
else
{
year = y;
}
}
void Date::getDateA()
{
if (validDate() == true)
{
cout << month << "/" << day << "/" << year << endl;
}
else
{
cout << "The date is invalid." << endl;
}
}
void Date::getDateB()
{
if (validDate() == true)
{
string name[] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December"};
cout << name[month - 1] << " " << day << ", " << year << endl;
//we use month - 1 because the array name starts at 0
}
else
{

cout << "The date is invalid." << endl;

}
void Date::getDateC()
{
if (validDate() == true)
{
string name[] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October", "November",
"December"}; //see getDateB for why we need this
cout << day << " " << name[month - 1] << ", " << year << endl;
}
else
{
cout << "The date is invalid." << endl;
}
}
bool Date::validDate()

if (month >= 1 && month <= 12 && day >= 1 && day <= 30 && year >= 1900
&& year <= 2025)
{
return true;
}
else
{
return false;
}
}
static bool Date::isValidYear(int y)
{
if (y < 1900 || y > 2016)
{
return false;
}
}
static bool Date::isValidMonth(int m)
{
if(m<1||m>12)
{
return false;
}
}
static bool Date::isValidDay(int m,int d)
{
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12)
{
if(d<1||d>31)
return false;
}
if(m==2)
{
if(isLeapYear()==true)
{
if(d<1||d>29)

return false;
}
else
{
if(d<1||d>28)
return false;
}
}
if(m==4||m==6||m==9||m==11)
{
If(d<1||d>30)
return false;
}
return true;
}
static bool Date::isLeapYear(int y)
{
if(y%4==0)
return true;
return false;
}
int main()
{
int x;
Date();
cout << "Default Date Example: " << endl;
testdate.getDateA(); //test format A
testdate.getDateB(); //test format B
testdate.getDateC(); //test format C
cout << "Invalid Date Example: " << endl;
Date invaliddate(13, 33, 2060);

invaliddate.getDateA();
invaliddate.getDateB();
invaliddate.getDateC();
cin >> x;
return 0;

You might also like