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 8406499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:07:35+00:00 2026-06-09T23:07:35+00:00

I have a base class with a constructor that requires one parameter (string). Then

  • 0

I have a base class with a constructor that requires one parameter (string). Then I have a derived class which also has its’ own constructor. I want to instantiate the derived class and be able to set the parameter of the base class’s constructor as well.

class BaseClass {
    public:
        BaseClass (string a);
};

class DerivedClass : public BaseClass {
    public:
        DerivedClass (string b);
};

int main() {
    DerivedClass abc ("Hello");
}

I’m not sure how to set the base class constructor’s parameter when calling the derived class.

  • 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-09T23:07:36+00:00Added an answer on June 9, 2026 at 11:07 pm

    You have two possibilities – inline:

    class DerivedClass : public BaseClass {
    public:
        DerivedClass (string b) : BaseClass(b) {}
    };
    

    or out of line:

    class DerivedClass : public BaseClass {
    public:
        DerivedClass (string b);
    };
    
    /* ... */
    DerivedClass::DerivedClass(string b) : BaseClass(b)
    {}
    

    more examples:

    class DerivedClass : public BaseClass {
    public:
        DerivedClass(int a, string b, string c);
    
    private:
        int x;
    };
    
    DerivedClass::DerivedClass(int a, string b, string c) : BaseClass(b + c), x(a)
    {}
    

    on initializer lists:

    class MyType {
    public:
        MyType(int val) { myVal = val; }    // needs int
    private:
        int myVal;
    };
    
    class DerivedClass : public BaseClass {
    public:
        DerivedClass(int a, string b) : BaseClass(b)
        {  x = a;  }   // error, this tries to assign 'a' to default-constructed 'x'
                       // but MyType doesn't have default constructor
    
        DerivedClass(int a, string b) : BaseClass(b), x(a)
        {}             // this is the way to do it
    private:
        MyType x;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a base class, B, which has two constructors, one with no paremeters
I have a decorator-like pattern with a base that requires a constructor parameter. The
So I have a base class with a constructor that takes (T V) .
So I have a base class that has many children. This base class defines
Possible Duplicate: Calling virtual function of derived class from base class constructor? I have
I have a base and derived class. The base class constructors have some static
I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have a base class ( car ) and a class that inherit the
I have a base class like this: public class BaseResponse { public string ErrorMessage
Given a base class that is inherited by plethora of derived classes, and a

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.