I have wrote a simple employee test program to learn how to create and initialize class objects upon user's choice.
What I want:
- User enters the number of employees needed
- User enters each employee's first name, last name, date of birth, hire date
So,
Here is what I did so far:
date.h
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date(int = 1, int = 1, int = 1990);
~Date();
void setDate(int, int, int);
void setDay(int);
void setMonth(int);
void setYear(int);
int getDay() const;
int getMonth() const;
int getYear() const;
int checkDay(int) const;
int checkMonth(int) const;
void printDate() const;
private:
int day;
int month;
int year;
};
#endif
date.cpp
#include <iostream>
#include "date.h"
using namespace std;
Date::Date(int d, int m, int y)
{
setDate(d, m, y);
}
void Date::setDate(int d, int m, int y)
{
setDay(d);
setMonth(m);
setYear(y);
}
void Date::setDay(int d)
{
day = checkDay(d);
}
void Date::setMonth(int m)
{
month = checkMonth(m);
}
void Date::setYear(int y)
{
year = y;
}
int Date::getDay() const
{
return day;
}
int Date::getMonth() const
{
return month;
}
int Date::getYear() const
{
return year;
}
int Date::checkDay(int d) const
{
const int daysPerMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (d > 0 && d <= daysPerMonth[getMonth()])
{
return d;
}
else if (getMonth() == 2 && d == 29 && (getYear() % 400 == 0 || (getYear() % 4 == 0 && getYear() % 100 != 0)))
{
return d;
}
else
{
cout << "Day " << d << " is invalid. Set to 1." << endl;
d = 1;
return d;
}
}
int Date::checkMonth(int m) const
{
if (m > 0 && m <= 12)
{
return m;
}
else
{
cout << "Month " << m << " is invalid. Set to 1." << endl;
m = 1;
return m;
}
}
void Date::printDate() const
{
cout << day << "-" << month << "-" << year << endl;
}
Date::~Date ()
{
cout << "Date object for date class is destroyed." << endl;
}
employee.h
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include "date.h"
class Employee
{
public:
Employee(const char * = '\0', const char * = '\0', const Date & = birthDate, const Date & = hireDate);
~Employee();
void setEmployeeData(const char *, const char *, const Date &, const Date &);
void setFirstName(const char *);
void setLastName(const char *);
void setBirthDateObject(Date);
void setHireDateObject(Date);
const char *getFirstName();
const char *getLastName();
Date getBirthDateObject() const;
Date getHireDateObject() const;
void printDate() const;
private:
char first_name[25];
char last_name[25];
static Date birthDate;
static Date hireDate;
};
#endif
employee.cpp
#include <iostream>
#include "employee.h"
using namespace std;
Employee::Employee(const char *first, const char *last, const Date &dateOfBirth, const Date &dateOfHire)
{
setEmployeeData(first, last, dateOfBirth, dateOfHire);
}
void Employee::setEmployeeData(const char *first, const char *last, const Date &dateOfBirth, const Date &dateOfHire)
{
setFirstName(first);
setLastName(last);
setBirthDateObject(dateOfBirth);
setHireDateObject(dateOfHire);
}
void Employee::setFirstName(const char *first)
{
int length = strlen(first);
if (length < 25)
{
length = length;
}
else
{
length = 24;
}
strncpy(first_name, first, length);
first_name[length] = '\0';
}
void Employee::setLastName(const char *last)
{
int length = strlen(last);
if (length < 25)
{
length = length;
}
else
{
length = 24;
}
strncpy(last_name, last, length);
last_name[length] = '\0';
}
void Employee::setBirthDateObject(Date dateOfBirth)
{
birthDate = dateOfBirth;
}
void Employee::setHireDateObject(Date dateOfHire)
{
hireDate = dateOfHire;
}
const char *Employee::getFirstName()
{
return first_name;
}
const char *Employee::getLastName()
{
return last_name;
}
Date Employee::getBirthDateObject() const
{
return birthDate;
}
Date Employee::getHireDateObject() const
{
return hireDate;
}
void Employee::printDate() const
{
cout << first_name << ' ' << last_name << endl;
cout << "Hired date: "; hireDate.printDate(); cout << endl;
cout << "Birth date: "; birthDate.printDate(); cout << endl;
}
Employee::~Employee()
{
cout << "Employee object for employee class destroyed." << endl;
}
main.cpp
#include <iostream>
#include <vector>
#include "employee.h"
using namespace std;
int main()
{
const int number = 10;
char fname[25];
char lname[25];
int bd, bm, by, hd, hm, hy;
vector<Date> emp_db(number);
vector<Date> emp_hd(number);
vector<Employee> emp(number);
for (int i = 0; i < number; i++)
{
cout << "Employee " << i + 1 << endl;
cout << "Enter first name: ";
cin >> fname;
cout << "Enter last name: ";
cin >> lname;
cout << "Enter date of birth: ";
cin >> bd >> bm >> by;
cout << "Enter hire date: ";
cin >> hd >> hm >> hy;
emp_db[i].setDate(bd, bm, by);
emp_hd[i].setDate(hd, hm, hy);
emp[i].setEmployeeData(fname, lname, emp_db[i], emp_hd[i]);
}
return 0;
}
Everything is compiling fine but is giving linker error when built:
employee.obj : error LNK2001: unresolved external symbol "private: static class Date Employee::birthDate" (?birthDate@Employee@@0VDate@@A) main.obj : error LNK2001: unresolved external symbol "private: static class Date Employee::birthDate" (?birthDate@Employee@@0VDate@@A) employee.obj : error LNK2001: unresolved external symbol "private: static class Date Employee::hireDate" (?hireDate@Employee@@0VDate@@A) main.obj : error LNK2001: unresolved external symbol "private: static class Date Employee::hireDate" (?hireDate@Employee@@0VDate@@A) Debug/lab 4a.exe : fatal error LNK1120: 2 unresolved externals
I'm a beginner in this business so please stay low and tell me that is it right what I'm doing in main()
?
Update: I have a doubt that something in main()
is causing it. Look at my main()
and tell me is it right? I mean, does C++ allow what I'm doing?