Questions tagged [diamond-problem]

In object-oriented programming languages with multiple inheritance and knowledge organization, 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.

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.

enter image description here

Example In C++:

/*
The Animal class below corresponds to class 
A in our graphic above
*/
class Animal { /* ... */ }; // base class
{
int weight;

public:

int getWeight() { return weight;};

};

class Tiger : public Animal { /* ... */ };

class Lion : public Animal { /* ... */ }    

class Liger : public Tiger, public Lion { /* ... */ };  


int main( )
{
Liger lg ;

/*COMPILE ERROR, the code below will not get past
any C++ compiler */

int weight = lg.getWeight();  
}
276 questions
0
votes
1 answer

avoiding diamond in multiple inheritance

i have to implement data structures that support abstract Numbers Collection is a collection of numbers sorted is a sorted collection list is a list collection of numbers (include duplicates) set is a collection of numbers without dup. Linked and…
aviadch
  • 321
  • 1
  • 5
  • 17
0
votes
1 answer

Range of a Rhombus

I want to make a function to check if a point is in range of a rhombus or not , Bool Conditional::InRange(Point P) { if( (P.x > Position.x-100) && (P.x < Position.x+100) && (P.y > Position.y-60) && (P.y < Position.y+60)) return…
Nate
  • 1
  • 2
0
votes
3 answers

Virtual Base Class in C++

I have a query regarding the virtual base class. In order to resolve the "dreaded diamond of death" /ambiguity problem in multiple inheritance, virtual base class is introduced. class A { public: void Foo() {} }; class B : public virtual A {}; class…
0
votes
4 answers

Access Individual classes of a derived class, which is derived from two same base class

I have a base class called Number. Class One and Two are derived from Number. Now I define another class Three, where I need to access individual base classes from the multiple inheritance: class Number{ protected: int val; public: …
HariHaraSudhan
  • 1,330
  • 3
  • 15
  • 24
0
votes
2 answers

Trouble with multiple inheritance. How to call base function?

I'm learning C++ and in a school assignment I must use a diamond structure even if it is not totally correct. class Book { public: virtual int getPurchasePrice() const; protected: int m_purchasePrice; }; class AdultBook:…
xenom
  • 377
  • 1
  • 5
  • 15
0
votes
1 answer

python multiple inheritance: avoid calling the constructors twice in diamond shape

Consider the following code: class A(object): def __init__(self): print("A.__init__") super(A, self).__init__() # 1 print("A.__init__ finished") class B(A): def __init__(self): print("B.__init__") …
khachik
  • 28,112
  • 9
  • 59
  • 94
0
votes
3 answers

Polymorphism object creation - diamond inheritance hierarchy

Language : C/C++ Problem : Taking the common example, where A is the parent class. B and C both inherited from class A. D is inherited from both B and C and we want to access A's function through an object of D Note: it's virtual inheritance! So…
-1
votes
1 answer

C++, child with both parents having a same ancestor

I'm having trouble with C++ classes and inheritance right now... Let's say we have Class A { A(string name); ~A(); void func(void); } Class B : public A { B(string name); ~B(); ... } Class C : public A { C(string name); …
Sinews
  • 13
  • 1
-1
votes
1 answer

Diamond inheritance with shared attribute in C++

with the following code class A { int b; int c; }; class B : virtual A { //constructor initialize b and c to something }; class C : virtual A { //constructor initialize b and c to something else }; class D : public C, public D…
-1
votes
1 answer

Is there any way to make this C++ program error free or I have to remove one of x declared in one the inherited class

#include using namespace std; class A { public: A(){cout<<"Constructing A \n";} ~A(){cout<<"Destructinf A \n";} int x; }; class B : virtual public A { public: B(){cout<<"Constructing B \n";} …
iamut04
  • 9
  • 1
-1
votes
2 answers

It keeps telling me that this object has no len in leetcode 35. Search Insert Position

it keeps giving me this error can you tell me why. I can't find the answer class Solution: def searchInsert(self, nums: List[int], target: int) -> int: x = len(List) for i in range(x): if List[i] == target: …
Rayan
  • 1
  • 1
  • 3
-1
votes
2 answers

Why doesn't data member inherit in a diamond problem c++

So, I was learning about inheritance and there was a question in last year's question set. It was just to create a diamond problem and inherit data member marks from base class to final class. So, I created a abstract base class for void setmarks()…
-1
votes
1 answer

Solving the Java Interface Diamond Issue

In the past (Java 7 and before), Java classes and interfaces served different roles: classes for abstracting method implementation; interfaces for abstracting object structure. However, since Java 8, interfaces can now define a method implementation…
Shuba
  • 376
  • 2
  • 6
  • 14
-1
votes
1 answer

Overcoming diamond ambiguity in abstract class

I have written a piece of code where I have an abstract base class. Class Tiger and Class Lion are inheriting from Animal Base Class virtually. Liger is inheriting from both Lion and Tiger. When I try to create an object of Liger and access walk…
-2
votes
2 answers

How to refactor class hierarchies to avoid diamond problem?

Suppose I have a large class hierarchy as a single rooted tree, with root being class A such that each of its offsprings has its own implementation of void f(...) and void g(...), with different argument lists. I have another class class X: public…
user103500
  • 77
  • 1
  • 8
1 2 3
18
19