Classes

Hello, I'm having a very difficult time understanding what it is I'm doing wrong, but it seems I'm doing a LOT wrong. This is my first time ever working with classes, we're to make a header file with the class and then use it in our program, but I'm coming upon COUNTLESS errors, If anyone could direct me in the right direction that'd be wonderful, thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

//Child class definition
class Child
{
private:
	std::string name;
	int age;
	char gender;
	bool inSchool;
public:
	std::string getName();
	int getAge();
	char getGender();
	bool getInSchool();
	std::string setName(std::string enteredName);
	int setAge(int enteredAge);
	char setGender(char enteredGender);
	void setInSchool(bool inSchool);
	void childMsg();
};


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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
#include "Child.h"
using namespace std;

int main()
{
	// Declare variables
	string enteredName = " ";
	int enteredAge = 0;
	char enteredGender = ' ';

	// Request the user to give you the name, age (1-17) 
    // and gender (‘M’ or ‘F’) of a child in this order
	cout << "Enter the child's name: " << endl;
	cin >> enteredName;
	Child.setName() = enteredName;
	cout << "Enter the child's age (1-17): " << endl;
	cin >> enteredAge;
	Child.setAge() = enteredAge;
	cout << "Enter the child's gender (M/F): " << endl;
    cin >> enteredGender;
	Child.setGender() = enteredGender;

	// Create a Child object using the data given and the 
    // three-parameter constructor
    Child childOne;

    // The child has a birthday tomorrow.  Change the child
    // object to reflect the child’s new age
	childOne.setAge(enteredAge + 1);

	// Print a message about this child
	cout << childOne.childMsg();

    // Create another Child object using the 
    // default constructor
	Child childTwo;

	// Change its gender to female
	childTwo.setGender('F');

	// Change its name to “Rose”
	childTwo.setName("Rose");

    // Print a message about this child
	cout << childTwo.childMsg();

     system(“pause”);
     return 0;
}

Child::Child()
{
	name = "Roy";
	age = 1;
	gender = 'M';
	inSchool = false;
}

string Child::getName()
{
	return name;
}

int Child::getAge()
{
	return age;
}

char Child::getGender()
{
	return gender;
}

bool Child::getInSchool()
{
	return inSchool;
}

string Child::setName(string enteredName)
{
	name = enteredName;
}

int Child::setAge(int enteredAge)
{
	age = enteredAge;
}

char Child::setGender(char enteredGender)
{
	gender = enteredGender;
}

void Child::setInSchool(bool inSchool)
{
	if (age >= 0 && age <= 4)
	{
		inSchool = false;
	}
	else if (age >= 5)
	{
		inSchool = true;
	}
}

void Child::childMsg()
{
	if (gender = 'M' && inSchool = false)
	{
		cout << Child.getName() << " is a " << Child.getAge() << " year-old male child, who is not in school." << endl;
	}
	else if (gender = 'M' && inSchool = true)
	{
		cout << Child.getName() << " is a " << Child.getAge() << " year-old male child, who is in school." << endl;
	}
	else if (gender = 'F' && inSchool = false)
	{
		cout << Child.getName() << " is a " << Child.getAge() << " year-old female child, who is not in school." << endl;
	}
	else if (gender = 'F' && inSchool = true)
	{
		cout << Child.getName() << " is a " << Child.getAge() << " year-old female child, who is in school." << endl;
	}
}
Last edited on
First errors I see are in main, you don't instantiate Child.
Lines 17, 20, 23 reference the class, not an object of the class.
Lines 17, 20, 23 you're not calling the setter correctly.
1
2
3
4
    Child c; 
    c.setName(enteredName);
    c.setAge(enteredAge);
    c.setGender(enteredGender);


At lines 25-27, I see comments about using a three argument constructor, but I see no such constructor. Consequently, childOne is uninitialized.

At line 34 and 47, you're trying to output the result of childMsg(), but childMsg is defined to return nothing (ie void). Since you're doing all the couts inside childMsg, you should just call childMsg(), and not try and do a cout with it.

Lines 110, 114, 118, 122, you using the assignment operator, not the comparison operator. Should be:
 
if (gender == 'M' && inSchool == false)


edit:
Line 53, you have a default constructor for Child, but no declaration for the constructor.




Last edited on
Oh my goodness, looking back at it I made some silly mistakes! Thank you for your reply...I suppose the part that confuses me is the constructor? I thought I made it in the main function?

1
2
3
4
5
6
7
Child::Child()
{
	name = "Roy";
	age = 1;
	gender = 'M';
	inSchool = false;
}


As I wanted the defaults to be "Roy", "1", "M", and "false," until the user inputed something different. Do I place that somewhere else or am I waaay off the mark? Excuse my ignorance, thank you so much for your help and patience!
Your default constructor is fine, you just need to include a declaration for it.
After line 12 in the header:
1
2
 
  Child (); 


In addition to the default constructor, you can also have an explicit constructor that takes specific values;
 
    Child (string, int, char, bool); 

Since you have four variables, I',m assuming you would want to initialize each of them, rather than just three as indicated by the comment.
1
2
  Child::Child (string n, int a, char g, bool ins)
  { name = n; age = a; gender = g; inSchool = ins; } 

I see! Is having an explicit constructor necessary in order for a user to input their own values, or is it ok to leave as is?

I've updated my code and now I'm just getting error messages for the function

 
Fixed


Should I change that from void to something else? Also its probably not working because I'm not specifying which object I want to use right? But how would I output that message otherwise without having 2 versions of the childMsg() functions?

The error messages I'm getting are:

 
Fixed
Last edited on
Ohhhh I figured that out nevermind! Now I'm just getting 3 error messages:
Here's my updated code and the error messages:

 
Fixed


 
Fixed
Last edited on
AHHH I figured that out too! I just needed to step away from it for a bit, It's FINALLY compiling, yay!! Thank you all so much, I'm now only dealing with one more minor error! Which is the fact that my output is a bit off I should be getting:

Enter the child’s name: Rob
Enter the child’s age: 10
Enter the child’s gender: M
Rob is a 11 year-old male child, who is in school
Rose is a 1 year-old female child, who is not in school
Press any key to continue . . .

Instead I'm getting:

Enter the child’s name: Rob
Enter the child’s age: 10
Enter the child’s gender: M
Rob is a 11 year-old male child, who is not in school
Rose is a 1 year-old female child, who is not in school
Press any key to continue . . .

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
void Child::setInSchool(bool inSchool)
{
	if (getAge() >= 0 && getAge() <= 4)
	{
		inSchool = false;
	}
	else if (getAge() >= 5)
	{
		inSchool = true;
	}
}

void Child::childMsg()
{
	if (gender == 'M' && inSchool == false)
	{
		cout << getName() << " is a " << getAge() << " year-old male child, who is not in school." << endl;
	}
	else if (gender == 'M' && inSchool == true)
	{
		cout << getName() << " is a " << getAge() << " year-old male child, who is in school." << endl;
	}
	else if (gender == 'F' && inSchool == false)
	{
		cout << getName() << " is a " << getAge() << " year-old female child, who is not in school." << endl;
	}
	else if (gender == 'F' && inSchool == true)
	{
		cout << getName() << " is a " << getAge() << " year-old female child, who is in school." << endl;
	}
}
Last edited on
Topic archived. No new replies allowed.