简体   繁体   中英

C++ inheritance: protected variables not available

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.

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 .

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM