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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:20:34+00:00 2026-05-30T15:20:34+00:00

I have two subclasess inheriting from the same abstract superclass. There is a common

  • 0

I have two subclasess inheriting from the same abstract superclass. There is a common operation to all subclasses which depends on several attributes. Let me explain with an example:

Say this is superclass A (A is abstract):

class superClass
{
  int valueA;
  int valueB;
  float* array;

   public superClass(){
      array[valueA + valueB]
   }

   virtual foo(){
   }

}

And these are my subclasses:

class firstSubClass: superClass
{

   public firstSubClass():superClass(), valueA(100),valueB(2){
   }

   foo(){
   }

}

class secondSubClass: superClass
{

   public secondSubClass():superClass(), valueA(50),valueB(3){
   }

   foo(){
   }

}

Will the array be properly initialized? Which translates to, is the subclass constructor called before the superClass one, or is it the other way around?

Is there a way to make the initialization behavior common to both subclasses by putting it into the superClass?

Thanks in advance.

  • 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-30T15:20:35+00:00Added an answer on May 30, 2026 at 3:20 pm

    The superClass constructor is called first.

    The superClass constructor should be responsible for initializing the superClass's data members
    and firstSubClass should initialize its own data members and call the superClass's constructor;

    More info on the constructor’s calling order here and you might also want to read about constructor initializer lists here

    So, I would define superClass , firstSubClass and secondSubClass as :

    class superClass
    {
      int valueA;
      int valueB;
      float* array;
    
      public:
    
       superClass( int a , int b ): valueA(a),valueB(b) {
    
          array = new float[valueA + valueB];
       }
       virtual foo(){
       }
    
    }
    
    
    
    class firstSubClass: public superClass
    {
    
       public:
    
       firstSubClass():superClass(100,2){ //calls superclass's constructor
       }
    
       foo(){
       }
    
    }
    
    class secondSubClass: public superClass
    {
    
       public :
    
       secondSubClass():superClass(50 , 3){
       }
    
       foo(){
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a superclass, say Animal , and two subclasses: Zebra and Giraffe
I have a superclass with two subclasses. The two subclasses both have a method
The superclass is Account, and I have two subclasses - CurrentAccount and SavingsAccount. The
I have an abstract class in vb.net with two subclasses. In the abstract class
I have an abstract class that defines some methods. This class has two subclasses.
I have two models, Article and Recipe, which have a bunch of the same
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two instances of QObject subclasses and two QMetaMethod instances of signal in
I have two classes, Class A and Class B, both of them are subclasses
I have a model, Director with two DateFields, and two subclasses (code below). I

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.