Answer:
Check the explanation
Explanation:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include "Date.h"
#include "Person.h"
using namespace std;
const int MAXSIZE = 50;
// Prototypes go here
int loadArray(string fileName, Person students[]);
void sortByName(Person students[], int numE);
void printStudentReport(Person students[], int numE);
int main()
{
Person students[MAXSIZE];
int numE;
string fileName;
cout << "Enter the file name: ";
cin >> fileName;
numE = loadArray(fileName, students);
cout << endl;
cout << "Before Sort: " << endl;
printStudentReport(students, numE);
sortByName(students, numE);
cout << endl;
cout << "After Sort: " << endl;
printStudentReport(students, numE);
return 0;
}
int loadArray(string fileName, Person students[]){
ifstream in;
in.open(fileName.c_str());
if(in.fail())
return -1;
int n=0;
string fname, lname;
int month, day, year;
while(!in.eof()){
in>>fname>>lname>>month>>day>>year;
students[n] = Person(fname, lname, Date(month, day, year));
n++;
}
return n;
}
void sortByName(Person students[], int numE){
for(int i=0; i<numE; i++){
for(int j=i+1; j<numE; j++){
if(students[i].getLastName()>students[j].getLastName()){
Person temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
void printStudentReport(Person students[], int numE){
for(int i=0; i<numE; i++){
cout<<students[i].getLastName()<<", "<<students[i].getFirstName()<<"\t\t"<<students[i].getDateofBirth().toString()<<endl;
}
}
Person.h
#include<iostream>
#include<string>
using namespace std;
#include "Date.h"
#ifndef PERSON_H
#define PERSON_H
class Person{
private:
string firstName;
string lastName;
public:
Date dateOfBirth;
Person();
Person(string firstName, string lastName, Date dob);
string getFirstName();
string getLastName();
Date getDateofBirth();
void setFirstName(string fname);
void setLastName(string lname);
void setDateOfBirth(Date dob);
void setDateOfBirth(int month, int day, int year);
};
#endif
Person.cpp
#include "Person.h"
Person::Person(){
firstName = "";
lastName = "";
}
Person::Person(string firstName, string lastName, Date dob){
this->firstName = firstName;
this->lastName = lastName;
setDateOfBirth(dob.getMonth(), dob.getDay(), dob.getYear());
}
string Person::getFirstName(){
return firstName;
}
string Person::getLastName(){
return lastName;
}
Date Person::getDateofBirth(){
return dateOfBirth;
}
void Person::setFirstName(string fname){
firstName = fname;
}
void Person::setLastName(string lname){
lastName = lname;
}
void Person::setDateOfBirth(Date dob){
setDateOfBirth(dob.getMonth(), dob.getDay(), dob.getYear());
}
void Person::setDateOfBirth(int month, int day, int year){
dateOfBirth.setMonth(month);
dateOfBirth.setDay(day);
dateOfBirth.setYear(year);
}
Date.h
#include<iostream>
#include<string>
using namespace std;
#ifndef DATE_H
#define DATE_H
class Date{
private:
int day;
int month;
int year;
public:
Date();
Date(int month, int day, int year);
int getMonth();
int getDay();
int getYear();
void setMonth(int month);
void setDay(int day);
void setYear(int year);
string toString();
};
#endif
Date.cpp
#include "Date.h"
Date::Date(){
month = 1;
day = 1;
year = 1900;
}
Date::Date(int month, int day, int year){
this->month = month;
this->day = day;
this->year = year;
}
int Date::getMonth(){
return month;
}
int Date::getDay(){
return day;
}
int Date::getYear(){
return year;
}
void Date::setMonth(int month){
this->month = month;
}
void Date::setDay(int day){
this->day = day;
}
void Date::setYear(int year){
this->year = year;
}
string Date::toString(){
string ans = to_string(month)+"/";
ans += to_string(day)+"/";
ans += to_string(year);
return ans;
}
Kindly check the code output below.