What is a virtual base class?

Answers were Sorted based on User's Feedback



What is a virtual base class?..

Answer / atul jawale

Virtual base class is a base class acts as an indirect base
for more than one without duplication of its data members.

A single copy of its data members is shared by all the base
classes that use it as a virtual base.

For example:
A
/ \
B C
\ /
D

class A { /* ... */ }; // indirect base class
class B : virtual public A { /* ... */ };
class C : virtual public A { /* ... */ };
class D : public B, public C { /* ... */ }; // valid

Using the keyword virtual in this example ensures that an
object of class D inherits only one subobject of class A.

Is This Answer Correct ?    75 Yes 7 No

What is a virtual base class?..

Answer / mukesh kumar

To Remove the ambguity problem in multiple inheritance we
make the base class as vitual that means it will make only
one copy its common data member.

EX: suppose we have a base class A , which have a data
member x as integer.

class b: virtual public A
{
};
class C : virtual public A
{
};
class d: public B,public C
{
}

without making classes A and B as virtual class d had two
copies of x.This will arised ambguity problem.

Is This Answer Correct ?    38 Yes 4 No

What is a virtual base class?..

Answer / ragib nasir ahmed

A virtual base class is one in which only one copy of the
base class is inherited to the derived class.
Let us consider the classes A,B,C,D
class A
{
int a;
public:
void geta(void)
{
a=10;
}
};
class B: public virtual A
{
int b;
public:
void getb(void)
{
b=20;
}
};
class C:virtual public A
{
int c;
public:
void getc(void)
{
c=30;
}
};
class D:public B,public C
{
public:
void display(void)
{
cout<<a<<b<<c<<d;
}
};

Is This Answer Correct ?    25 Yes 2 No

What is a virtual base class?..

Answer / raj randhawa

Suppose you have two derived classes B and C that have a
common base class A, and you also have another class D that
inherits from B and C. You can declare the base class A as
virtual to ensure that B and C share the same subobject of
A.

In the following example, an object of class D has two
distinct subobjects of class L, one through class B1 and
another through class B2. You can use the keyword virtual
in front of the base class specifiers in the base lists of
classes B1 and B2 to indicate that only one subobject of
type L, shared by class B1 and class B2, exists.

For example:






class L { /* ... */ }; // indirect base class
class B1 : virtual public L { /* ... */ };
class B2 : virtual public L { /* ... */ };
class D : public B1, public B2 { /* ... */ }; // valid

Using the keyword virtual in this example ensures that an
object of class D inherits only one subobject of class L.

Is This Answer Correct ?    5 Yes 0 No

What is a virtual base class?..

Answer / guessme

Suppose u 've 2 derived class b & c that is a common base
class of a. & u also 've another class d.that is inherit
from b and c.

Is This Answer Correct ?    7 Yes 15 No

What is a virtual base class?..

Answer / btech

virtual base class contains one or more virtual functions

Is This Answer Correct ?    22 Yes 46 No

Post New Answer

More C++ Interview Questions

What does it mean to declare a function or variable as static?

0 Answers   Amazon,


What are the different scope C++ provide ?

0 Answers   Amdocs,


What is an abstract class?

5 Answers   Siemens,


Tell me about virtual function

1 Answers  


What is C++11?

0 Answers   Adobe,






Can we call a virtual function from a constructor?

1 Answers  


Write a program to generate the Fibonocci Series in C++.

0 Answers   Accenture,


What are the advantages and disadvantages of B-star trees over Binary trees?

0 Answers  


What is the difference between malloc, calloc and realloc?

0 Answers   Alter,


Explain encapsulation in C++.

0 Answers   Verifone,


explain the term 'resource acquisition is initialization'?

0 Answers   Amazon,


Write a program to display the following output using a single cout statement Maths=90 Physics=77 Chemistry = 69

2 Answers  


Categories