Thread: Inheritance in c++

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    11

    Inheritance in c++

    Could someone please help me better understand inheritance in c++. I would like to know the differences between public, protected, and private inheritance. Also, I would like to better understand the relationship between two classes declared as friends.

    From the little I have read on the subject it seems that private inheritance allows the lease amount of protection, protected offers a little more protection, and private offers the most protection to members and functions of the class. Also it seems that friend classes have full rights to a class that they are friends with.

    The more details you can provide me the better. Thanks for your help.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Could someone please help me better understand inheritance in c++.
    Generally, public inheritance keeps the access in the derived class the same as access in the base class, protected inheritance promotes public members to protected status in the derived class, and private inheritance promotes public and protected base class members to private status.

    Also, I would like to better understand the relationship between two classes declared as friends.
    The member functions of the class that is declared a friend will have unrestricted access to all the members of the class of which it has been declared a friend. Frienship is not a reciprocal arrangement.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> it seems that private inheritance allows the lease amount of protection
    I think you meant to say "public inheritance" there.

    First, learn the semantics of how they work, like what access level the base class members and functions will have, and what can be used by further derived classes.

    But once you know that stuff, you should think of public and private inheritance as completely different tools. You should not think of them in terms of one versus the other because they solve different design issues. Once you have the basic design concepts for your problem then only one will make sense as a potential implementation. Protected inheritance isn't used very often, so other than understanding what it would do if used, you don't really need to focus on it.

    Public inheritance models IS-A or substitutability. Private inheritance models HAS-A or implemented-in-terms-of. You use public inheritance if your derived class can be used as a base class object in code that already works on base class references or pointers. You use private inheritance in some cases as an alternative to simply creating a member variable of the "base class" type within the "derived class".
    Last edited by Daved; 02-22-2006 at 11:45 AM. Reason: grumpy reminded me of the phrase I meant to use

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Protected and private inheritence are also used for relationships of the form "implemented in terms of".

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    If it's OK I would like to join in this discussion and ask one more question... If a base class has a protected member, is it also a member of the derived class? I find this concept kind of difficult to understand...
    So, if we declare a derived class as
    Code:
    class sub_class : public base_class {
    .....
    ....
    };
    does it mean that it inherits members of the public base class only, right? Whatever is private and protected stays in the base class, but can be indirectly accessed? I keep reading and reading, somehow it is still a mystery to me.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by kocika73
    If it's OK I would like to join in this discussion and ask one more question... If a base class has a protected member, is it also a member of the derived class? I find this concept kind of difficult to understand...
    So, if we declare a derived class as
    Code:
    class sub_class : public base_class {
    .....
    ....
    };
    does it mean that it inherits members of the public base class only, right? Whatever is private and protected stays in the base class, but can be indirectly accessed? I keep reading and reading, somehow it is still a mystery to me.
    No, when you declare a derived class as public of a base class, all protected members are inherited.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    If a base class has a protected member, is it also a member of the derived class?
    Yes. In your example, it becomes a protected member of the derived class.

    "Within the class, the keyword protected has exactly the same effect as the keyword private...The difference between protected and private members only becomes apparent in a derived class. Members of a base class that are declared as protected are freely accessible in the function members of a derived class, whereas the private members of the base class are not."(Ivor Horton's Beginning C++, p 588)

    I keep reading and reading, somehow it is still a mystery to me.
    Try experimenting. Here is an example:

    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    public:
    	Base(int n1, int n2, int n3)
    	{
    		publicNum = n1;
    		protectedNum = n2;
    		privateNum = n3;
    	}
    	
    public:
    	int publicNum;
    protected:
    	int protectedNum;
    private:
    	int privateNum;
    };
    
    /**********************/
    
    class Derived : public Base
    {
    public:
    	Derived(int num1, int num2, int num3): Base(num1, num2, num3)
    	{
    	}
    
    	void showPublic()
    	{
    		cout<<publicNum<<endl;
    	}
    
    	void showProtected()
    	{
    		cout<<protectedNum<<endl;
    	}
    	
    	
    	void showPrivate()
    	{
    		  cout<<privateNum<<endl; //error
    		
    		 /*error C2248: 'privateNum' : cannot access private member declared in class 'Base'*/ 
    	}
    	
    };
    
    /*************************/	
    
    int main ()
    {
    	Derived d(1, 2, 3);
    	d.showPublic();
    	d.showProtected();
    	
    	return 0;
    }
    Now trying adding this function to the public section of Base:
    Code:
    void showAll()
    {
    	cout<<publicNum<<" "<<protectedNum<<" "<<privateNum<<endl;
    }
    and add this code to the end of main():
    Code:
    cout<<endl;
    
    d.showAll();
    My book says that all members of the base class are inherited--it's just a question of whether they are accessible or not; and private members in the base class are not accessible by members of the derived class.
    Last edited by 7stud; 02-22-2006 at 04:08 PM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    99
    Thanks, 7stud! Let me try rearranging some examples from my textbook too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  4. Inheritance and Polymorphism
    By bench386 in forum C++ Programming
    Replies: 2
    Last Post: 03-18-2004, 10:19 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 04:41 AM