Create a C++ project with 2 classes, Person and Birthdate. The description for e
ID: 3733965 • 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
C++ code :
#include "stdafx.h"
#include "person.h"
Person::Person (char fName, char lName, char* SSN, int Birth_Year,
int Birth_Month, int Birth_Day){
fName = "";
lName = "";
SSN = "000000000";
Birth_Year = 0000;
Birth_Month = 00;
Birth_Day = 00;
}
int Person::findLength (){
int len = 0;
char *s=value;
for (;s!=''; s++, len++);
return len;
}
bool Person::validateSSN (char* soc){
value = soc;
Length = findLength();
if (Length < 9 && Length > 9)
return false;
else if (Length == 9)
return true;
}
void Person::SetfName (char* first){
fName = first;
}
void Person::SetlName (char* last){
lName = last;
}
void Person::SetSSN (char* soc){
SSN = soc;
}
void Person::SetBirth_Year (int byr){
Birth_Year = byr;
}
void Person::SetBirth_Month (int bmo){
Birth_Month = bmo;
}
void Person::SetBirth_Day (int bdy){
Birth_Day = bdy;
}
char* Person::GetfName (){
return fName;
}
char* Person::GetlName (){
return lName;
}
char* Person::GetSSN (){
return SSN;
}
int Person::GetBirth_Year (){
return Birth_Year;
}
int Person::GetBirth_Month (){
return Birth_Month;
}
int Person::GetBirth_Day (){
return Birth_Day;
}