#include <iostream>
usingnamespace std;
class BaseClass{
public:
BaseClass():nValue(0){}
void inc(){
nValue++;
}
void print(){
cout << "\nnValue of BaseClass object = " << nValue << ".\n";
}
protected:
int nValue;
};
class SubClass:BaseClass{
public:
SubClass(){
nValue = 1;
}
private:
int nValue2;
};
int main()
{
BaseClass x1;
x1.inc();
x1.print();
SubClass x2;
x2.inc();
x2.print();
return 0;
}
I've just finished learning the basics about inheritance and thought I'd have a little play around. The above program returns the errors:
"error: 'void BaseClass::inc()' is inaccessible"
"error: within this context"
"error: 'BaseClass' is not an accessible base of 'SubClass'".
(and the same for the print() function)
This confuses me since I thought the whole idea of inheritance is that the functions & data of a base class can be used in its subclasses as if it were contained in the subclass.