开发者

Default Constructors all encapsulated in the header files

While in my class today, we had an exercise in inheritance. We were to write a UnderGrad class that inherited from a Student class which inherited from a Person class.

The Person had 3 variables to the class a name, an address and a birthday.

The Student had an ID number, Major and degree type.

And finally the Undergrad had a previous high school and credits earned variables.

When I wrote the header file for each of the classes, the default constructor looked something like this:

   //snippet from UnderGrad.h
 Undergrad(string nm = "", string ad开发者_StackOverflowd = "", string bday = "", int id = 0, string maj = "", string degtyp = "", string hs = "", int cred = 0) : Student(nm,add,bday,id,maj,degtyp) {

     highSchool = hs;
     credits = cred;
};


//snippet from Student.h
Student(string nm = "", string add = "", string bday = "", int id = 0, string maj = "", string degtyp = "") : Person(nm,add,bday){
     stuId = id;
     major = maj;
     degreeType = degtyp;
};

//snippet from Person.h
Person(string nm = "", string add = "", string bday = ""){
     name = nm;
     address = add;
     bDay = bday;
};

My professor said that this is unreadable and that its not really done this way.

I was wondering, what is wrong with creating default constructors this way? Is there anything wrong? Is there a better way of doing it?


Without the benefit of the context of the code, there are only a few things that I would change about these constructors.

  1. I would consider moving the definitions of the constructors into separate source files. This will allow the definitions to be changed without triggering a recompile of the rest of your code. For small programs, or very simple constructors that are unlikely to change without the header also changing, this is probably not worth the effort.

  2. I would change the definitions of the constructors to use the member initialiser list instead of assigning members in the constructor body. (See the answer from Als for an explanation).

  3. I would remove the semicolons at the ends of the function definitions, they are totally unneeded.

  4. I would split the long constructors into several lines, to avoid overly long lines of code (long lines may not fit in your editor, and could be hard to perform diffs and merges on).

  5. It appears that you have a using namespace ::std or a using ::std::string somewhere in your header file. This is bad practice, because it means that code which includes your headers will have this using applied. This could make the meaning of the client code change, or make the client code become ambiguous and fail to compile. I would remove the using, and instead use a qualified name for std::string.


These changes put into action:

//snippet from UnderGrad.h
Undergrad(
    std::string nm = "",
    std::string add = "",
    std::string bday = "",
    int id = 0,
    std::string maj = "",
    std::string degtyp = "",
    std::string hs = "",
    int cred = 0) :
        Student(nm,add,bday,id,maj,degtyp),
        highSchool(hs),
        credits(cred)
{
}

//snippet from Student.h
Student(
    std::string nm = "",
    std::string add = "",
    std::string bday = "",
    int id = 0,
    std::string maj = "",
    std::string degtyp = "") :
      Person(nm,add,bday),
      stuId(id),
      major(maj),
      degreeType(degtyp)
{
}

//snippet from Person.h
Person(std::string nm = "", std::string add = "", std::string bday = "") :
    name(nm), address(add), bDay(bday)
{
}


Semantically fine, though you could possibly use initialisers for the member strings instead of assigning each one.

Stylistically, your lines are a little long, but that's about all I could take serious issue with.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜