+ Reply to Thread
Results 1 to 3 of 3

Thread: What is inheritance? Can I access private member of base class in derive class?

  1. #1

    What is inheritance? Can I access private member of base class in derive class?

    I am new to C++ and I am learning C++.

    What is inheritance? Can I access private member of base class in derive class?

    Provide example.

  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    37
    No, you cannot access private members of base class from the derived class.

    If you wan to access base class members from the derived class make them protected.

    Example:

    Code:
    class CBase
    {
    protected:
    	int m_nProtected ;
    
    public:
    	CBase()
    	{
    		m_nProtected = 42 ;
    	}
    } ;
    
    class CDerived : public CBase
    {
    public:
    	int GetProtected()
    	{
    		return m_nProtected ;
    	}
    } ;
    
    void UseProptected()
    {
    	CDerived objDerived  ;
    	int nProtected = objDerived.GetProtected() ;
    }

  3. #3
    Thanks buddy.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts