.Create a class called myDate with month, date, and year fields2.Overload the >>
ID: 3754309 • Letter: #
Question
.Create a class called myDate with month, date, and year fields2.Overload the >> operator for the class to read a date in the format mm/dd/yyyya.E.g., 09/25/2017 should read 9 in the field month, 25 in the field date and 2017 in the field year3.
Create a class called Student with fields name and birthday. Name should be of type string and birthday should be of type myDate. 4.Overload >, <, <=, >= to compare two objects of type myDate5.
From a file called birthdays.txt, read a list of student birthdays in the format: mm/dd/yyyyAlicemm/dd/yyyyBob6.Print the oldest and youngest students’ name and birthdays7.
Need help ASAP
Explanation / Answer
// File name: myDate.h
#ifndef myDate_H
#define myDate_H
// Class myDate definition
class myDate
{
// Data member to store date information
int day;
int month;
int year;
public:
// Prototype of member function
myDate();
void setDay(int d);
void setMonth(int m);
void setYear(int y);
int getDay();
int getMonth();
int getYear();
void display();
};// End of class myDate
#endif
--------------------------------------------------------------------------------------------
// File name: myDate.cpp
#include "myDate.h"
#include <iostream>
using namespace std;
// Default constructor definition to initialize data member to zero
myDate::myDate()
{
day = month = year = 0;
}// End of default constructor
// Function to set date
void myDate::setDay(int d)
{
day = d;
}// End of function
// Function to set month
void myDate::setMonth(int m)
{
month = m;
}// End of function
// Function to set year
void myDate::setYear(int y)
{
year = y;
}// End of function
// Function to return day
int myDate::getDay()
{
return day;
}// End of function
// Function to return month
int myDate::getMonth()
{
return month;
}// End of function
// Function to return year
int myDate::getYear()
{
return year;
}// End of function
// Function to display dates information
void myDate::display()
{
// Displays each date information
cout<<month<<"/"<<day<<"/"<<year;
}// End of function
-----------------------------------------------------------------------------------------
// File name: student.h
#ifndef student_H
#define student_H
#include "myDate.h"
#include <iostream>
using namespace std;
// Defines class student
class Student
{
// Data member to store data
string name;
myDate dob;
public:
Student();
friend istream&operator >>(istream &fRead, Student &stu);
friend ostream&operator <<(ostream &out, Student &stu);
friend bool operator == (Student one, Student two);
friend bool operator > (Student one, Student two);
friend bool operator >= (Student one, Student two);
friend bool operator <= (Student one, Student two);
friend bool operator < (Student one, Student two);
};// End of class Student
#endif
---------------------------------------------------------------------------------------------
// File name: student.cpp
#include "student.h"
#include <iostream>
using namespace std;
// Default constructor definition
Student::Student()
{
name = "";
}// End of default constructor
// Overloading operator >> to read student and dob information and return it
istream&operator >>(istream &fRead, Student &stu)
{
// To store date, month, and year read from file
int d, m, y;
// To store separator '/'
char symbol;
// Extracts month from file
fRead>>m;
// Extracts separator symbol from file
fRead>>symbol;
// Calls the function to set month
stu.dob.setMonth(m);
// Extracts day from file
fRead>>d;
// Extracts separator symbol from file
fRead>>symbol;
// Calls the function to set date
stu.dob.setDay(d);
// Extracts year from file
fRead>>y;
// Calls the function to set year
stu.dob.setYear(y);
// Extracts name from the file
fRead>>stu.name;
// Returns the record object
return fRead;
}// End of function
// Overloading << operator to display student and dob information
ostream&operator <<(ostream &out, Student &stu)
{
out<<" Name: "<<stu.name<<" DOB: ";
stu.dob.display();
return out;
}// End of function
// Overloading << operator to check two objects are equal or not
bool operator == (Student one, Student two)
{
// Checks first parameter object year, month and day is equals to
// second object parameter year, month and day then returns true
if(one.dob.getYear() == two.dob.getYear() && one.dob.getMonth() == two.dob.getMonth() && one.dob.getDay() == two.dob.getDay())
return true;
// Otherwise return false
else
return false;
}// End of function
// Overloading << operator to check greater out of two object
bool operator > (Student one, Student two)
{
// Checks if first parameter object year is greater than
// second object parameter year then returns true
if(one.dob.getYear() > two.dob.getYear())
return true;
// Otherwise checks if first parameter object year is equals to
// second object parameter year then returns true
else if(one.dob.getYear() == two.dob.getYear())
{
// Checks if first parameter object month is greater than
// second object parameter month then returns true
if(one.dob.getMonth() > two.dob.getMonth())
return true;
// Otherwise checks if first parameter object month is equals to
// second object parameter month then returns true
else if(one.dob.getMonth() == two.dob.getMonth())
{
// Checks if first parameter object day is greater than
// second object parameter year then returns true
if(one.dob.getDay() > two.dob.getDay())
return true;
}// End of inner else if condition
}// End of outer else if condition
// Otherwise return false
return false;
}// End of function
// Overloading << operator to check greater then or equals out of two object
bool operator >= (Student one, Student two)
{
// Checks first parameter object year is greater than or equals
// second object parameter year then returns true
if(one.dob.getYear() >= two.dob.getYear())
return true;
// Otherwise checks if first parameter object year is equals to
// second object parameter year then returns true
else if(one.dob.getYear() == two.dob.getYear())
{
// Checks if first parameter object month is greater than or equals to
// second object parameter month then returns true
if(one.dob.getMonth() >= two.dob.getMonth())
return true;
// Otherwise checks if first parameter object month is equals to
// second object parameter month then returns true
else if(one.dob.getMonth() == two.dob.getMonth())
{
// Checks if first parameter object day is greater than or equals to
// second object parameter year then returns true
if(one.dob.getDay() >= two.dob.getDay())
return true;
}// End of inner else if condition
}// End of outer else if condition
// Otherwise return false
return false;
}// End of function
// Overloading << operator to check less than or equals out of two object
bool operator <= (Student one, Student two)
{
// Checks first parameter object year is less than or equals to
// second object parameter year then returns true
if(one.dob.getYear() <= two.dob.getYear())
return true;
// Otherwise checks if first parameter object year is equals to
// second object parameter year then returns true
else if(one.dob.getYear() == two.dob.getYear())
{
// Checks if first parameter object month is less than or equals to
// second object parameter month then returns true
if(one.dob.getMonth() <= two.dob.getMonth())
return true;
// Otherwise checks if first parameter object month is equals to
// second object parameter month then returns true
else if(one.dob.getMonth() == two.dob.getMonth())
{
// Checks if first parameter object day is less than or equals to
// second object parameter year then returns true
if(one.dob.getDay() <= two.dob.getDay())
return true;
}// End of inner else if condition
}// End of outer else if condition
// Otherwise return false
return false;
}// End of function
// Overloading << operator to check small out of two object
bool operator < (Student one, Student two)
{
// Checks first parameter object year is less than
// second object parameter year then returns true
if(one.dob.getYear() < two.dob.getYear())
return true;
// Otherwise checks if first parameter object year is equals to
// second object parameter year then returns true
else if(one.dob.getYear() == two.dob.getYear())
{
// Checks if first parameter object month is less than
// second object parameter month then returns true
if(one.dob.getMonth() < two.dob.getMonth())
return true;
// Otherwise checks if first parameter object month is equals to
// second object parameter month then returns true
else if(one.dob.getMonth() == two.dob.getMonth())
{
// Checks if first parameter object day is less than
// second object parameter year then returns true
if(one.dob.getDay() < two.dob.getDay())
return true;
}// End of inner else if condition
}// End of outer else if condition
// Otherwise return false
return false;
}// End of function
------------------------------------------------------------------------------------------------
// File name: mainStudentDate.cpp
#include "student.cpp"
#include "myDate.cpp"
#include <iostream>
#include <fstream>
#include <stdlib.h>
// For maximum number of dates
#define MAX 75
using namespace std;
// main function definition
int main()
{
// ifstream object declared
ifstream fRead;
// Opens the file for reading
fRead.open("BirthDay.txt");
// Checks if the file unable to open for reading display's error message with file name
if(!fRead)
{
cout<<" ERROR: Unable to open the file for reading.";
exit(0);
}// End of if condition
// Declares an array of object of class Student of size MAX
Student student[MAX];
// Sets the record length to zero
int len = 0;
// Loops till end of the file
while(!fRead.eof())
{
// Reads student data from file using operator overloading >>
// and stores it at len index position
fRead>>student[len];
// Displays each student information at index len using operator overloading <<
cout<<student[len];
// Increase the record counter by one
len++;
}// End of while loop
// Displays the students having equal DOB
cout<<" Equal date of birth students: ";
// Loops till number of records
for(int x = 0; x < len; x++)
{
// Loops from outer loop value plus one to number of records minus one
for(int y = x+1; y < len-1; y++)
// Checks if student at x index position is equals to student at y index position
if(student[x] == student[y])
// Display both student information
cout<<student[x]<<" and "<<student[y];
}// End of for loop
// Stores the first student as youngest and oldest
Student youngest = student[0], oldest = student[0];
// Displays the youngest student based on DOB
cout<<" Youngest student: ";
// Loops till number of records starting from second student
for(int x = 1; x < len; x++)
{
// Checks if the student at x index position is greater than the earlier youngest student
if(student[x] > youngest)
// Update the youngest student by assigning the student at x index position
youngest = student[x];
}// End of for loop
cout<<youngest;
// Displays the oldest student based on DOB
cout<<" Oldest student: ";
// Loops till number of records starting from second student
for(int x = 1; x < len; x++)
{
// Checks if the student at x index position is less than the earlier oldest student
if(student[x] < oldest)
// Update the oldest student by assigning the student at x index position
oldest = student[x];
}// End of for loop
cout<<oldest;
}// End of main function
Sample Output:
Name: Rakesh DOB: 9/25/2017
Name: Mohan DOB: 12/11/2015
Name: Sunita DOB: 9/28/2017
Name: Pyari DOB: 3/27/1978
Name: Anita DOB: 12/11/2017
Name: Promod DOB: 12/11/2015
Name: Sasmita DOB: 12/11/2015
Equal date of birth students:
Name: Mohan DOB: 12/11/2015
and
Name: Promod DOB: 12/11/2015
Youngest student:
Name: Anita DOB: 12/11/2017
Oldest student:
Name: Pyari DOB: 3/27/1978
BirthDay.txt file contents
09/25/2017 Rakesh
12/11/2015 Mohan
09/28/2017 Sunita
03/27/1978 Pyari
12/11/2017 Anita
12/11/2015 Promod
12/11/2015 Sasmita
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.