Need some guidance with a c++ constructor

I have a class Student with name,surname and id number and class Teacher with name, surname and suject. I have to make class School which contains an array of Students and an array of Teachers.I have to make default constructor, copy constuctor, operator= and destructor.Also i have to make functions for finding the name of a student by a id number, function which prints the name and the surname of a student and a fuction which prints the name of the teachers, teaching a subject. What should I write in my main function? Thank you
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include<iostream>
#include<string>
using namespace std;
class Student
{
private:
	char *firstName;
	char *lastName;
	char *midName;
	int age;
	int number;
	void copy(Student &other)
	{
		setFirstName(other.firstName);
		setMidName(other.midName);
		setLastName(other.lastName);
		age = other.age;
		number = other.number;
	}
	void destroy()
	{
		delete[]  firstName;
		firstName = NULL;
		delete[] lastName;
		lastName = NULL;
		delete[] midName;
		midName = NULL;

	}
	void setString(char *dest, const char *source) {
		delete[]  dest;

		int sourceLen = strlen(source) + 1;
		dest = new char[sourceLen];
		strncpy(dest, source, sourceLen);
	}
public:
	Student(const char* _firstName , const char * _lastName ,const char * _midName, int _age , int _number )
	{
		this->firstName = NULL;
		this -> midName = NULL;
		this->lastName = NULL;
		setFirstName(firstName);
		setMidName(midName);
		setLastName(lastName);
		this->age = age;
		this->number = number;
	}
	
	Student(Student &other)
	{
		firstName = NULL;
		midName = NULL;
		lastName = NULL;
		copy(other);
	}
	Student& operator=(Student &other)
	{
		if (this != &other)
		{
			destroy();
			copy(other);
		}
		return *this;
	}
	~Student()
	{
		destroy();
	}
	const char * getFirstName()
	{
		return firstName;
	}
	const char * getMidName()
	{
		return midName;
	}
	const char * getLastName()
	{
		return lastName;
	}

	int getAge()
	{
		return age;
	}

	int getNumber()
	{
		return number;
	}

	void setFirstName(const char *firstName)
	{
		setString(this->firstName, firstName);
	}
	void setMidName(const char *midName)
	{
			setString(this->midName,midName );
	}

	void setLastName(const char *lastName) 
	{
		setString(this->lastName, lastName);
	}

	void setAge(int age)
	{
		this->age = age;
	}

	void setNumber(int number) {
		this->number = number;
	}
};

class Teacher
{
	char *firstName;
	char *midName;
	char *lastName;
	char subject;

	void copy(Teacher &other)
	{
		setFirstName(other.firstName);
		setMidName(other.midName);
		setLastName(other.lastName);
		subject = subject;
	}
	void destroy()
	{
		delete[]  firstName;
		firstName = NULL;
		delete[] lastName;
		lastName = NULL;
		delete[] midName;
		midName = NULL;

	}
	void setString(char *dest, const char *source) {
		delete[]  dest;

		int sourceLen = strlen(source) + 1;
		dest = new char[sourceLen];
		strncpy(dest, source, sourceLen);
	}
public:
	Teacher(const char* _firstName, const char * _lastName, const char * _midName,char _subject)
	{
		this->firstName = NULL;
		this->midName = NULL;
		this->lastName = NULL;
		setFirstName(firstName);
		setMidName(midName);
		setLastName(lastName);
		
	}

	Teacher (Teacher &other)
	{
		firstName = NULL;
		midName = NULL;
		lastName = NULL;
		copy(other);
	}
	Teacher& operator=(Teacher &other)
	{
		if (this != &other)
		{
			destroy();
			copy(other);
		}
		return *this;
	}
	~Teacher()
	{
		destroy();
	}
	const char * getFirstName()
	{
		return firstName;
	}
	const char * getMidName()
	{
		return midName;
	}
	const char * getLastName()
	{
		return lastName;
	}
	const char getSubject()
	{
		return subject;
	}

	

	void setFirstName(const char *firstName)
	{
		setString(this->firstName, firstName);
	}
	void setMidName(const char *midName)
	{
		setString(this->midName, midName);
	}

	void setLastName(const char *lastName)
	{
		setString(this->lastName, lastName);
	}

	void setSubject(char subject)
	{
		this->subject = subject;
	}
};
class School
{
	Student students[5];
	Teacher teachers[2];

public:
	
};
int main()
{
	
	system("pause");
	return 0;
Your main function should:

- create a School object
- populate the School object with students and teachers
- call the methods of the School object to demonstrate that you've implemented everything that the question asks you to.
Hello shad0wblaz3,

I think I'd be remiss if I didn't mention some notes on style here. I don't mean to sound elitist or imposing on your program -- I think what you're doing is great and it looks like you have a working understanding of some of the lower-level facilities in C++; that's awesome, as it builds into a much more thorough understanding of C++ at higher levels.

Could I ask why you're using character-pointers to manage your student member variables? std::string may be a better solution, as it manages the construction and deletion of its own memory, saving you from having to explicitly manage your own memory which is oftentimes more prone to error.

With that said, I'm noting some strange behavior in the source code you've given.

1) You're deleteing memory which is most likely not dynamically allocated, in the destroy method. A student could be instantiated in main:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
   Student blakeSmith("Blake", "Smith", "Daniel", 24, 185764);
   Student aaronJones("Aaron", "Jones", "Michael", 30, 14324);

   // For whatever reason, I copy one student to another. This calls operator=. Which calls destroy() on blakeSmith.
   blakeSmith = aaronJones; // blakeSmith.operator=(aaronJones);

   std::cin.get();

   return 0;


As noted in the comments, this calls destroy() on blakeSmith's firstName, lastName and midName which were not allocated dynamically, but rather point to const char*. Unless I'm mistaken (which I'm not ruling out as a possibility), I believe the deletion of stack memory is an error. Note you also call destroy in the destructor and in setString. Same possible issue.

2) In your Student() and Teacher() constructors, you're using setFirstName(firstName);, but the constructor's argument for first name is _firstName, so I think you're doing self-assignment there. Same with Mid and Last.

3) You're returning a pointer to internal data members of an object. Generally this is a big "no-no" as you've now exposed a direct handle to the internal data member(s) of your object. getFirstName(), getLastName(), getMidName() all return const char* which are returning the addresses of your internal data members firstName, lastName and midName. Now, granted, these are const, but const can be cast away via const_cast.

1
2
3
4
5
6
7
8
9
10
11
int main()
{
   Student blakeSmith("Blake", "Smith", "Daniel", 24, 185764);
   const char* constInternalName = blakeSmith.getFirstName(); // returns handle to internal data member
   char* internalName = const_cast<char*>(constInternalName); // or some such code; not sure if that is precisely it.
   internalName[0] = "F"; // blakeSmith.firstName now equals "Flake"

   std::cin.get();

   return 0;
}


A lot of these issues stem from using low level pointers-to-char as data members. Instead, if you were to use std::string a lot -- if not all -- of these pitfalls could be avoided. I think at some point we're all fascinated with low-level facilities and feel as though we're "cheating" the language by using the standard-library, but it was created to be used! Good luck, buddy!

Topic archived. No new replies allowed.