Class within a Class

Hi guys,

Need a little help, here's the code...

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
#include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <iomanip>
using namespace std;

class Employee
{
public:
	Employee::Employee();
	void input();

private:
	Date emp_year;
	Date emp_month;
	Date emp_day;
	enum occupation {accountant, laborer, plumber, cook, director, clerk};
	int emp_number;
	double emp_salary;
};

class Date
{
public:

private:
	int year;
	int month;
	int day;
	
};


int main()
{
	Employee e1, e2, e3;
	char choice;
	do
	{
		cout<<"Enter the first employee.\n";
		e1.input();


		cout<<"Do you wish to enter three more employees? (y/n)\n";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day\n";
	exit(1);



	_getch();
	return 0;
}
Employee::Employee(){}
void Employee::input()
{
	char symbol;
	cout<<"Enter the employee number: ";
	cin>>emp_number;
	while(emp_number)
	{
		cout<<"employee number can't be less than zero, enter again: ";
		cin>>emp_number;
	}
	cout<<"Enter the employee's start date in the format 'mm/dd/yyyy': ";
	cin>>emp_year;//problem here!

	
}


I need to be able to access the "Date" variables, through the "Employee" class, but have met with little success. Every which way I try, I get an error. Can someone please show me the proper format, without using pointers or anything fancy?

Thanks,

Mike
Here is the code with most of the syntax errors and the non-standard sections removed:

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
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;

class Date
{
public:

private:
	int year;
	int month;
	int day;
	
};


class Employee
{
public:
	Employee();
	void input();

private:
	Date emp_year;
	Date emp_month;
	Date emp_day;
	enum occupation {accountant, laborer, plumber, cook, director, clerk};
	int emp_number;
	double emp_salary;
};




int main()
{
	Employee e1, e2, e3;
	char choice;
	do
	{
		cout<<"Enter the first employee.\n";
		e1.input();


		cout<<"Do you wish to enter three more employees? (y/n)\n";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day\n";
	exit(1);



	return 0;
}
Employee::Employee(){}
void Employee::input()
{
	char symbol;
	cout<<"Enter the employee number: ";
	cin>>emp_number;
	while(emp_number)
	{
		cout<<"employee number can't be less than zero, enter again: ";
		cin>>emp_number;
	}
	cout<<"Enter the employee's start date in the format 'mm/dd/yyyy': ";
	cin>>emp_year;//problem here!

	
}


The problem now is that you're trying to gather input from cin and place that input into an object of type Date.
cin>>emp_year;
he compiler has no idea what it means to gather input and store it into an object of type Date. It knows how to do this for an int, or a double, of a float, or a char, or any other built-in variable, but how is it supposed to know what to do with a Date object?

Your choices are to either write the operator >> for the Date object yourself, or to only use it to input data into types the compiler already knows how to deal with. For example:

cin>>emp_year.year; would gather data and put it into the year variable inside a Date object. As year is of type int, the compiler already knows how to do this.


While I am here, this looks insane:
1
2
3
	Date emp_year;
	Date emp_month;
	Date emp_day;

So each Employee has THREE Date objects. One is named emp_year, one is named emp_month, and one is named emp_day? It looks to me like you actually only want one Date object, and you want to store inside that single Date object a year, a month and a day. Making three separate Date objects and giving them these names suggests that you don't actually understand what classes are for.
Last edited on
Hi,

Thanks for the quick reply, and you are right, in your condescension, I don't know what I am doing, hence, I am in the beginner forum asking for help.
It's what you'll get round here. Most of us don't actually care enough to be condescending; it comes across like that because we just don't have the time to be anything other than brusque.

You've defined a Date class. Clearly you want to store something in here. Some data. A year, a month, a day. When you create an instance of this class, for example like this:

Date aDateObject;

you create one complete object of type Date. What's in a Date object? Let's take a look:

1
2
3
4
5
6
7
8
9
class Date
{
public:

private:
	int year;
	int month;
	int day;
};


So the Date object we made contains three integers; one named year, one named month and one named day. If you only want to store one date, you need only one Date object.



I know I need a date object, but I need at access it through the "Employee" class and I am not sure what the proper syntax is to do so. I have tried many different things, but have come up with nothing.

For example, in void Employee::input() I need to access the "Date" class. My experience with C++ tells me I should declare the type, than the variable, and then the dot operator to access it. so...

I figure it should be something like this inside "Employee" class

Date emp_year;

Then, inside the function "Employee::input()" in order to access date variable the code should be like this.

emp_year.year;

But it does not work, and I don't know why.

Thanks,

Mike
Then, inside the function "Employee::input()" in order to access date variable the code should be like this.

emp_year.year;


What you actually did: cin>>emp_year;//problem here!

If you had done: cin >> emp_year.year ; you would've had a different experience.

I would suggest (aside from using only one date in the employee class as Moschops suggests) renaming the date to something more descriptive like, perhaps:

employmentDate
Last edited on
I have tried that, even before posting here, but I get a number of errors.

int assumed. Note: C++ does not support default-int
'emp_year' : undeclared identifier
'.year' must have class/struct/union

So I have no idea what's going on. When I try this with structures, it works fine, but with classes, I am getting issues.
You're not declaring your objects. Do that, and you should be good to go
When I try this with structures, it works fine, but with classes, I am getting issues.


That's very unfortunate, as in C++ a structure and a class are identical but for default privacy level. It makes no sense that you can do it with a structure but not with a class.
You are trying to access private members of the class from outside of the class. Either make Dates members public or add accessors/mutators.

EDIT:
1
2
cout<<"Enter the employee's start date in the format 'mm/dd/yyyy': ";
	cin>>emp_year;//problem here! 

If you want the date in that format you will need to overload operator>> in Date class and assign members there.
Last edited on
Topic archived. No new replies allowed.