CS304 GDB Solution

CS304 Object Oriented Programming
GDB Fall 2016

The Problem:
The main purpose of inheritance is reuse of code. Multiple-inheritance is more productive in terms of reuse of code than single inheritance. But all of this comes with a cost. Suppose we have a class Person which is inherited by Teacher and Student. Both Teacher and Student are further inherited by Teacher Assistant thus resulting in multiple-inheritance.

Question: Would you select multiple-inheritance in your code along with its intrinsic cost or do the other ways? Justify with solid reasons in either case.

Answer: In the scenario mentioned, I would not use multiple-inheritance in my code. Because multiple-inheritance is prone to errors. The diamond problem is an example. If multiple-inheritance is used, the new Teacher-Assistant class will have two copies of many same functions that are inherited by the Teacher and Student Classes from Person Class, calling such functions results in ambiguity.
One solution for the problem would be virtual inheritance. In virtual inheritance we use the word virtual in the deceleration of the classes and when we inherit with virtual inheritance, we are guaranteed to get only a single instance of the common base class. So this will solve many problems in our code and will be the best practice to use here.


(07-27-2017, 08:24 PM)abrish.khan Wrote:  a.salam

WaAlaikum Assalam. Welcome to ForumVU Smile

Possibly Related Threads…
Thread / Author Replies Views Last Post
Last Post by Plaquenil
09-18-2021, 12:17 PM
Last Post by Crywoge
09-16-2021, 02:44 AM
Last Post by Hessess
09-13-2021, 09:16 PM

Users browsing this thread: 1 Guest(s)