默认构造函数全部封装在头文件中

发布于 2024-12-08 04:48:51 字数 942 浏览 1 评论 0原文

今天在我的课堂上,我们进行了继承练习。我们要编写一个 UnderGrad 类,该类继承自 Student 类,而 Student 类又继承自 Person 类。

Person 类有 3 个变量:姓名、地址和生日。

学生有 ID 号、专业和学位类型。

最后,本科生有一个以前的高中和学分获得变量。

当我为每个类编写头文件时,默认构造函数看起来像这样:

   //snippet from UnderGrad.h
 Undergrad(string nm = "", string add = "", 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;
};

我的教授说这是不可读的,而且它并不是真正这样做的。

我想知道,以这种方式创建默认构造函数有什么问题?有什么问题吗?有更好的方法吗?

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 add = "", 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2024-12-15 04:48:51

如果没有代码上下文的好处,我只需要更改这些构造函数的一些内容。

  1. 我会考虑将构造函数的定义移动到单独的源文件中。这将允许更改定义,而不会触发其余代码的重新编译。对于小型程序,或者非常简单的构造函数,如果标头不发生变化,就不可能发生变化,这可能不值得付出努力。

  2. 我将更改构造函数的定义以使用成员初始值设定项列表,而不是在构造函数主体中分配成员。 (请参阅 Als 的答案以获取解释)。

  3. 我会删除函数定义末尾的分号,它们完全不需要。

  4. 会将长构造函数分成几行,以避免代码行过长(长行可能不适合您的编辑器,并且可能很难执行差异和合并)。

  5. 您的头文件中似乎有一个 using namespace ::stdusing ::std::string 。这是不好的做法,因为这意味着包含标头的代码将应用此using。这可能会使客户端代码的含义发生变化,或者使客户端代码变得不明确而无法编译。我将删除 using,而是使用 std::string 的限定名称。


这些改变付诸实施:

//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)
{
}

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)
{
}
深空失忆 2024-12-15 04:48:51

语义上很好,尽管您可以对成员字符串使用初始化程序而不是分配每个字符串。

从风格上来说,你的台词有点长,但这就是我能认真对待的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文