Can it be better?
Nov 17, 2012 at 7:04pm UTC
Hi Everyone
Have been working on an assignment, it compiles, builds and runs fine. Just wanted to know if there is anything better I can do in the form of comments, lay-out, any unnecessary code, etc. Always trign to improve on the way I do things since some of the things I learn in class are not always the best way to do things. Thanks!!
Employee.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
// Preprocessor Directives
#pragma once
#include <string>
using namespace std;
//-----------------------------------------------------------------------------------------------------
// Define Employee Class
class Employee
{
public :
// Constructors and Destructor
Employee(void );
Employee(string first, string last, char gen, int dep, double salary);
~Employee(void );
// Methods to Access Attributes
double calculatePay();
void displayEmployee();
// Getters and Setters
string getFirstName();
void setFirstName(string);
string getLastName();
void setLastName(string);
char getGender();
void setGender(char );
int getDependents();
void setDependents(int );
void setDependents(string);
double getAnnualSalary();
void setAnnualSalary(double );
void setAnnualSalary(string);
static int getNumEmployees();
private :
// Declare Data Members
string firstName;
string lastName;
char gender;
int dependents;
static int numEmployees;
double annualSalary;
};
//-----------------------------------------------------------------------------------------------------
// Declare Prototypes
string getFirstName(string);
string getLastName(string);
char getGender(char );
int getDependents(int );
int getNumEmployee(int );
double getAnnualSalary(double );
void displayFirstName(string);
void displayLastName(string);
void displayGender(char );
void displayDependents(int );
void displayAnnualSalary(double );
void DislayApplicationInformation();
void DisplayDivider(string);
void DisplayDivider2(string);
string GetInput (string);
void TerminateApplication();
//-----------------------------------------------------------------------------------------------------
Employee.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include <iostream>
#include <iomanip>
//-----------------------------------------------------------------------------------------------------
// Declare Variable
int Employee::numEmployees = 0;
//-----------------------------------------------------------------------------------------------------
// Default Employee Constructor
Employee::Employee() : gender('U' ), dependents(0), annualSalary(20000.0), firstName("not given" ), lastName("not given" )
{
numEmployees++;
}
//-----------------------------------------------------------------------------------------------------
// Employee Deconstructor
Employee::~Employee()
{
numEmployees--;
}
//-----------------------------------------------------------------------------------------------------
// Multi-Arg Employee Constructor
Employee::Employee(string first, string last, char gen, int dep, double salary)
{
firstName = first;
lastName = last;
gender = gen;
dependents = dep;
annualSalary = salary;
numEmployees++;
}
//-----------------------------------------------------------------------------------------------------
// Define CalculatePay Function
double Employee::calculatePay()
{
return (annualSalary/52);
}
//-----------------------------------------------------------------------------------------------------
// DisplayEmployee Function
void Employee::displayEmployee()
{
cout << "Employee Name:\t\t" << firstName << " " << lastName << "\n" ;
cout << "Employee Gender:\t" << gender << "\n" ;
cout << "Employee Dependents:\t" << dependents << "\n" ;
cout << "Employee Annual Salary:\t$" <<
setprecision(2) << showpoint << fixed << annualSalary << "\n" ;
cout << "Employee Weekly Pay:\t$" << calculatePay() << "\n" ;
cout << "\n--- Number of Employee Objects Created ----" << "\n" ;
cout << "Number of employees: " << Employee::getNumEmployees() << "\n" ;
}
//-----------------------------------------------------------------------------------------------------
// Define GetFirstName and SetFirstName
string Employee::getFirstName()
{
return firstName;
}
void Employee::setFirstName(string newFirstName)
{
firstName = newFirstName;
}
string Employee::getLastName()
{
return lastName;
}
void Employee::setLastName(string newLastName)
{
lastName = newLastName;
}
//-----------------------------------------------------------------------------------------------------
// Define GetGender and SetGender
char Employee::getGender()
{
return gender;
}
void Employee::setGender(char newGender)
{
gender = newGender;
}
//-----------------------------------------------------------------------------------------------------
// Define GetDependents and SetDependents (overloaded)
int Employee::getDependents()
{
return dependents;
}
void Employee::setDependents(int newDependents)
{
dependents = newDependents;
}
void Employee::setDependents(string newDependents)
{
dependents = atoi(newDependents.c_str());
}
//----------------------------------------------------------------------------------------------------
// Define GetAnnualSalary and SetAnnualSalary (overloaded)
double Employee::getAnnualSalary()
{
return annualSalary;
}
void Employee::setAnnualSalary(double newAnnualSalary)
{
annualSalary = newAnnualSalary;
}
void Employee::setAnnualSalary(string newAnnualSalary)
{
annualSalary = atof(newAnnualSalary.c_str());
}
//----------------------------------------------------------------------------------------------------
// Define GetNumEmployees
int Employee::getNumEmployees()
{
return numEmployees;
}
Last edited on Nov 18, 2012 at 2:14pm UTC
Nov 17, 2012 at 7:04pm UTC
MainLab.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
// Preprocessor Directives
#include "stdafx.h"
#include "Employee.h"
#include <iostream>
#include <string>
using namespace std;
//-----------------------------------------------------------------------------------------------------
// Main Function
int main()
{
// Declare Function Variables
string tempString;
char gender;
int dependents = 0;
double annualSalary;
// Call DisplayApplicationInformation and DisplayDivider Proceedures
DislayApplicationInformation();
DisplayDivider("Employee 1" );
// Construct Employee1 Object
Employee employee1;
//Getting Employee1 Data
string firstName = GetInput("First Name" );
employee1.setFirstName(firstName);
string lastName = GetInput("Last Name" );
employee1.setLastName(lastName);
tempString = GetInput("Gender" );
gender = tempString.c_str()[0];
employee1.setGender(gender);
tempString = GetInput("Number of Dependents" );
dependents = atoi(tempString.c_str());
employee1.setDependents(dependents);
tempString = GetInput("Annual Salary" );
annualSalary = atof(tempString.c_str());
employee1.setAnnualSalary(annualSalary);
// Calling DisplayDivider2 and DisplayEmployee Proceedures
DisplayDivider2("Employee Information" );
employee1.displayEmployee(); //Display Employee1 Data
cout << endl;
// Construct Employee2 Object
Employee employee2("Mary" , "Noia" , 'F' , 5, 24000.0);
// Calling DisplayDivider, DisplayDivider2 and DisplayEmployee Proceedures
DisplayDivider("Employee 2" );
DisplayDivider2("Employee Information" );
employee2.displayEmployee(); // Display Employee2 Data
// Call TerminateApplication Proceedure
TerminateApplication();
// Pause System
system("pause" );
return 0;
}
//-----------------------------------------------------------------------------------------------------
// DisplayApplicationInformation Procedure
void DislayApplicationInformation()
{
// Display Program Header
cout << "Welcome to your first Object Oriented Program" << "\n" ;
cout << "Employee Class CIS247C, Week 2 Lab" << "\n" ;
cout << "Name: Jim Stevens" << "\n" ;
}
//-----------------------------------------------------------------------------------------------------
// DisplayDivider Prodedure
void DisplayDivider(string outputTitle)
{
// Display Divider with Output Title
cout << "\n***************************" << outputTitle <<"***************************" << "\n" ;
}
//-----------------------------------------------------------------------------------------------------
// DisplayDivider2 Prodedure
void DisplayDivider2(string outputTitle)
{
// Display Divider with Output Title
cout << "\n" << outputTitle << endl;
cout << "\n________________________________________________________________" << "\n" ;
}
//-----------------------------------------------------------------------------------------------------
// GetInput Function
string GetInput (string inputType)
{
// Declare Function Varaible
string input;
// Prompt User for Input
cout<<"Please enter your " << inputType <<": " ;
getline(cin, input);
return input;
}
//-----------------------------------------------------------------------------------------------------
// TerminateApplication Procedure
void TerminateApplication()
{
// Display Termination Message
cout << "\nThank you for using the Employee Class program\n" ;
}
//-----------------------------------------------------------------------------------------------------
Last edited on Nov 18, 2012 at 2:16pm UTC
Topic archived. No new replies allowed.