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

  • Home
  • SEARCH
  • 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 9063687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:04:07+00:00 2026-06-16T16:04:07+00:00

Possible Duplicate: C++ superclass constructor calling rules How do you delegate work to the

  • 0

Possible Duplicate:
C++ superclass constructor calling rules

How do you delegate work to the superclass’ constructor? For instance

class Super {
public:
    int x, y;
    Super() {x = y = 100;}
};
class Sub : public Super {
public:
    float z;
    Sub() {z = 2.5;}
};

How do I get Sub::Sub() to call Super::Super() so that I don’t have to set x and y in both constructors?

  • 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-06-16T16:04:08+00:00Added an answer on June 16, 2026 at 4:04 pm

    Use constructor’s member initializer lists:

    class Super {
    public:
        int x, y;
        Super() : x(100), y(100)  // initialize x and y to 100
        {
           // that was assignment, not initialization
           // x = y = 100;
        }
    };
    class Sub : public Super {
    public:
        float z;
        Sub() : z(2.5) { }
    };
    

    You don’t need to explicitly call base class’ default constructor, it’s automatically called before derived class’ constructor is ran.

    If, on the other hand, you want base class to be constructed with parameters (if such constructor exists), then you need to call it:

    class Super {
    public:
        int x, y;
        explicit Super(int i) : x(i), y(i)  // initialize x and y to i
        { }
    };
    class Sub : public Super {
    public:
        float z;
        Sub() : Super(100), z(2.5) { }
    };
    

    Furthermore, any constructor that can be called without arguments is a default constructor, too. So you can do this:

    class Super {
    public:
        int x, y;
        explicit Super(int i = 100) : x(i), y(i)
        { }
    };
    class Sub : public Super {
    public:
        float z;
        Sub() : Super(42), z(2.5) { }
    };
    class AnotherSub : public {
    public:
        AnotherSub() { }
        // this constructor could be even left out completely, the compiler generated
        // one will do the right thing
    };
    

    And only call it explicitly when you don’t want base members to be initialized with default value.

    Hope that helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: why does initializing subclasses require calling the super class's same init function?
Possible Duplicate: Implementations and Collections My super class is Exam. One of my sub
Possible Duplicate: error: expected class-name before ‘{’ token I've got one main super-class GameObject
Possible Duplicate: Why aren't Python's superclass init methods automatically invoked? For example: class Pet(object):
Possible Duplicate: Why do I have to specify my own class when using super(),
Possible Duplicate: Django models.Model superclass By default, if you inherit a model class, django
Possible Duplicate: Understanding Python super() Class B subclasses class A , so in B's
Possible Duplicate: subclass __init__ overrides superclass’s class A(): z = 'z it is' def
Possible Duplicate: Default class inheritance access I know I can set the protection level
Possible Duplicate: cannot find interface declaration for ‘AbstractPickerView’,superclass of ‘AttackLayer’ There are 3 header

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.