Purpose of : public <class name>?

This is a discussion on Purpose of : public <class name>? within the C++ Programming forums, part of the General Programming Boards category; What's the purpose of, when inheriting, doing: Code: class baseClass { // yadda }; class inheritClass : public baseClass { ...

  1. #1
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903

    Purpose of : public <class name>?

    What's the purpose of, when inheriting, doing:

    Code:
    class baseClass
    {
    // yadda
    };
    
    class inheritClass : public baseClass
    {
    // yadda
    };
    I've always just used : public <name> when inheriting, but is there any difference if you do it with private or protected?

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Here's one good old example:

    class B has a public: member, a protected: member, and a private: member.

    class B {
    public:
    void publ() throw();
    protected:
    void prot() throw();
    private:
    void priv() throw();
    };

    Class PrivD privately inherits from B, class ProtD protectedly inherits from B, and PublD publicly inherits from B.
    Code:
    class PrivD : private   B { };
    class ProtD : protected B { };
    class PublD : public    B { };
    With private inheritance, the public and protected parts of B become private in PrivD. This means that PrivD can access these member functions, but user code and classes derived from PrivD cannot access them.

    With protected inheritance, the public and protected parts of B become protected in ProtD. This means that members and friends of ProtD can access these member functions, as can classes derived from ProtD, but user code cannot access them.

    With public inheritance, the public parts of B become public on PublD, and the protected parts of B remain protected in PublD.

    In all three cases, the private parts of B are inaccessible to the derived classes (PrivD, ProtD, and PublD) as well as to user code.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Ok..so:

    Code:
    class Base
    {
    public:  void pub();
    protected: void prot();
    private: void priv();
    };
    
    
    class Public : public Base
    {
    public:
    
       void pub();
    protected:
    
       void prot();
    private:
    };
    
    
    class Protected : protected Base
    {
    public: 
    protected:
    
       void prot();
       void pub();
    private:
    };
    
    
    
    class Private : private Base
    {
    public: 
    protected:
    private:
    
       void prot();
       void pub();
    };
    Correct?
    Last edited by jverkoey; 04-13-2005 at 05:24 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Private and protected inheritance are very different concepts from public inheritance that are used for different design models. They are rare, especially protected inheritance. Private inheritance is similar to composition (is-implemented-in-terms-of), but composition is usually preferred. The public inheritance you are used to models is-a relationships, which are more common than is-implemented-in-terms-of that require private inheritance.
    Quote Originally Posted by jverkoey
    Correct?
    No. priv() is not visible in any way in any of the derived classes. Otherwise it is correct.
    Last edited by Daved; 04-13-2005 at 05:23 PM.

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Ah, ok, *edits it to match that*

    There we go, that's correct then?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    Yes.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    My beginning C++ book has a list of the different access levels with inheritance, so I assume most do. There are 9 different combinations:
    Code:
    class DerivedName : public BaseName
    base class: derived class:
    public----------->public
    protected-------->protected
    private---------->inherited but not accessible--except by publicly inherited base class functions
    
    class DerivedName : protected BaseName
    base class: derived class:
    public---------->protected
    protected------->protected
    private--------->inherited but not accessible*
    
    class DerivedName : private BaseName
    base class:     derived class:
    public--------->private
    protected------>private
    private-------->inherited but not accessible**
    (src: "Ivor Horton's Beginning C++")

    Quote Originally Posted by Daved
    No. priv() is not visible in any way in any of the derived classes.
    That's not quite true. The result: "inherited but not accessible--except for publicly inherited functions" allows you to do this:
    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    private:
    	int member;  //Can you get to this with public inheritance?
    
    public:
    
    	void display()
    	{
    		cout<<member<<endl;
    	}
    
    	Base(int num)
    	{
    		member = num;
    	}
    
    	Base()
    	{
    		member = 0;
    	}
    };
    
    class Derived : public Base
    {
    private:
    	double number;
    
    public:
    	Derived(double d, int b): Base(b)
    	{
    		number = d;
    	}
    
    	Derived()
    	{
    		number = 0.0;
    	}
    };
    
    
    
    int main()
    {
    	Base B(2);
            B.display();
    
    	Derived D(1.0, 2);
    	D.display();   //inherited method
    
    	return 0;
    }
    In addition, after examining the chart above, I can see there is also a way to get some visibility of a private base class member with protected inheritance. A public base class function, like display() (which has access to and displays the private base class member) will be protected in the derived class. Protected is the same as private within a class, and one of the first things we learn about classes is that class methods can access private members of the class. Although that means you cannot access display() directly with derived class objects, it does mean you can create methods in the derived class that have access to display(). That means you can create a method in the derived class which calls the display() method, and display() in turn has access to the private base class member. So, once again a private member of the base class has some visibility with protected base class inheritance:
    Code:
    #include <iostream>
    using namespace std;
    
    class Base
    {
    private:
    	int member;  //can you get to this with protected inheritance?
    	
    public:
    
    	void display()
    	{
    		cout<<member<<endl;
    	}
    
    	Base(int num)
    	{
    		member = num;
    	}
    
    	Base()
    	{
    		member = 0;
    	}
    };
    
    class Derived : protected Base
    {
    private:
    	double number;
    
    public:
    	Derived(double d, int b): Base(b)
    	{
    		number = d;
    	}
    
    	Derived()
    	{
    		number = 0.0;
    	}
    
    	void getBaseMember()
    	{
    		display();
    	}
    };
    
    
    
    int main()
    {
    	Base B(2);
            B.display();
    	
    	Derived D(1.0, 2);
    	//D.display();
    	D.getBaseMember(); 
    
    	return 0;
    }
    In fact, that example also works if you change the inheritance to private. With private inheritance, the public base class method display() will be private instead of protected in the derived class, but within a class there is no difference between protected and private, and both are accessible to derived class methods. So, the private base class member has some visibility with private inheritance as well.

    *
    **
    The code examples above show that there is a way to get some visibility of private members in the base with all three types of inheritance: public, protected, and private.
    Last edited by 7stud; 04-14-2005 at 05:15 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Process Memory
    By polydegmon in forum C# Programming
    Replies: 0
    Last Post: 05-26-2009, 07:18 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 01:39 PM
  4. Stuck with Structs...
    By DanFraser in forum C# Programming
    Replies: 8
    Last Post: 05-03-2007, 09:55 AM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21