Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:43:48+00:00 2026-05-26T02:43:48+00:00

While in my class today, we had an exercise in inheritance. We were to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:43:48+00:00Added an answer on May 26, 2026 at 2:43 am

    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)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While answering Static class variables in Python I noticed that PythonWin PyWin32 build 209.2
I'm using the MessageBox class to show errors to users, and while that might
Today while playing with a De-compiler, i Decompiled the .NET C# Char Class and
This is a challenging one that got me stumped while I was coding today.
I came across this class while reading a C# book and have some questions.
While designing an interface for a class I normally get caught in two minds
I have been working on a Java project for a class for a while
While going through university and from following the development of SO, I've heard a
While I've seen rare cases where private inheritance was needed, I've never encountered a
Earlier today I was working on my homework for my C# class LINK TO

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.