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 the 4 difference between delete[] and delete?

0 Answers   Alter,


Identify the error in the following program. include<iostream> using namespace std; void main() { int num[]={1,2,3,4,5,6}; num[1]==[1]num ? cout<<"Success" : cout<<"Error"; }

1 Answers  


What is conversion constructor in C++

0 Answers   TCS,


Is deconstructor overloading possible? If yes then explain and if no Then why?

1 Answers  


How to delete array of objects in C++? Proof by C++ code for proper deletion

0 Answers  






C++ Public access specifier instead of Private – What is bad ?

0 Answers  


Explain encapsulation in C++.

0 Answers   Verifone,


Write a C++ Program to Display Number (Entered by the User).

1 Answers  


Explain the FOR loop with a help of a code.

0 Answers   Accenture,


What Is Polymorphism in C++ ?

1 Answers   IBS, Impetus, ITC Indian Tobacco Company, Motorola,


What do you by Function Overloading in C++?

0 Answers   Akamai Technologies, Infogain,


In C++ what is a vtable and how does it work?

0 Answers   Agilent,


Categories