Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given the class inheritance hierarchy where Corporation is a base class and Publ

ID: 3808039 • Letter: G

Question

Given the class inheritance hierarchy where Corporation is a base class and

PublicCorporation and PrivateCorporation are derived classes. Suppose ForProfitPrivate and NonProfitPivate are derived from PrivateCorporation. Suppose that each class is written in C++ and defines a head() member function, and suppose that ph[] is an array of pointers to the Corporation type, and suppose that ph[] is assigned the addresses of one of each of the derived objects as follows:

             PublicCorporation pc;

             PrivateCorporation pv;

            ForProfitPrivate fppv;

            NonProfitPrivate nppv; Corporation *ph[4]; ph[0] = &pc; ph[1] = &pv;            ph[2] = &fppv;

            ph[3] = &nppv;

      Explain how ph[i]->head() is interpreted for a given index i if the base class defines head() as follows: (a) Regular non-virtual method (no polymorphism) (b) Virtual method (polymorphism).

Explanation / Answer

Case 1:In the base class, head() is defined as a regular non-virtual method

Here, ph is an array which is of type Corporation, which is the base class. So, as per the C++ standards, you can make a base class pointer assigned to the derived class. Then there comes the two situations.. situation 1 is this case.

When we assign a base class pointer to the derived class object, we can not call the public methods of derived class via that pointer. We can only call the methods that are derived from the base class. And if some method name is same in both the base class and derived class, using a base pointer to derived class, when I call that method, it will always call base class method, the derived class method is hidden.

For example, if I am having a Base class B and derived class D and both has a display() method,

class B

{

....

void display()

....

}

class D : public B

{

..

void show()

...

}

Now, I declare in main(), the base class type pointer as : B *bpointer; and when I assign it bpointer = new B() and when I call bpointer->display(), it calls base class B's display() method. Pretty nice right.

Twist comes here. When I assign it a derived class like bpointer = new D(); and call bpointer->display() , it again calls class B's display(), not class D's because pointer is of type class B and in it, display() is not written virtual, so this is done.

Here, all the classes i.e. base class and derived class has head() method so if the method is a non-virtual method, all the calls from all the indexes to ph[i]->head() will call the class Corporation's head() method.

Case 2: Polymorphism is used.

When the method is declared as a virtual in the base class, we need to override it in our derived class. So, when the method head() is written virtual in class Corporation, it is overridden by every derived class with the same method signature.

So, in case of method overriding and polymorphism, it is decided at run time that which method is to be called. If the base class pointer is pointing to the base class, its method is called and if it is pointing to the derived class, derived class method is called.

So, in this case, Corporation *ph[4] is declared

ph[0] is pointing to PublicCorporation so ph[0]->head() calls head() method of class PublicCorporation.

ph[1] is pointing to PrivateCorporation so ph[1]->head() calls head() method of class PrivateCorporation.

ph[2] is pointing to ForProfitPrivate so ph[2]->head() calls head() method of class ForProfitPrivate.

ph[3] is pointing to NonProfitPrivate so ph[3]->head() calls head() method of class NonProfitPrivate.

Do comment if there is any query or you need more on this. Thank you. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote