I am trying to create objects of classes inside functions, and then using those objects in main(). Here is my code, followed by the problem.
Code:
#include <iostream>
#include <cstdlib>
void createDates(); // notice this
/////////skip this//////////////////////////
class Date
{
private:
int month, day, year;
public:
Date(int m = 0, int d = 0, int y = 0)
{month = m; day = d; year = y;}
void setDate(int m, int d, int y){month = m; day = d; year = y;}
};
class Employee
{
private:
int id, salary;
Date hired;
public:
Employee(int, int, Date);
};
Employee::Employee(int i, int s, Date h)
{
id = i; salary = s;
hired = h;
}
//////////////stop skipping////////////////////////////
int main()
{
createDates(); // this executes before anything else in main.
Date today(7, 31, 2002); // object to be used in future lines
Employee jake(1, 32900, today); // uses the Date object created above
Employee brian(2, 34000, tomorrow); // uses the object created in function
system("pause");
return 0;
}
void createDates()
{
Date tomorrow(8, 1, 2002);
}
main() begins by calling createDates(), which creates one Date object called tomorrow.
Then, a date object is created called today directly in main().
The code then goes on to create Employee objects, using Date objects as their last argument/parameter thingy. jake will work fine, but brian will not, because its class object parameter/argument thingy was declared outside main().
I need to have tomorrow declared in a separate function. Need to!! how can I have Employee objects recognize Date objects declared in such functions?