Create a C++ project with 2 classes, Person and Birthdate. The description for e
ID: 3734224 • Letter: C
Question
Create a C++ project with 2 classes, Person and Birthdate. The description for each class is shown below:
Birthdate class:
private members: year, month and day
public members: copy constructor, 3 arguments constructor, destructor, setters, getters and age (this function should return the age)
Person class:
private members: firstName, lastName, dateOfBirth, SSN
public members: 4 arguments constructor (firstName, lastName, datOfBirth and SNN), destructor and printout
Implementation:
- use the separated files approach
- implement all the methods for the 2 classes
- make sure to use the member initialization list
create 3 objects and test the implemented methods.
Using the files:
BirthDate.h:
#ifndef BIRTHDATE_H
#define BIRTHDATE_H
#include <iostream>
using namespace std;
class birthDate{
private:
int year, month, day;
public:
birthDate(birthDate&);
birthDate(int, int, int);
~birthDate(){}
void setYear(int);
void setMonth(int);
void setDay(int);
int getYear();
int getMonth();
int getDay();
int age();
};
#endif
Person.h
#ifndef PERSON_H
#define PERSON_H
#include "birthDate.h"
#include <string>
class Person{
private:
string firstName, lastName;
birthDate DOB;
int SSN;
public:
Person(string, string, birthDate, int);
~Person(){}
void printout();
};
#endif
BirthDate.cpp:
#include "birthDate.h"
Person.cpp:
#include"Person.h"
Source.cpp:
#include"Person.h"
void main(){
system("pause");
}
Explanation / Answer
here is your two cpp file : ------->>>>
birthDate.cpp : -------->>>>>>>>>
#include "birthDate.h"
#include<ctime>
birthDate::birthDate(int d,int m,int y){
setDay(d);
setMonth(m);
setYear(y);
}
birthDate::birthDate(birthDate &oth){
day = oth.day;
month = oth.month;
year = oth.year;
}
void birthDate::setDay(int d){
if(d > 0 && d <= 31){
day = d;
}
}
void birthDate::setMonth(int m){
if(m > 0 && m < 13){
month = m;
}
}
void birthDate::setYear(int y){
year = y;
}
int birthDate::getDay(){
return day;
}
int birthDate::getMonth(){
return month;
}
int birthDate::getYear(){
return year;
}
int birthDate::age(){
time_t now = time(0);
tm *ltm = localtime(&now);
return ltm->tm_year - year;
}
Person.cpp : ----------->>>>>>>>>>>
#include "Person.h"
Person::Person(string fn,string ln,birthDate b,int s):firstName(fn),lastName(ln),DOB(b),SSN(s){
}
void Person::printout(){
cout<<" SSN : "<<SSN;
cout<<" Name : "<<firstName<<" "<<lastName;
cout<<" Age : "<<DOB.age();
}