0

I have a base class Array and a derived class NumericArray

class Array{//Array.h
private: int size;

public: Array();//Default constructor
        Array(int index);// Initialize with size
        GetSize();//getter function
};

class NumericArray:public Array
{//NumericArray.h

public: NumericArray();//Default constructor
        NumericArray(int index);// Initialize with size
};

I know how to call the default Array constructor in NumericArray class. But when it comes to the NumericArray(int index), I have no idea. Since the derived class cannot access the size in base class, I think I should use getter function in base class. But how should I do?

Thanks.

Yifan
  • 27
  • 1
  • 6
  • Not your question, but note that mutable arrays don't suit the ordinary inheritance very well. E.g. `std::unique_ptr` is specialized for arrays to not allow up- or down- conversion. Of course it depends on the functionality offered by the base class `Array`, but if it doesn't offer any array-like functionality it should maybe not be there in the first place. – Cheers and hth. - Alf Feb 11 '18 at 05:35
  • The missing semicolons show that the presented code is **not real code**. Please do post real code. – Cheers and hth. - Alf Feb 11 '18 at 05:36
  • I see a dim, very dim, actual question being asked here: yes, in the body of the derived class constructor, since the base class is now fully constructed you can invoke the base class's methods from the derived class's constructor. – Sam Varshavchik Feb 11 '18 at 05:40
  • Even though access to the constructors and destructor of the base class is not inherited as such, they are automatically called by the constructors and destructor of the derived class. – David C. Rankin Feb 11 '18 at 08:09

2 Answers2

0

What you can do is call the constructor of the base class whenever the derived class has an object instantiated. You can do this relatively easily by using something similar to member-init:

class Base
{
private:
    int size;
public:
    Base(int param) { size = param;}
    //rest of the code here
};

class Derived : public Base
{
private:
   //other data members
public:
    Derived(int param): Derived(param) { //set rest of data}
};

This passes param to the constructor of Base, and lets it do whatever with it. Derived doesn't need direct access to size in Base because it can use what is already in place to handle it. Here is a pretty good explanation if you want a more in depth explanation and examples. (scroll about 2/3 of the way to get to constructors)

Hawkeye5450
  • 672
  • 6
  • 18
0

Since size is a private variable in the base class Array, it cannot be accessed in the child class NumericArray.

There are two ways to access size in the child class NumericArray:

  1. Make size a protected variable in the base class Array and access it in the child class NumericArray.

    protected: int size;
    
  2. Write a getter public function (GetSize()) in the base class Array that returns size, and the child class NumericArray can just call this getter public function in the constructor of the child class using super.GetSize() per this link.

    public:
        GetSize() { return size }
    
Vikram Hosakote
  • 3,528
  • 12
  • 23
  • Thanks, I also think I should use the getter function. May I ask how to use it? How can I call the GetSize() in the constructor in NumericArray.cpp after I write this function? – Yifan Feb 12 '18 at 09:59
  • @Yifan Upvote my answer if you think it is useful! `GetSize()` in the base class can be called in the child class `NumericArray` using `super.GetSize()` per https://docs.oracle.com/javase/tutorial/java/IandI/super.html. – Vikram Hosakote Feb 12 '18 at 22:00