I am using a class say baseClass, from which I derive another class derivedClass. I have a problem definition that says, apart from others:
i) A member – object initialiser should be used to initialise a data member, say var1, that is declared in the base class.
ii) i) is done inside a base class constructor. It says, this has to be invoked only via a derived class constructor.
iii) The base class is an abstract class, whose objects cannot be created. But, I have a third class, inside which, I use:
baseClass *baseObjects[5];
The compiler does not report an error.
I do not understand, what i) and ii) really mean. An explanation in simple words would be fine. Also, any assistance on iii) is welcome.
I think an illustration will be best.
i)
The part
i(ii)is an example of a member – object initialiser. Since C++ guarantees all constructors of members will be called before the constructor body is entered, this is your only way of specifying which constructor to call for each member.ii) In C++ there is no
superkeyword. You must specify the base class as such:That’s partially due to the fact C++ allows multiple inheritence.
iii) Note that you haven’t created any objects, only pointers to objects. And it’s by this method polymorphism via inheritence is acheived in C++.