So this is a little example of some beginner C++ fundamentals. This will go over Classes, Nesting, Casting, Enums, Low Lever Error Checking, Sending Values to a functions and receiving Values from a functions. This is just a simple Console Application, there for no GUI. If anyone takes an interest I will explain the code in more detail if needed.
This program was to replicate a business program at the same time cover the key OOP parts of C++. The code takes the users input for multiple things like date of higher and salary. A simple algorithm was used to determine if its a leap year.
Over all its a good start into OOP and C++. More to come b/c I am helping a friend code out this stuff for his class. Please ask question or post requests!
Code:
#include <iostream>
#include <stdio.h>
using namespace std;
enum Category{manager, laborer, secretary};
class Date
{
private:
int year, month, day;
public:
void Set()
{
cout << "Enter In The Year : "; cin >> year;
cout << "Enter In The Month : "; cin >> month;
cout << "Enter In The Day : "; cin >> day;
if ( !! Validate())
{
cout << "\n" << endl;
Set();
}
};
void Set(int yearx, int monthx, int dayx)
{
year = yearx;
month = monthx;
day = dayx;
if ( !! Validate())
{
cout << "\n" << endl;
Set();
}
};
void Display()
{
cout << year << ":" << month << ":" << day << endl;
};
bool Validate()
{
int days_in_month[] = {31, 28 + (year % 4 == 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (year >= 1900)
{
if (month >= 1 && month <= 12)
{
if (day >= 1 && day <= days_in_month[month - 1])
{
return false;
}
else
{
cout << "[ERROR] Day range ( 1 to " << days_in_month[month - 1] << " )"<< endl;
return true;
}
}
else
{
cout << "[ERROR] Month range ( 1 to 12 )" << endl;
return true;
}
}
else
{
cout << "[ERROR] Year is to small (1900)" << endl;
return true;
}
};
};
class Employee
{
private:
int e_number, e_job, e_dateofhire;
double e_salary;
Date dl;
public:
void Set()
{
cout << "Enter In The Employee's Number : "; cin >> e_number;
cout << "Enter In The Employee's Salary : $"; cin >> e_salary;
cout << "[0] Manager, [1] Laborer, [2] Secretary" << endl;
cout << "Enter In The Employee's Job Category : "; cin >> e_job;
if ( !! Validate())
{
cout << "\n" << endl;
Set();
}
dl.Set();
}
void Set(int e_numberx, double e_salaryx, int e_jobx, Date dlx)
{
e_number = e_numberx;
e_salary = e_salaryx;
e_job = e_jobx;
dl = dlx;
if ( !! Validate())
{
cout << "\n" << endl;
Set();
}
};
void Display()
{
cout << "\n" << endl;
cout << "Employee's Number : " << e_number << endl;
cout << "Employee's Salary : $" << e_salary << endl;
cout << "Employee's Job Category : ";
switch (e_job)
{
case manager : cout << "Manager" << endl; break;
case laborer : cout << "Laborer" << endl; break;
case secretary : cout << "Secretary" << endl; break;
}
cout << "Date of employed "; dl.Display();
cout << "" << endl;
}
bool Validate()
{
if (e_number > 0 && e_number < 100000000)
{
if (e_salary > 0 && e_salary < 1000000)
{
if (e_job > -1 && e_job < 3)
{
return false;
}
else
{
cout << "Employee's Category 0 to 2)" << endl;
return true;
}
}
else
{
cout << "Employee's Salary 1 to 999999)" << endl;
return true;
}
}
else
{
cout << "Employee's Number 1 to 999999999)" << endl;
return true;
}
}
};//End of Employe
void main()
{
Employee dl, dl2;
Date dx;
dx.Set(2004, 2, 29);
dl.Set(1023, 8555, 0, dx);
dl.Display();
dl2.Set();
dl2.Display();
}
Just rename the file to Mmowned Lab1.cpp