Inheritance types with examples, wht is friend and virtual functions

babuvoni
babuvoni used Ask the Experts™
on
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.
max
Comment
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: http://www.cplusplus.com/doc/tutorial/tut4-4.html

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?

Clemens
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?"
http://www.fmi.uni-konstanz.de/~kuehl/c++-faq/friends.html

"This code will illustrate about friend function and its use"
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=6568&lngWId=3

"The following example defines a friend function to overload the C++ output operator"
http://www.juicystudio.com/tutorial/cpp/friend.html

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
http://www.kuzbass.ru/docs/isocpp/access.html

Commented:
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.

Virtual:
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>(...)

Commented:
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.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer

Commented:
agreed

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