C++ inheritance

Started by
5 comments, last by Zahlman 14 years, 7 months ago
Well I've been doing C++ for a while, but I keep getting hung up on inheritance. I can do the basic things, but there is some I don't know, and some I'm sure I've learned wrong. First off, inheriting variables. I have tried using all three settings (public, private, protected), but I can only use public ones in the sub-classes. I thought protected could be inherited as well? The problem is that I want most variables to stay private, however if I plan on having them inherited I have to make them public. I know there is a way to get around this, but google couldn't help. Secondly are virtual destructors. Before I go about using them I just want to ask, does it only execute the one destructor or does it go up the classes executing them? For instance (incomplete code)


class ObjectA
{
    private:
        int* A;

    public:
        virtual ~ObjectA()
        {
            delete A;
        }
};

class ObjectB: public ObjectA
{
    private:
        int* B;

    public:
        virtual ~ObjectB()
        {
            delete B;
        }
};

When ObjectB is destroyed, is the base ObjectA (and therefore A) destroyed as well?
Advertisement
Quote:Original post by SomeoneX
First off, inheriting variables. I have tried using all three settings (public, private, protected), but I can only use public ones in the sub-classes. I thought protected could be inherited as well? The problem is that I want most variables to stay private, however if I plan on having them inherited I have to make them public. I know there is a way to get around this, but google couldn't help.

Post specific examples that you're having trouble with.

Quote:
When ObjectB is destroyed, is the base ObjectA (and therefore A) destroyed as well?

Yes.
Quote:Original post by SomeoneX
First off, inheriting variables. I have tried using all three settings (public, private, protected), but I can only use public ones in the sub-classes. I thought protected could be inherited as well? The problem is that I want most variables to stay private, however if I plan on having them inherited I have to make them public. I know there is a way to get around this, but google couldn't help.


You can use protected members. Variables are always inherited, anyway, even if they are inaccessible in the derived class.

Show some code illustrating your problem.

Quote:Secondly are virtual destructors.... When ObjectB is destroyed, is the base ObjectA (and therefore A) destroyed as well?


Yes. However, you should try not to rely on your own destructors for memory management. Use wrappers that accomplish what you need them to. Smart pointers and standard library containers are your friends. Normally you can simplify things to the point that you just define a do-nothing, virtual base destructor, and don't bother mentioning a destructor in derived classes (since the automatically-generated default destructor will do what is needed, and use virtual dispatch due to the once-virtual-always-virtual rule).
I don't have the code for the original private variable inheritance problem, but I was able to duplicate it.

Here is the code

class ObjectA{	private:		int a;	public:		ObjectA() {a = 10;}		virtual ~ObjectA() {};};class ObjectB: public ObjectA{	private:		int b;	public:		ObjectB() {b = 10; a = 5};		virtual ~ObjectB() {};};int main(){	ObjectA oa;	ObjectB ob;}


I am using g++ 4.3.3, and the errors I received are:

test.cpp: In constructor ‘ObjectB::ObjectB()’:
test.cpp:4: error: ‘int ObjectA::a’ is private
test.cpp:16: error: within this context
test.cpp:16: error: expected `;' before ‘}’ token
That's what should happen. A derived class shouldn't be able to access private members of the base class.
I know that. I just tried protected and.... it worked?
I clearly remember it failing when I used it in another project. Looks like I should double-attempt some things before asking...
Let me just confirm with you guys: protected allows sub-classes access, but only them, right?
That's all the 'protected' keyword allows; but you can also grant access on an individual basis with the 'friend' keyword (even to 'private') data. This should, of course, be used sparingly and only with a good reason.

This topic is closed to new replies.

Advertisement