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

Factory Method C++ – How to delete pointers returned by it

0 Answers  


What are Agilent PRECOMPILERS?

0 Answers   Agilent,


Explain the difference between C and C++.

0 Answers   Accenture,


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

0 Answers  


Can we provide one default constructor for our class?

0 Answers  






Tell us the size of a float variable.

0 Answers   Accenture,


Difference between Call by pointer and by reference.

0 Answers   Adobe,


Write a syntax and purpose of switch statement.

0 Answers   Agilent,


What does malloc return in C and C++?

0 Answers   Alter,


Difference between function overloading and function overriding.

0 Answers   Alter,


Define namespace.

1 Answers  


Describe the different styles of function prototypes in C++.

0 Answers   Adobe,


Categories