What is virtual class and friend class?

Friend classes are used when two or more classes are designed to work together and need access to each other's implementation in ways that the rest of the world shouldn't be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.  

Showing Answers 1 - 16 of 16 Answers

Friend Class: When an entire class is declared as a friend of another class. For example, Class A is declared as a friend is Class B. All members of Class A can access Class B.

Virtual Class: Also known as Virtual Base Class, aids in effective multiple inheritance. For example,
class A
{
public:
virtual void myFunc() { // do something
}
};
class b : public class A {
public:
void myFunc() { // do something else
}
// Has two variations of myFunc, one from class A, and one from class b.
}
class c: public class A {
public:
void myFunc() { //do third thing
}
// Has two variations of myFunc, one from class A, and one from class c.
}
now, without doing anything virtual, if you say
class D: public b, public c
{
public:
void myFunc() { // do fourth thing
}
// has 6 variations of myFunc, one from class A, two from class b, two from class c along with one from D.
}
Instead, if you say,
class D: virtual public b, virtual public c
{
public:
void myFunc() { // do fourth thing
}
// Has 4 variations of the, one from class A, one from class b, one from class c, and one from class d. 
}

  Was this answer useful?  Yes

Hi SomGollakota,

if i will declared
class A
{};

class b: virtual public A
{};

class c: virtual public A
{};

class d:public b,public c
{};

instead of

class b:public A
{};
class c:public A
{};
class d:virtual public b,virtual public c
{};

Then what will be happen, the class the has also 6 variation or 4 variations of the function. Please reply me what is the difference between by placing the "virtual" keyword in class b and class c inheritance level AND by placing class d inheritance level that
you have mentioned.
Myfun()

  Was this answer useful?  Yes

Answering amarendra.moharana's question.
Amarendra, My apologies that it took so long to answer your question. Haven't logged in for quite a while.
In class D of your example, you will still have 6 versions of the MyFunc. The reason is as follows. 

When you inherited in Class B and Class C from Class A, your inheritance was virtual. So, this far, the virtual class problem in a multiple inheritance case has been effectively handled. However, since class B and class C are both inheritting from the same base class, the problem you were trying to tackle by virtual inheritance was not there to begin with. Class B has one copy of MyFunc (it's own) and one copy of its base class A (which the root class in the hierarchy). The same is the case with class C as well. From inside class B or class C methods, you can always call MyFunc (member of itself) or A::MyFunc (member of its parent).

However, the problem actually comes when you do multiple inheritance from two child classes (siblings) of the same parent. In your example, you class D does that (inheriting from siblings). So, the fact that each of the siblings (class B and class C) had a virtual inheritance from their parent (class A) does not really matter to Class D. This is because class D has not used virtual.

Hope this answers your question. Please do let me know how it goes.

Thanks,
--Som G

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions