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 is pass by value & reference.

0 Answers   Accenture,


Identify the errors in the following program. #include <iostream> using namespace std; void main() { int i=5; while(i) { switch(i) { default: case 4: case 5: break; case 1: continue; case 2: case 3: break; } i-; } }

1 Answers  


What is data abstraction? How is it implemented in C++?

0 Answers   Amdocs,


How can a C function be called in a C++ program?

0 Answers  


Write a C++ Program to Find Sum and Average of n numbers using for loop.

1 Answers  






What is Coupling?

0 Answers   Cap Gemini,


What is Copy Constructor?

5 Answers   ABC, Siemens,


What is a memory leak in C++?

0 Answers   Agilent,


Explain about Searching and sorting algorithms with complexities

0 Answers   Accenture,


What are the major differences between C and C++?

0 Answers   Amazon,


Can we provide one default constructor for our class?

0 Answers  


What is a virtual base class?

6 Answers   Fidelity, Siemens,


Categories