Sign in to follow this  

question on inheretance in C++

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

if i define a class like this class A{ private: int a; public: int b; protected: int c; }; now when i like to create a class that inherits from class A i write a class like this class B:public A{ }; then class B inherits the public and proctected attributes of class A. But what if i write my class B like this? 1.) class B:private A{ }; or maybe something like this 2.) class B:protected A{ }; what attributes are inherited in class B using class declaration in 1. and also in 2.? or can somebody just explain to me the use of access modifiers when writing a class that inherits from a super class.. thanks peace all rulz

Share this post


Link to post
Share on other sites
Quote:
Original post by yuppies

class A{

private:
int a;
public: int b;

protected: int c;
};


1.)
class B : private A {

};


private inheritance makes all of the super-type's i.e A's public & protected members private in sub-types scope i.e B.

unrelated & related types cannot access A::a, A::b, A::c from type B. If you wont to make any public/proctected members of a super-type acessable to unrelated or sub-types of B then you use the using declaration in the appropriate place i.e. public or protected.

Private inheritance makes no is-a relationship and is equivalent to composition so it makes a has-a relationship i.e. B has-a A.

Quote:
Original post by yuppies
or maybe something like this
2.)
class B : protected A {

};


protected inheritance makes all public & proctected members of a super-type i.e A protected in the scope of sub-types i.e. B.

unrelated types can access public members of the super-type i.e. A's member b but cannot access it through B as its now a protected member of B again If you wont to make any public/proctected members of a super-type accessable to unrelated or sub-types of B then you use the using declaration in the appropriate place i.e. public or protected. related types (sub-types of B) automatically do have access.

in all cases private members of the super-type i.e A's private members are not accesssable to unrelated or related type in anyway (unless you make a friend-ship [smile]).

[Edited by - snk_kid on September 15, 2004 4:32:31 AM]

Share this post


Link to post
Share on other sites

This topic is 4842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this