Program 4

Creating the Project

First create an Easy Windows project in a directory Exam4. Next, move the file rcpointe.h into that directory.

The Header File

The header file looks much like the header in program 3 with the exception of the fact we have included rcpointe.h, replaced all non constructors virtual functions, made Employee an abstract class, and added the type definitions for pointers to objects of each class:

#ifndef EMPLOY_H
#define EMPLOY_H
#include 
#include "rcpointe.h"
class istream; //this will be defined by include when needed
class ostream;

class Employee 
{
public:
	enum {HOURLY, FLATRATE};
// There is no object of type Employee, so the funtions are dummies
	virtual void write (ostream&)=0;
	virtual void read(istream&)=0;
	virtual void print()=0;
};
typedef RcPointer < Employee > EmployeePtr;

class HourlyEmployee :public Employee
{
public:
	HourlyEmployee();
	HourlyEmployee(const string &,int);
	virtual void write (ostream&);
	virtual void read(istream&);
	virtual void print();
private:
	string _name;
	int _hours;
};

typedef RcPointer < HourlyEmployee > HourlyPtr;

class FlatRateEmployee :public Employee
{
public:
	FlatRateEmployee();
	FlatRateEmployee(const string &);
	virtual void write (ostream&);
	virtual void read(istream&);
	virtual void print();
private:
	string _name;
};

typedef RcPointer < FlatRateEmployee > FlatRatePtr;
#endif

Code Changes

You have already seen the major code change that occurs in run when you read the file. If we look at the old version of run that created the file you will notice there aren't many lines of code that call on virtual functions (only the two writes):

{
	ofstream out("exam3.dat");
	int size;
	cout<<"How many employees do you want to enter? >";
	cin >> size;
	out << size << endl;//file begins with the size of the file
	for ( int i = 0; i < size; i++)
	{
		cout << "Type of Employee (1 for hourly, 2 for flat rate > ";
		int type;
		cin >> type;
		cout << "Name >";
		string name;
		name.read_line(cin);
		if (type==1)
		{
			int hours;
			cout << "Hours worked >";
			cin >> hours;
			HourlyEmployee e(name,hours);
			e.write(out);
		}
		else
		{
			FlatRateEmployee e(name);
			e.write(out);
		}
	}
}

For a complete copy of this project click here.

Back to Week 3.