Hi there, I was trying to summary some basic conceptions about class inheritance and I am not 100% sure about my answer to the following two questions. Please point out if my answer is wrong. And any suggestions and comments appreciated :)
1. if the base class uses "new" to dynamic allocate memory
the copy constructor for the base class is marked as protected and
the derived class doesn't have any member which needs dynamic memory allocation
does the derived class need an explicit copy constructor? //NO
Also, the copy assignment operator for base class should be marked as public and the derived class doesn't need an explicit copy assignment operator declaration
2. a declaration of function with same name and different signature in the derived class can hide functions defined in base class with same name. In following two situations:
Situation1:
#1, #2, #3 functions are all hiden in the derived classCode://base class
class base{
public:
virtual void func(); //#1
virtual void func(int); //#2
virtua void func(int, int); //#3
//...
};
//derived class A
class A: public base{
public:
virtual void func(int, int, int)
//...
};
then, for a pointer or reference of the base class (which may point or refer to the derived class object), ptr or rf, are the following statement valid?
also if class B derived from classA,which may define functions like #1, #2, #3, even, ptr actually points the a classB object, statements #4 and #5 will be still invalid.Code:ptr->func(); //#4 invalid
ra.func(3, 5); //#5 invalid
Situation 2:
if the derived class just redefines #2,. will #1 and #3 inherited? //yesCode://base class
class base{
public
virtual void func(); //#1
virtual void func(int); //#2
virtua void func(int, int); //#3
};
//derived class
class A : public base{
void func(int)
};
are statements #4 and #5 valid?
ptr->func(); //valid
ra.func(3, 5); //valid
also , can a derived class object still invoke function #1 and #3? //yes