0

I have a template class called Array

        template <class T>
        class Array {
        private:
            T *m_data;
            int m_size;
            static int defaultsize;

        public:
            Array();
            Array(int size);
            Array(const Array<T> &obj);
            virtual ~Array();
            static int DefaultSize();
            static void DefaultSize(int count);
            Array<T> &operator=(const Array<T> &source);
            int Size() const;
            void setElement(int index, T p);
            T &getElement(int index) const;
            T &operator[](int index);
            const T &operator[](int index) const;

        };

My task is to generically inherit this into a derrived class

template <class T>
        class NumericArray: public Array<T>
        {
            public:
                NumericArray(); //default constructor
                NumericArray(int size); // overirrdden constructor
                ~NumericArray(); //destructor
                void operator*(double index); //scaling numeric array
                NumericArray<T> operator+(NumericArray<T> inputArray); //adding numeric array
                double &DotProduct(NumericArray<T> inputArray);
                NumericArray(const NumericArray<T> &obj);
        };

NumericArray.cpp

template <class T>
        double & NumericArray<T>::DotProduct(NumericArray<T> inputArray) {

            try {
                if(inputArray.Size()!= this->Size())
                {
                    throw OutofBoundsException(inputArray.Size());
                }
            }
            catch(ArrayException &error)
            {
                cout << error.GetMessage()<< endl;
            }

        }

        template <class T>
        void NumericArray<T>::operator*(double index) {}

        template <class T>
        NumericArray<T> NumericArray<T>::operator+(NumericArray<T> inputArray) {}

        template <class T>
        NumericArray<T>::~NumericArray() {}

        template <class T>
        NumericArray<T>::NumericArray() {}

        template <class T>
        NumericArray<T>::NumericArray(int size) {}

        template <class T>
        NumericArray<T>::NumericArray(const NumericArray<T> &obj) {}

I am struggling to create the constructor and access the m_data and m_size and default size variables for the NumericArray instance. I am sure I am missing some easy intermediate step. I am unable to access any of the base class data from Array or its functions. Would love to know what I am doing wrong here.

  • 3
    What issues do you get? Compiler error? Incorrect output? Also, probably relevant: [Why can templates only be implemented in the header file?](https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file) – Yksisarvinen Jun 30 '20 at 12:43
  • 1
    `access the m_data and m_size` Well, they are private. Write accessor functions. Or make them protected. – KamilCuk Jun 30 '20 at 12:44
  • 1
    m_data is private - it can't be accessed from derived classes. If you need to access this from derived class you need to make it protected or public. What are you trying to achieve? – Mircea Ispas Jun 30 '20 at 12:44
  • @KamilCuk Noted. How do i access the base class constructor in my derived class? I amt tying to call the base class constructor in the derived class's constructor. – Udbhav Agarwal Jun 30 '20 at 12:54
  • `NumericArray::NumericArray() : Array() {}` https://stackoverflow.com/questions/120876/what-are-the-rules-for-calling-the-superclass-constructor – KamilCuk Jun 30 '20 at 12:58
  • @KamilCuk In the code I have pasted, does the NumericArray Class inherit the Array memebrs correctly? If so does that mean that when I initiate a NumericArray, will it have m_data, m_size as its data members? – Udbhav Agarwal Jun 30 '20 at 13:03
  • Any instantiation of `NumericArray` behaves like any other class, including how it inherits from base classes. You can't access any inherited private members, but they exist. – molbdnilo Jun 30 '20 at 13:05
  • It will "have" them, but you will not be able to access them from derived class. – KamilCuk Jun 30 '20 at 13:05
  • @KamilCuk Why are the constructor not inherited? and why do i have to create new constructors? – Udbhav Agarwal Jun 30 '20 at 13:28
  • 1
    Because the rules say so. You don't have to, you can inherit them. – KamilCuk Jun 30 '20 at 13:35

0 Answers0