Practice #7

Download code here

Do it yourself

  1. Implement a member function named IsYounger in class Person that takes a Person object (obj) and returns true if this object was born before obj

    bool Person::IsYounger (const Person& obj) const;

  2. Implement a member function named ChangeAddress in class Employee that changes the value of the employee address

    void Employee::ChangeAddress (string add);

  3. Implement a function (NOT a member function) named SmithFamilyAddress that takes an array of Persons and displays the addresses of those whose last name is "smith"

    void SmithFamilyAddress (const Person persons[], const int size);

[Date.h]

	#ifndef DATE_H
	#define DATE_H

	#include <iostream>
	using namespace std;

	class Date
	{
	public:
		Date (int d, int m, int y)
			: day (d), month (m), year (y)
		{}

		void GetDate (int& d, int& m, int& y) const
		{
			d = day; m = month; y = year;
		}

		void SetDate (int d, int m, int y)
		{
			day = d; month = m; year = y;
		}

		void Display () const
		{
			cout<< "Date of Birth : "<< day<< "/"<< month<< "/"<< year<< endl;
		}

	private:
		int day, month, year;

	};

	#endif
		

[Name.h]

	#ifndef NAME_H
	#define NAME_H

	#include <iostream>
	#include <string>
	using namespace std;

	class Name
	{
	public:
		Name (string f, string l)
			: first (f), last (l)
		{}

		void GetName (string& f, string& l) const
		{
			f = first;
			l = last;
		}

		void SetName (string f, string l)
		{
			first = f;
			last = l;
		}

		void Display () const
		{
			cout<< "Name : "<< first<< " "<< last<< endl;
		}

	private:
		string first, last;
	};

	#endif
		

[Person.h]

	#ifndef PERSON_H
	#define PERSON_H

	#include <iostream>
	#include <string>
	using namespace std;

	#include "Name.h"
	#include "Date.h"

	class Person
	{
	public:
		Person (string f, string l, int d, int m, int y, string add)
			: name (f, l), DOB (d, m ,y), address (add)
		{}

		Person (Name n, Date dob, string add)
			: name (n), DOB (dob), address (add)
		{}

		Name GetName () const
		{
			return name;
		}

		Date GetDOB () const
		{
			return DOB;
		}

		string GetAddress () const
		{
			return address;
		}

		void SetName (Name n)
		{
			name = n;
		}

		void SetDOB (Date dob)
		{
			DOB = dob;
		}

		void SetAddress (string add)
		{
			address = add;
		}
		
		void DisplayInfo () const
		{
			name.Display ();
			DOB.Display ();
			cout<< "Address : "<< address<< endl;
		}

		void ClassName () const
		{
			cout<< "class Person\n";
		}

		void Doo () const
		{
			cout<< "Function Doo () in class Person\n";
		}
		

	private:
		Name name;
		Date DOB;
		string address;
	};

	#endif
		

[Employee.h]

	#ifndef EMPLOYEE_H
	#define EMPLOYEE_H

	#include <iostream>
	using namespace std;

	#include "Person.h"

	class Employee : public Person
	{
	public:
		Employee (string f, string l, int d, int m, int y, string add, int id)
			: Person (f, l, d, m ,y, add), empID (id)
		{}

		Employee (Name n, Date dob, string add, int id)
			: Person (n, dob, add), empID (id)
		{}
		
		// DisplayInfo () overrides Person::DisplayInfo ()
		void DisplayInfo () const
		{
			Person::DisplayInfo ();
			cout<< "Employee ID : "<< empID<< endl;
		}

		// ClassName () overrides Person::ClassName ()
		void ClassName () const
		{
			cout<< "class Employee\n";
		}
		

	private:
		int empID;
	};

	#endif
		

[main.cpp]
Test the functionalities of your classes

Write a function named BornInNovember that takes an array of employees and displays first and last names of those who were born in November.

	#include <iostream>
	using namespace std;

	#include "Name.h"
	#include "Date.h"
	#include "Person.h"
	#include "Employee.h"

	void BornInNovember (const Employee [], const int);

	void main ()
	{
		Employee John ("John", "Smith", 5, 11, 1994, "Amman - Jordan", 10020);
		John.DisplayInfo ();
		cout<< endl;
		John.ClassName ();
		cout<< endl;
		John.Doo ();
		cout<< "\n\n----------------------------------------\n\n";

		Person Jack ("Jack", "Bowers", 19, 5, 1990, "Amman - Jordan");
		Jack.DisplayInfo ();
		cout<< endl;
		Jack.ClassName ();
		cout<< endl;
		Jack.Doo ();
		cout<< "\n\n----------------------------------------\n\n";

		Employee Sarah ("Sarah", "Edwards", 20, 11, 1995, "Amman - Jordan", 10021);
		Employee David ("David", "Clarson", 7, 1, 1993, "Ohio - USA", 10021);

		const int size = 3;
		Employee emps[] = {John, Sarah, David};

		cout<< "\n\n----------------------------------------\n";
		cout<< "Employees Born in November\n";
		BornInNovember (emps, size);
	}

	
	// display first and last names of employees who were born in November
	void BornInNovember (const Employee emps[], const int size)
	{
		int d, m, y;
		for (int i = 0; i < size; ++i) {
			emps[i].GetDOB ().GetDate (d, m ,y);
			if (m == 11) // month == November
				emps[i].GetName ().Display ();
		}
	}