www.vustudents.ning.com

We non-commercial site working hard since 2009 to facilitate learning Read More. We can't keep up without your support. Donate.

# GDB CS304

Graded Discussion Board Announced Dated: Jan 31, 17

Dear students,

The activity of Graded Discussion Board (GDB) will be conducted on February 07, 2017. Students will be able to post their comments for 48 hours only.

GDB Topic:

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.

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.

Try to provide precise and to the point comments avoiding irrelevant details.

For any query, feel free to email at cs304@vu.edu.pk

Views: 5554

Attachments:

### Replies to This Discussion

@Muhammad Usman's idea is accurate and way too precise.

Dear Students Don’t wait for solution post your problems here and discuss ... after discussion a perfect solution will come in a result. So, Start it now, replies here give your comments according to your knowledge and understandings....

Read Lacture no. 31 (Virtual Inheritance)

Idea For GDB Solution

In this question there is a diamond problem use in this diagram

Diamond problem:

The "diamond problem" is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

Why we use Diamond Problem:

It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this article, person is at the top, both teacher and student separately below it, and teacher –Assistant joins the two together at the bottom to form a diamond shape.  Because of this problem we cannot extend two classes for implementing multiple inheritance and to resolve this problem of multiple inheritance in object oriented programming we now use interfaces for implementing the functionality of multiple inheritance. As we know we do not define a function but only declare that function in an interface. So if we use interfaces we can extend one class and one or more interfaces or we can implement more than one interface at a time to use the functionality of multiple inheritance and we can escape from diamond problem.

In this question there is a diamond problem use in this diagram

Diamond problem:

The "diamond problem" is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

Why we use Diamond Problem:

It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this article, person is at the top, both teacher and student separately below it, and teacher –Assistant joins the two together at the bottom to form a diamond shape.  Because of this problem we cannot extend two classes for implementing multiple inheritance and to resolve this problem of multiple inheritance in object oriented programming we now use interfaces for implementing the functionality of multiple inheritance. As we know we do not define a function but only declare that function in an interface. So if we use interfaces we can extend one class and one or more interfaces or we can implement more than one interface at a time to use the functionality of multiple inheritance and we can escape from diamond problem.

Tahir Sipra MCS 2nd  thanks for sharing

Virtual Inheritance is the best solution for the diamond problem

CS304 GDB Solution

Attachments:

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.

1

2

3

4

5

## VIP Member Badge & Others

------------------------------------

## Latest Activity

6 minutes ago
Code replied to 刀のの尺's discussion Sԋαყαɾι ιʂԋϙ ԋαι ɱҽɾα
6 minutes ago
Code replied to 刀のの尺's discussion Sԋαყαɾι ιʂԋϙ ԋαι ɱҽɾα
6 minutes ago
7 minutes ago

8 minutes ago

11 minutes ago