Everything is Inherited?

This is a discussion on Everything is Inherited? within the C++ Programming forums, part of the General Programming Boards category; I tried this :- Code: #include<iostream> using namespace std; class A { int x; }; class B:private A { int ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    99

    Everything is Inherited?

    I tried this :-

    Code:
    #include<iostream>
    using namespace std;
    
    class A
    {
    	int x;
    };
    
    class B:private A
    {
    	int y;	
    	public:
    		void foo()
    		{
    			cout<<x;		// error
    		}
    };
    
    
    int main()
    {
    	A a;
    	B b;
    	cout<<sizeof(a);		// 4
    	cout<<sizeof(b);		// 8
    	b.foo();
    }
    code suggests me, that even the private members of the base class are actually inherited and present in derived objects <sizeof>, but accessibility only restricts us to which members i can actually access from my derived object or infact in my derived class

    I would appreciated any confirmation

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

    Think of it this way: If variables from the base class weren't also kept around in derived classes, all functions from the base class that used those variables would now have nothing to work with.

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I'm not sure I understand what you're saying. 'x' is private in class A, so it can only be accessed by members of class A. If you declared it as protected (or public), then your code would compile.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    I believe you misunderstand the purpose of access specifiers - Generally, they exist to control which parts of a class are internal implementation details (Or should be hidden away for whatever purpose), and which are the externally available components, by which other classes or other parts of the program may use the class.

    Inheritance causes the entire base class to be inherited - including members to which the derived class has no access. It is entirely down to the public/protected interface of the base class to allow control over base class components.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator overloaded but not inherited
    By vaibhav in forum C++ Programming
    Replies: 5
    Last Post: 08-07-2006, 05:02 AM
  2. Limiting inherited interface
    By tuxster in forum C++ Programming
    Replies: 5
    Last Post: 02-23-2006, 09:21 PM
  3. Help With Inherited Classes Needed
    By elliott in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2005, 08:05 AM
  4. Inherited static members
    By Vikstar in forum C++ Programming
    Replies: 2
    Last Post: 08-28-2003, 11:44 PM
  5. inherited for C++
    By Jaguar in forum C++ Programming
    Replies: 3
    Last Post: 07-20-2002, 11:12 AM

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