So, I've only been learning to program for the past couple weeks. I've been reading Sams Teach Yourself C++ in 21 Days while supplementing it with Object Oriented Programming in C++.
I'm finishing up the chapter on objects and classes and am doing an exercise. This is going to be VERY basic code and will compile. I'm just wondering if my coding is "intelligent" and organized making it easy to set and display values of multiple classes. Remember I'm VERY new and this code was all from scratch from the following question:
"Extend the employee class of Exercise 4 to include a date class and an etype enum. An object of the date class should be used to hold the date of first employment; that is, the date when the employee was hired. The etype variable should hold the employees type: (I used different ones). These two items will be private member data in the employee definition, just like the employee number and salary. You'll need to extend the getemploy() and putemploy() functions to obtain this new information from the user and display it. These functions will probably need switch statements to handle the etype variable. Write a main() program that allows the user to enter data for three employee variables and then displays this data."
// Exercise 6
#include <iostream>
using namespace std;
enum etype {manager, laborer, executive, driver // define the types of employees
};
class date // date the employee was hired
{
private:
int year, month, day;
public:
date():year(0), month(0), day(0) // set default to 0,0,0 (no real purpose here but looks good)
{}
void getDate(int y, int m, int d); // get and set the private variables
void showDate() const; // show the date of employment (Can't modify anything)
};
void date::getDate(int y, int m, int d)
{
year = y; month = m; day = d;
}
void date::showDate() const
{
cout << month << '/' << day << '/' << year;
}
class employee // Employee class
{
private:
etype position; // store position
int number;
float wage;
date eDate; // store date for employee as an object of date
public:
void getStat(int n, float w, etype type, int y, int m, int d); // get all the stats in one call (simplicity?)
void showEmp() const; // show all the stats in one call
};
void employee::getStat(int n, float w, etype type, int y, int m, int d)
{
number = n; wage = w; position = type;
eDate.getDate(y, m, d); // call the date class to store the dates
}
void employee::showEmp() const
{
cout << " position is: "; // figure out the position of the employee and display
switch (position)
{
case manager: cout << "manager"; break;
case laborer: cout << "laborer"; break;
case executive: cout << "executive"; break;
case driver: cout << "driver"; break;
}
cout << "\nEmployee number is: " << number;
cout << "\nEmployee wage is: " << wage;
cout << "\nEmployee start date is: "; eDate.showDate(); // call the member function of date to show employee date
}
int main()
{
employee John, Joe, Jill; // 3 employees
int number, year, month, day;
float wage;
etype type;
int temptype; // to convert user input to enum etype
char temp = ' '; // for the '/' input from user
cout << "Please enter the following details about John - ";
cout << "\nPosition (0)manager, (1)laborer, (2)executive, (3)driver: ";
cin >> temptype;
switch(temptype) // turn the input into an enum etype
{
case 0: type = manager;break;
case 1: type = laborer; break;
case 2: type = executive;break;
case 3: type = driver;break;
}
cout << "\nEmployee number: "; cin >> number;
cout << "\nDate of employment (mm/dd/yyyy) format: "; cin >> month >> temp >> day >> temp >> year;
cout << "\nWage: "; cin >> wage;
John.getStat(number, wage, type, year, month, day);
cout << "Please enter the following details about Joe - ";
cout << "\nPosition (0)manager, (1)laborer, (2)executive, (3)driver: "; cin >> temptype;
switch(temptype)
{
case 0: type = manager;break;
case 1: type = laborer; break;
case 2: type = executive;break;
case 3: type = driver;break;
}
cout << "\nEmployee number: "; cin >> number;
cout << "\nDate of employment (mm/dd/yyyy) format: "; cin >> month >> temp >> day >> temp >> year;
cout << "\nWage: "; cin >> wage;
Joe.getStat(number, wage, type, year, month, day);
cout << "Please enter the following details about Jill - ";
cout << "\nPosition (0)manager, (1)laborer, (2)executive, (3)driver: "; cin >> temptype;
switch(temptype)
{
case 0: type = manager;break;
case 1: type = laborer; break;
case 2: type = executive;break;
case 3: type = driver;break;
}
cout << "\nEmployee number: "; cin >> number;
cout << "\nDate of employment (mm/dd/yyyy) format: "; cin >> month >> temp >> day >> temp >> year;
cout << "\nWage: "; cin >> wage;
Jill.getStat(number,wage,type,year,month,day);
cout << "Johns stats: ";
John.showEmp() ;
cout << endl;
cout << "Joe's stats: ";
Joe.showEmp() ;
cout << endl;
cout << "Jill's stats: ";
Jill.showEmp() ;
cout << endl;
return 0;
}
Any advice on the code I wrote? Things to pay attention to or things I over complicated?
I hope to use your brains to help me learn properly and not get into bad habbits! Thanks in advance.