Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
/*
Name: Exercise 6
Author: Steven Waters <stevenwaters@mail.usf.edu>
Date: 12-08-14
Features: The program will prompt the user to create records for 3 employees. Each record consist of the following values:
- First Name
- Last Name
- Gender
- Social Security Number
- Payroll
- Position
Furthemore, when these values are extracted from user input, they are passed to the Employee constructor as arguments.
The Employee constructor then passes the first name, last name, gender, and the social security number to the Person consturctor
which then passes the first and last name values to the Name constructor. Each of the classes for these constructors allows the
worker object to print out these values by invoking the info() method.
Bugs: None at the moment.
*/
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
// Global variables that can be used in the setw() I/O manipulator
const int width1 = 35;
const int width2 = 40;
// Prints out the characters for a Top Bar
void topBar() {
cout << endl;
cout << "-----------------------------------------------------------------" << endl;
cout << endl;
};
// Prints out the characters mainHeader
void mainHeader(){
cout << endl;
cout << "=================================================================" << endl;
cout << " Employee Logger v0.5 " << endl;
cout << "=================================================================" << endl;
cout << endl;
};
// Name Class
// Defines pointers for first and last name values
// Defines the default constructor for this class
// Assigns pointers to the pointers that were passed from the employee object.
// Defines a function for outputting the first and last name values
// that were inputted by the user
class Name {
protected:
// Declaring pointers for the first and last name that was past to this class
char *first, *last;
public:
// Default contructor for the Name class
Name() {};
// Defining the default constructor for the Name Class
// Allows a derived class to pass values for the first and last name to
// the class where it's defined
Name (char *f, char *l) {
// Assigning f to first, f was passed from the Person Class
first = f;
// Assigning l to last , l was passed from the Person class
last = l;
};
// Displays the formatted first and last name records for each Employee object
// This is the first info() function invoked after the user completes an employee record.
// Uses the setw() and left I/O manipulators to format the output.
virtual void info() {
// Prints out a fancy looking top bar above each print out for the Employee record
topBar();
// Outputs the first name for each Employee record
cout << left << setw(width1) << "The first name is: " << setw(width2) << first << endl;
// Outputs the last name for each Employee record
cout << left << setw(width1) << "The last name is: " << setw(width2) << last << endl;
};
}; // End Name Class
// Person Class - A derivative of the Name Class
// Defines variables for holding gender and social security number values
// Calls the default constructor for the Name class and uses it to pass
// the first and last name values that were inputted to the Name class.
// Defines a function for outputting the gender and social security number values
// that were inputted by the user
class Person: public Name {
// Declaring the gender variable
char gender;
// Declaring the ssn variable for the social security number input
int ssn;
public:
// Default contructor for the Person class
Person() {};
// Accepts arguments first name, last name, gender, and social security
// number values for each Employee object.
// Calls the Name constructor so it can pass the first and
// last name values to the Name class.
Person(char *f, char *l, char g, int s): Name::Name(f, l) {
// Assigning g to gender, g was passed from the Employee class
gender = g;
// Assinigng s to ssn, s was passed from the Employee class
ssn = s;
};
// Displays the formatted gender and social security number records for each Employee object
// This is the second info() function invoked after the user completes an employee record.
// Uses the setw() and left I/O manipulators to format the output.
virtual void info() {
// Displays the output defined in the info() method for the Name class
Name::info();
// Outputs the gender for each Employee record
cout << left << setw(width1) << "The gender is: " << setw(width2) << gender << endl;
// Outputs the social security number for each Employee record
cout << left << setw(width1) << "The social security number is: " << setw(width2) << ssn << endl;
};
}; // End Person Class
// Employee Class - A derivative of the Person Class
// Defines variable for the employees payroll information
// and a pointer for the employee's position information
// Gets all of the input values that were entered in by the user
// via the Employee consturctor
class Employee: public Person {
protected:
// Declaring payroll variable for holding the payroll value
int payroll;
// Declaring the position variable for the holding the position value
char *position;
public:
// Default contructor for the Employee class
Employee() {};
// Get's extracted value for all of the user's input
// Accepts arguments first name, last name, gender, and social security
// number, payroll, and position values for each Employee object.
// Calls the Person constructor so it can pass the first name, last name, gender,
// and social security number values to the Person class.
Employee(char *f, char *l, char g, int s, int p, char *pos) : Employee::Person(f,l,g,s) {
// Assigning p to payroll, the value for p comes each Worker object that is created
payroll = p;
// Assigning pos to position, the value for pos comes each Worker object that is created
position = pos;
};
// Displays the formatted paywall and position records for each Employee object
// This is the third info() function invoked after the user completes an employee record.
// Uses the setw() and left I/O manipulators to format the output.
virtual void info() {
// Displays the output defined in the info() method for the Person class
Person::info();
// Outputs the payroll for each Employee record
cout << left << setw(width1) << "The payroll is: " << "$" << setw(width2) << payroll << endl;
// Outputs the position for each Employee record
cout << left << setw(width1) << "The position is: " << setw(width2) << position << endl;
// Prints out a fancy looking top bar below each print out for the Employee record
topBar();
};
}; // End Employee Class
int main() {
// Defining char arrays for user input
// fn is for first name, ln is for last name, pos is for position
char fn[20], ln[20], pos[20];
// Defining char variable for inputted gender value
char gen;
// Defining int variables for inputted social security number and payroll values
int s, pay;
// Creating an instance of the name
Name *worker;
// Prompts the user 3 times to enter in the first name , last name, gender,
// social security number, and payroll information for each Employee
for (int i=0; i<3; i++) {
// Prints out the mainHeader when the program loads
// or during the first pass
if (i<1) {
// Calls the mainHeader() function that was defined up top
mainHeader();
}
// Prompts the user to enter in the Employee's first name
cout << "Input the first name: " << endl;
// Extracts the first name input
cin >> fn;
// Adds vertical space
cout << endl;
// Prompts the user to enter in the Employee's last name
cout << "Input the last name: " << endl;
// Extracts the last name input
cin >> ln;
// Adds vertical space
cout << endl;
// Prompts the user to enter in the Employee's gender
// Should be M or F , since it should be Char value
cout << "Input the gender: " << endl;
// Extracts the gender input
cin >> gen;
// Adds vertical space
cout << endl;
// Prompts the user to enter in the Employee's Social
// Security Number
cout << "Input the social security number: " << endl;
// Extracts the social security number input
cin >> s;
// Adds vertical space
cout << endl;
// Prompts the user to enter in the Employee's payroll
cout << "Input the payroll: " << endl;
// Extracts the payroll input
cin >> pay;
// Adds vertical space
cout << endl;
// Prompts the user to enter in the Employee's position
cout << "Input the position: " << endl;
// Extracts the position input
cin >> pos;
// Create new object using the Employee constructor
worker = new Employee(fn, ln, gen, s, pay, pos);
// Invokes the info() function for Employee , which subsequently calls the
// info() functions for the Person and Name classes
worker->info();
};
system("pause");
return 0;
} // End int main() function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment