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
delete
ing 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!