Inheritance types with examples, wht is friend and virtual functions

babuvoni used Ask the Experts™
Hi all,
       Please find the question, I want to know how many types of inheritance are there in C++, and give simple examples for friend functions/class and virtual functions.
Thanks in advance.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
A virtual function is a function that can be overridden in derived classes, that is, if you have a pointer to the derived class, but the type of the pointer is that of the base class, the correct function (the one in the derived class) will be called nontheless. (This is kind of difficult to explain ;) ). For a longer tutorial on this with examples, see:

A friend function is simply a function which is not a member function of the class, but still has access to all its private and protected members as if it was.

I'm not sure what you mean by "how many types of inheritance"... do you mean single and multiple inheritance?

Hi again, babuvoni, I will not answer about inheritance types (I already did it before, certainly you are looking for a second opinion : ) , but I will post some useful links that maybe will explain friend functions/classes

"What is a friend?
Do friends violate encapsulation?
What are some advantages/disadvantages of using friend functions?
What does it mean that "friendship isn't inherited, transitive, or reciprocal"?
Should my class declare a member function or a friend function?"

"This code will illustrate about friend function and its use"

"The following example defines a friend function to overload the C++ output operator"

I hope this helps,
Fernando - Brasil
This is "Member access control" section from C++ ISO formal definition ... This is well explained and talk about friend classes and functions
Should you be charging more for IT Services?

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

This is "Member access control" section from C++ ISO formal definition ... This is well explained and talk about friend classes and functions

Paste this in fixed width:
Inheritance | Membertype_in_base       Changes to Membertype_in_derived
Public      | Public                   Public
            | Protected                Protected
            | Private                  Private
Protected   | Public                   Protected
            | Protected                Protected
            | Private                  Private
Private     | Public                   Private
            | Protected                Private
            | Private                  Private

The topic of Single & Multiple inheritance is very big. you better refer to a good C++ book.

Friends are nonmember functions of classes but can access private members of the class.

let's say u have
class base{

class derived : public base{

int main(){
  derived d;
  base *bptr = &d;
return 0;
now if u have same function name (e.g. printdata()) in both classes and you use bptr->printdata(), you will use the function you defined for base. But in many cases you would want to use derived::printdata(). Add the keyword "virtual" at the beginning of fcn prototype. this solves the problem.
but there is a side-effect to it which is not very serious: now if you have derived2 from derived, even if u didn't make printdata() virtual in derived it will remain virtual indefinitely. to access derived::printdata() you do the very obvious manipulation - dynamic_cast<derived>(...)

No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: knightmad {http:#9254077}

Please leave any comments here within the next seven days.

EE Cleanup Volunteer


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial