4

I've got the following C++ code in XCode, giving two errors I cannot make sense of:

#include <iostream>


class Buch{
public:
    Buch (long int nummer = 0, char autor[25] = (char*)"", int jahr = 0, bool ausgel = false);

    long int getNr();
    int getJahr();

protected:
    long int __nummer;
    char     __autor[25];
    int      __jahr;
    bool     __ausgel;

    void setAusl(bool x);
    bool getAusl();
};

class FachBuch : Buch {
public:
    void setSWort(int sw);
    int getSWort();

protected:
    char     __fach[15];
    int      __swort;
};

class UBuch : Buch {
public:
    void setAlter(int a);
    int getAlter();

protected:
    int      __kateg;
    char     __land[15];

private:
    int _ab_alter;
};

class Bildband : UBuch {
public:
    Bildband(long int nummer = 0, char autor[25] = (char*)"", int jahr = 0, int kategorie = 0, char land[15] = (char*)"");
};

Buch::Buch (long int nummer, char autor[25], int jahr, bool ausgel) {
    __nummer = nummer;
    //_autor = autor;
    __jahr = jahr;
    __ausgel = ausgel;
}

long int Buch::getNr() {
    return __nummer;
}

int Buch::getJahr() {
    return __jahr;
}

void Buch::setAusl(bool x) {
    __ausgel = x;
}

bool Buch::getAusl() {
    return __ausgel;
}


void FachBuch::setSWort(int x) {
    __swort = x;
}

int FachBuch::getSWort() {
    return __swort;
}


void UBuch::setAlter(int x) {
    _ab_alter = x;
}

int UBuch::getAlter() {
    return _ab_alter;
}

Bildband::Bildband(long int nummer, char autor[25], int jahr, int kategorie, char land[15]) {

    __nummer = nummer; // error message: Cannot cast 'Bildband' to its private base class 'Buch'

    //Buch(nummer, autor, jahr, false); // error message: '__nummer' is a private member of 'Buch'

}

int main () {
    Bildband Masuren(356780, (char*)"Kranz", 2010, 4, (char*)"Polen");
    return 0;
}

I get the following errors: main.cpp:92:5: Cannot cast 'Bildband' to its private base class 'Buch' main.cpp:92:5: '__nummer' is a private member of 'Buch'

My knowledge of C++ is quite limited and I had no luck googling, probably mainly because I lack the necessary C++ basic knowledge.

Can anyone explain to me why these errors occur and what terms I need to look up to understand the problem?

Thanks in advance.

SirSiggi
  • 143
  • 1
  • 1
  • 9

3 Answers3

9

They are not available because UBuch inherits Buch privately. When defining a class, inheritance is private by default.

// These two lines mean the same thing:
class UBuch : Buch
class UBuch : private Buch

All members of Buch are inherited, but those visible to UBuch are inherited as private to UBuch.

This means that code external to UBuch has no access to any members of Buch on UBuch objects, nor can it convert pointers or references to UBuch objects to pointers or references to Buch.

This includes classes that derive UBuch.

class Bildband : UBuch

Even though Buch is in the inheritance chain, its members were inherited privately by UBuch, and so Bildband has no access to the members inherited from Buch.

To fix this, you should inherit Buch publicly (and you probably want all of your other classes to inherit publicly from their respective base classes, too):

class UBuch : public Buch

Also, note that identifiers containing two consecutive underscores are reserved by the environment, so all such identifiers in your code (__nummer, __autor, etc.) cause undefined behavior.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
1

If you don't define an access-specifier, the compiler will default to private inheritance for classes. Simply prefix your UBuch class and Buch class with "public" like so:

// ...
class UBuch : public Buch {
// ...
class Bildband : public UBuch {

I assume "public" here, since I guess that you want to provide access to the methods like getNr / getJahr from users of BildBand as well.

./regards Florian

fschaper
  • 721
  • 7
  • 10
1

EDIT: See comments below

The type of inheritance effects which members are inherited:

public inheritance means only public members are inherited.

protected inheritance means public members are inherited and protected members are inherited as protected as well.

private inheritance means public members are inherited and protected members are inherited as private.

Here you are inheriting privately by default.

user3126802
  • 419
  • 8
  • 19
  • No, public inheritance means that **all** members are inherited. – cdhowie Feb 02 '15 at 20:56
  • 1
    private members are never inherited. protected members are not inherited by public – user3126802 Feb 02 '15 at 20:58
  • They are inherited, but not visible to the deriving class (otherwise they would not exist). And even if I grant that, `public` also causes `protected` members to be inherited as `protected`, otherwise further-derived classes could not use a grandparent's `protected` members. – cdhowie Feb 02 '15 at 20:59
  • To put it another way, a class always inherits all members from its parent classes, but does not inherit *access* to the private ones. – cdhowie Feb 02 '15 at 21:03
  • 1
    You are right! Thanks for correcting me! Sorry for the confusion! I'll edit the answer – user3126802 Feb 02 '15 at 21:03
  • 1
    I believe the following link summarizes the answer quite well: http://www.programiz.com/cpp-programming/public-protected-private-inheritance – user3126802 Feb 02 '15 at 21:08