Thread: Inheritance Related Question

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Inheritance Related Question

    Hi all

    I was thinking on the behavior of inheritance. So I have declared protected constructor in base class. In derived class, I inherited base class as private, I have created a function which return a base class object.

    Below is the code.

    Code:
    #include<iostream>
    
    using namespace std;
    
    
    class A
    {
     int a;
    
    
    	void getData()
    	{
    	  cout << "\n Inside A getData()";
    	}
    
    protected:
    	A(){}
    
    
    };
    
    
    class B : private A
    {
      
      int b;
      
    public:
    	void getData()
    	{
    	  cout << "\n Inside B getData()";
    	}
    
    	A* getBase()
    	{
    	  A *a = new A();  // error line
    	  return a;
    	}
    
    	B() {} 
    };
    
    int main()
    {
    
    
      B b;
      b.getData();
      getchar();
      return 0;
    
    }
    At error line, I am getting below error:

    cannot access protected member declared in class 'A'
    I am not able to understand that when I create B b; it can access the A constructor ( as constructor is protected), then why not when I am creating A object?

    Can any body help?

    thanks
    Bargi

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    As clang says,
    protected constructor can only be used to construct a base class subobject
    That is, you could do the following, nothing more.
    Code:
    B():A() {}
    This is new for me too, thanks !

    I will leave it to some other ninja to post the standard document location for this.

    Last edited by manasij7479; 02-07-2015 at 04:35 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is not really about the constructor per se. Consider this example:
    Code:
    class A
    {
    protected:
        int n;
    };
    
    class B : private A
    {
    public:
        void foo()
        {
            A a;
            a.n = 123;
        }
    
        void bar()
        {
            n = 123;
        }
    };
    
    int main()
    {
        B b;
        b.foo();
        b.bar();
        return 0;
    }
    You will find that there's a similiar error found in foo(), i.e., you cannot assign to a.n even though n is protected, not private, in A, because that n is not inherited by B: it is the n non-public member variable of an A object that has nothing to do with B. Yet, you can assign to n in bar(), because that n is inherited by B.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Then how, A() constructor get called when B object was created. It was too protected in class A, and was called from B class constructor?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bargi
    Then how, A() constructor get called when B object was created. It was too protected in class A, and was called from B class constructor?
    In this sense the constructor was inherited by B.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    But what I have read is that, constructor,destructor,assignment operator and copy constructor do not inherit to sub classes. Isn't it contradict?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Bargi
    But what I have read is that, constructor,destructor,assignment operator and copy constructor do not inherit to sub classes. Isn't it contradict?
    Yes, that is why I say "in this sense" and provided you with an example that does not involve a constructor. It is a matter of context, i.e., in one case the context is that of invoking something in a derived class that comes from the base class subobject; in the other case you're invoking something in an entirely separate base class object.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    So it means, when we inherit, protected constructor was by default get to be accessed from derived class constructor. But if tried to create a new object, in derived class, it is same as creating new object of class base, and since it is protected, we can't call it.

    Correct?

    Thanks
    Bargi

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks a lot Laserlight...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to BGI
    By progue in forum Windows Programming
    Replies: 6
    Last Post: 05-25-2008, 10:04 AM
  2. Compile error: Inheritance related?
    By blyrrh in forum C++ Programming
    Replies: 5
    Last Post: 11-08-2006, 10:05 PM
  3. keystrokes related question..
    By AmiTsur in forum C Programming
    Replies: 3
    Last Post: 11-10-2002, 09:22 AM
  4. Inheritance related question. Is this correct?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 07-19-2002, 04:30 AM
  5. Dos related Question
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-02-2002, 12:16 PM