In: Computer Science
C++
* Program 2:
Create a date class and Person
Class. Compose the date class in the
person class.
First, Create a date class.
Which has integer month, day and year
Each with getters and setters. Be sure that you
validate the getter function inputs:
2 digit months - validate 1-12 for month
2 digit day - 1-3? max for day - be sure the
min-max number of days is validate for specific month
Use 4 digits for the
year... - validate greater or equal to 0001
Have a default constructor which set values to 01 01
1900
Have a parameterized constructor which lets you set
date
* Next, Create a person class
Which has string firstname, lastname ( private members
)
Date of birth (using new date class YOU declared -
place declaration as private member in person )
Write getter and setters for firstname and last name (
public members )
Write getter and setters for month, day and year for
private data declaration ( public members )
Write a person default constructor function that
defaults "None" "None" for firstname and lastname ( public members
)
Write a person parmaterized constructor function that
can pass data to firstname and lastname, and
the composed birthdate. ( public
members )
* Code and test a person class
Declare a person Person1 using the default
constructor, to test it.
Using getters and setters of the
person class to test fully.
Declare a person Person2 using the parmaterized
constructor, to test it.
Using getters and setters of the
person class to test fully.
#include <iostream>
using namespace std;
// Online C++ compiler to run C++ program online
#include <iostream>
class Date
{
public: //access control
int day; //these data members
int month; //can be accessed
int year; //from anywhere
Date() // setter
{ // Constructor without parameters
day = 01;
month = 01;
year = 1900;
}
Date(int x, int y, int z) // setter
{ // Constructor with parameters
if(x>=01 && x<=31 && y>=1 && y<=12 && z>=0001)
{
day = x;
month = y;
year = z;
}
else
{
cout << "Invalid Input";
}
}
public:
int dayGetter()
{
return day;
}
public:
int monthGetter()
{
return month;
}
public:
int yearGetter()
{
return year;
}
};
class Person
{
private: //access control
string firstName; //these data members
string lastName; //can be accessed
Date dateOfBirth; //from anywhere
public:
Person(string x, string y, Date d) // setter
{ // Constructor with parameters
firstName = x;
lastName = y;
Date dateOfBirth = d;
}
public:
Person()
{ // Constructor without parameters (setter)
firstName = "None";
lastName = "None";
}
public:
string firstNameGetter()
{
return firstName;
}
public:
string lastNameGetter()
{
return lastName;
}
};
int main() {
cout << "Hello world!\n\n";
Date d1(17, 03, 2020);
cout << d1.yearGetter() << "\n";
Person p1;
cout << p1.lastNameGetter() << "\n";
}