I'm new to OOP. I recently learned some things about Inheritence in C++, and that a protected field can't be accessed from outside of the class, but it can be accessed in inherited classes. I have some problems with my code and I don't understand what is wrong:
class Base
{
protected:
int x;
int y;
int z;
public:
Base(int a, int b): x(a), y(b) { cout << "Base constructor" << endl; };
Base(const Base& prev) : x(prev.x), y(prev.y) { cout << "Base copy constructor" << endl; };
void print_x()
{
cout << x << endl;
}
void print_y()
{
cout << y << endl;
}
~Base() { cout << "Base destructor" << endl; };
};
class Derived : public Base
{
public:
Derived(int a, int b): x(a), y(b) { cout << "Derived constructor" << endl; }; ////////ERROR : Class
'Derived' does not have any fields named 'x' and 'y'
};
int main()
{
Base* b = new Base(10, 20);
Derived* d = new Derived(10, 20);
delete b;
delete d;
return 0;
}
If Derived inherits Base, why does the compiler say that Derived doesn't have the fields x and y?