Hi,
I think you should still have the constructor, rather than using set functions to do the same thing. The point of constructors is to initialise all the member variables. I am not saying set functions are a bad thing, they
may still be needed if something needs to change after the object has been created. At the moment you don't have that situation. Often one can combine several related functions into one: as in changing the names.
All the get functions should be marked
const
: they don't change (or should not be allowed to change a member variable). You have that for some of them, not all.
There was nothing wrong with the
printFullName() function: I would rather have one function to call, instead of several. If
Name was a class, then this could be generalised:
printFullName() would still work no matter how many names there were. you could do the same with the Date.
The parameters of the constructor which are strings or any other type of class should be passed by reference, and a member initialization list should be used. Also, do yourself a favour: make the parameters different to the member variable names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
#include <string>
#include "Student.hpp"
#include "Date.hpp"
Student::Student(const std::string& surnameArg,
const std::string& firstnameArg,
const std::string& majorArg,
const Date& dobArg)
: // colon introduces member initializer list
surname(surnameArg), // direct intialization
firstname(firstnameArg),
major(majorArg) ,
dob(dobArg)
{
// do validation here
}
|
Consider making Name a class in it's own right. It could have a
std::vector<std::string> FullName
That way, one can have multiple middle names, and deal with the Family name scenarios - some cultures write the Family name first :+)
The Date class could have an enum of the months. Scott Meyers even mentions having a class for
every month !! The idea being to make the class hard to use wrongly: given the various date formats around the world. 10/03/2016 could be 10th of March or Oct 3 depending on the locale.
Good Luck !!
Edit:
it is online so it is hard to really know if we have to do it exactly how we are doing it in the book or if we can take shortcuts |
I would try to get away with as many language features or best practises as possible, or at least ask what is allowed.