0

Lets say I have 2 classes: foo and foo2 who are written as such:

foo.h:

#ifndef __InheritanceTest__foo__
#define __InheritanceTest__foo__

#include <stdio.h>
class foo
{
public:
    foo();
    int getSize();
protected:
    int size;

};
#endif

foo.cpp:

#include "foo.h"

foo::foo()
{
    size = 23;
}

int foo::getSize()
{
    return size;
}

foo2.h:

#ifndef __InheritanceTest__foo2__
#define __InheritanceTest__foo2__

#include <stdio.h>
#include "foo.h"
class foo2: foo
{
public:
    foo2();
};
#endif

foo2.cpp:

#include "foo2.h"

foo2::foo2()
{
    size = size *2;
}

this is my main:

#include <iostream>
#include "foo.h"
#include "foo2.h"

int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    foo2 f2;
    int i = f2.getSize();
    std::cout << i << "\n";

    return 0;
}

I'm getting two errors:

'getSize' is a private member of foo

and

cannot cast foo2 to its private base class foo.

Amre
  • 1,630
  • 8
  • 29
  • 41

2 Answers2

2

Inheritance through classes are private by default. This means inherited data members and member functions are private in the derived class and can only be accessed through itself or friends of the class.

Use the keyword public to specify public inheritance:

class foo2 : public foo
{ ... }

Note that this is unlike struct's which have public inheritance and public access for its data members and member functions.

David G
  • 94,763
  • 41
  • 167
  • 253
  • That worked, but I could have sworn I had added public at some point, and it failed to compile and I removed it. I must be imagining things. – Amre Oct 16 '14 at 01:46
1

Your foo2 declaration in foo2.h causes this problem:

...
class foo2: foo
{
...

In general:

In C++ there are inheritance access specifiers for each inherited class, just like there are member(function) access specifiers inside every class (private, protected, public). These specifiers decide what is the most open level, the base class' member(function)s will be available in the derived class.

Note that "most open" means the more strict of the inheritance access specifier and the member(function) access specifier:

public inheritance + public member(function) = public member(function)

public inheritance + protected member(function) = protected member(function)

public inheritance + private member(function) = private member(function)

protected inheritance + public member(function) = protected member(function)

protected inheritance + protected member(function) = protected member(function)

protected inheritance + private member(function) = private member(function)

private inheritance + public member(function) = private member(function) (your case)

private inheritance + protected member(function) = private member(function)

private inheritance + private member(function) = private member(function)

In your case:

The default inheritance access specifier is private, so foo is inherited privately here (which means every function and member inherited from foo, will be private in foo2). Because of this foo.getsize() becomes private from foo2's point of view and you can't call it.

Solution: Change line above to the following:

class foo2 : public foo

More about the topic: SO Question: C++ access specifiers

P.S.: When i use the term member(function) i mean both data members and member functions.

Community
  • 1
  • 1
Pregnor
  • 311
  • 1
  • 5
  • 8