-
July 22nd, 2007, 12:22 AM
#1
C++ Private Inheritance
Hi Experts,
If I have a class derived from the base class using private keyword(private inheritance), then what are the members of the base class I can access from the derived class.
I read from a book that incase of private inheritance all the data members of the base class become private in the derived class. if above is true then following code should not compile.
#include <iostream>
using namespace std;
class Base {
public:
int j;
void display1() {
cout << "Value of i = " << i << endl;
}
private:
int i;
protected:
int k;
};
class Derived_public: private Base {
public:
void display() {
display1();
cout << "Derived_public: protected data = " << k << endl;
cout << "Derived_public: public data = " << j << endl;
}
};
int main() {
Derived_public d1;
d1.display();
return 0;
}
+++++++++++++++++++++++++++++++++++++
But the above code compiles and I can access public and protected data members of base class.
Is this the normal behaviour..Please help..
Thanks and Regards,
Paresh
-
July 22nd, 2007, 12:29 AM
#2
Re: C++ Private Inheritance
Derive another class from Derived_public and try to access Base's public stuff; then change Derived_public back to public inheritence and see if your new class can access Base's public.
-
July 22nd, 2007, 12:34 AM
#3
Re: C++ Private Inheritance
or, for that matter, try calling d1.display1();
then change it to public inheritance and try calling d1.display1();
-
July 22nd, 2007, 01:08 AM
#4
Re: C++ Private Inheritance
Thanks for your reply.
================================================
or, for that matter, try calling d1.display1();
then change it to public inheritance and try calling d1.display1();
================================================
Yes, I agree that it will be compilation error if I try to call d1.display1(), Since it is a private inheritance one cannot access Base class members from the main function using derived class objects.(I got compile time error while calling d1.display1() from main() )
But I thought same should have been true within the Derived class. I thought access to any member of Base class from Derived class should have been compile time error. If at all I wanted to use the base class member I should have used "using" keyword in the public part of the derived class. But in this case "using" keyword is not required..
Thanks,
-
July 22nd, 2007, 01:12 AM
#5
Re: C++ Private Inheritance
Also, If private inheritance behaves this way, then I donot see any difference between private and protected inheritance.
-
July 22nd, 2007, 01:32 AM
#6
Re: C++ Private Inheritance
But I thought same should have been true within the Derived class. I thought access to any member of Base class from Derived class should have been compile time error. If at all I wanted to use the base class member I should have used "using" keyword in the public part of the derived class. But in this case "using" keyword is not required..
What happens is that the public and protected members of the base class become private members of the derived class. As such, the derived class can access them, because they are not private members from the base class, but effectively its own private members. The using keyword would be used to bring names from the base class into the scope of the derived class, but this is not the issue here.
Also, If private inheritance behaves this way, then I donot see any difference between private and protected inheritance.
Private inheritance is used to implement a class in terms of another class. I suppose protected inheritance could be used for the same thing, except that classes that derive from the derived class would have access to those members originally from the base class.
-
July 22nd, 2007, 02:01 AM
#7
Re: C++ Private Inheritance
Thanks a lot for the clarification. I think i got iy now... Using keyword is used in the derived class to publicize the base class members so that object of derived class can access that publicized member.
One more question...When we derive with public keyword from the Base class, private part of the base doesnot become private part of derived class??
-
July 22nd, 2007, 02:09 AM
#8
Re: C++ Private Inheritance
Using keyword is used in the derived class to publicize the base class members so that object of derived class can access that publicized member.
No, it is to bring the base class members into scope if they are otherwise hidden by a derived class member of the same name. Take for example:
Code:
class Base
{
public:
void foo()
{
std::cout << "Base::foo()" << std::endl;
}
};
class Derived : public Base
{
public:
void foo(int num)
{
std::cout << "Derived::foo(" << num << ")" << std::endl;
}
};
We would expect that we can write:
Code:
Derived obj;
obj.foo();
However, this is not the case, since the foo(int) member function in Derived has caused the foo() member function inherited from Base to be hidden. To solve this, we can bring Base::foo into scope in Derived:
Code:
class Derived : public Base
{
public:
using Base::foo;
void foo(int num)
{
std::cout << "Derived::foo(" << num << ")" << std::endl;
}
};
Now we can call both obj.foo() and obj.foo(2).
When we derive with public keyword from the Base class, private part of the base doesnot become private part of derived class?
It does not become a private member of the derived class, but is instead hidden from access.
-
July 22nd, 2007, 02:32 AM
#9
Re: C++ Private Inheritance
Yes..you are right . But also incase of private inheritance if you want to access the base class member from the derived class object then ..using keyword in the public part of the derived class helps.
Below text is from BruceEckel:----------------------------------------
--------------------------------------------------------------------------------
Publicizing privately inherited members:
When you inherit privately, all the public members of the base
class become private. If you want any of them to be visible, just say
their names (no arguments or return values) in the public section of
the derived class:
//: C14:PrivateInheritance.cpp
class Pet {
public:
char eat() const { return 'a'; }
int speak() const { return 2; }
float sleep() const { return 3.0; }
float sleep(int) const { return 4.0; }
};
class Goldfish : Pet { // Private inheritance
public:
Pet::eat; // Name publicizes member
Pet::sleep; // Both overloaded members exposed
};
int main() {
Goldfish bob;
bob.eat();
bob.sleep();
bob.sleep(1);
//! bob.speak();// Error: private member function
} ///:~
-
July 22nd, 2007, 05:45 AM
#10
Re: C++ Private Inheritance
Originally Posted by paresh@codeguru
But I thought same should have been true within the Derived class. I thought access to any member of Base class from Derived class should have been compile time error.
No, in the same way that your class can access its own private data members, it can also access its own private base class.
Consider:
Code:
class Base
{
public:
void f();
int x;
private:
void g();
int y;
};
class Derived : private Base
{
public:
void h()
{
// can access f and x but not g and y !
}
};
class Also
{
public:
void h()
{
// can access o.f and o.x but not o.g and o.y !
}
private:
Base o;
};
Last edited by Zaccheus; July 22nd, 2007 at 05:50 AM.
-
July 22nd, 2007, 08:26 AM
#11
Re: C++ Private Inheritance
For the immediate derived class, there is no difference between public, protected and private inheritance, just as there is no difference between public, protected and private members. The difference is only apparent to users of the class - that is, only further derived classes can access a base class with protected inheritance, and no-one else (apart from friends) can access a privately inherited base class.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
July 22nd, 2007, 08:29 AM
#12
Re: C++ Private Inheritance
Thanks a lot to all on the forum. Definitely your reply's have helped understand Private/Protected inheritance in a much simpler way.
-
July 22nd, 2007, 08:40 AM
#13
Re: C++ Private Inheritance
Yes, Graham...After the above replys..I wrote the code just to verify this concepts and have a understood it. Need to check the the access for the friend functions/classes incase of private and protected inheritance.
Your way of seeing the inheritance is better way to remember the concept.
Thanks for that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|