Creating objects within classes

Hello, I was wondering how I would create an object within the private members of a class. My C++ professor is of zero help to me, and refuses to answer any question (even though this is my first of the year -.-). Anyhow, he said that I should make two objects and use them as private members in a different class. The issue is that the within the "TimeCard" class, it doesn't recognize the objects punchInTime and punchOutTime. It refuses to acknowledge that they're class objects :(. Any help or general advice is greatly appreciated! Thank you :)

TLDR
1
2
3
4
5
6
7
8
9
10
class TimeCard {
 private:
   time2 punchInTime; //the rest of the class functions don't recognize that
   time2 punchOutTime;//these are members of another class.
 ...
}

class time2 {
 ...
}



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  class TimeCard {
private:
	string workerID;
	time2 punchInTime;
	time2 punchOutTime;
	double payrate, totalpay = 0;
	bool hasPunched=0;
public:
	TimeCard();
	TimeCard(string, double);
	void punchOut();
	void punchIn();
	void pay();
};

TimeCard::TimeCard(){}

TimeCard::TimeCard(string workerID, double payrate)
{
	this->workerID = workerID;
	this->payrate = payrate;
	
}

void TimeCard::punchIn() 
{
	int sec, min, hour;
	for (;;)
	{
		cout << "Punched in at (H M S): ";
		cin >> hour >> min >> sec; //takes punched out time
		if (hour >= 8 && hour < 17 && min >= 0 && min <= 59 && sec >= 0 && sec <= 59) break;
		//validates input
		if (hour == 17 && min == 0 && sec == 0) break;
		//validates special case of input
		else cout << endl << "Improper input. Try again." << endl;
	}
	punchInTime.calcTime(hour, min, sec);
}

void TimeCard::punchOut()
{
	int sec, min, hour;
	for (;;)
	{
		cout << "Punched out at (H M S): ";
		cin >> hour >> min >> sec; //takes punched out time
		if (hour >= 8 && hour <= 17 && min >= 0 && min <= 59 && sec >= 0 && sec <= 59) break;
		//validates input
		if (hour == 17 && min == 0 && sec == 0) break;
		//validates special case of input
		else cout << endl << "Improper input. Try again." << endl;
	}
	punchOutTime.calcTime(hour, min, sec);
	hasPunched = 1;
}

void TimeCard::pay() {
	totalpay = ((punchOutTime.returntime() - punchInTime.returntime()) / 3600) * payrate;
	if (punchOutTime.returntime() < punchInTime.returntime()) cout << "You cannot punch out before punching in." << endl;
	else cout << "Your total pay for the day is $" << totalpay;
}


///////////////////////////////////////////////////////////////////////////////
class time2 {
private:
	int hour, minute, second, seconds;
public:
	time2();
	void calcTime(int,int,int);
	int returnTime();
};

time2::time2() {//constructs class type
	hour = 0;
	minute = 0;
	second = 0;
}

void time2::calcTime(int hour, int minute, int second) {//calculates seconds of day
	int seconds = 0;
	this->hour = hour;
	this->minute = minute;
	this->second = second;
	//validation to make sure they're within the correct range
	seconds += 3600 * hour;
	seconds += 60 * minute;
	seconds += second;
}

int time2::returnTime() {//returns time of day in seconds
	return seconds;
}
Last edited on
Also, feel free to ask any questions in particular about why I did something, or why anything is the way that it is lol. Don't wanna confuse anyone!
Do you understand the proto-typing rule in C++ where functions that are called must already exist? Well the same rule applies for classes. You can't use Time2 because Time2 doesn't exist yet to the compiler.

You need to copy and paste all the time2 code above TimeCard.

1
2
3
4
5
6
7
8
9
10
class time2 {
 ...
}

class TimeCard {
 private:
   time2 punchInTime; //the rest of the class functions don't recognize that
   time2 punchOutTime;//these are members of another class.
 ...
}


When you get to larger programs. It's better to separate your class definitions from function definitions using .cpp and .h files. That will make sure you don't have to worry about which code needs to be above which other code.
Last edited on
closed account (E0p9LyTq)
Either forward declare the time2 class or swap the class definitions so time2 comes before the TimeCard class.
Yah, I just corrected that issue in my code, but it's still giving me the error that "TimeCard::punchOutTime uses undefined class 'time2'" on line 4/5 of the code :O

NVM I GOT IT. LOVE YOU GUYS :D
Last edited on
I take that back. You can't forward declare. You must copy and paste Time2 above TimeCard. That is because you put all your code in the same file. Has that fixed it nerded?

Edit: Great! :D
Last edited on
Topic archived. No new replies allowed.