Доступ к переменным класса родителя C++
Только недавно начал изучать ООП. Решил создать 2 класса: класс точки и производный класс линии. Вот что получилось:
class point
{
protected:
int x;
int y;
public:
point ( int a, int b ): x(a), y(b) {}
point () { x=0; y=0; }
};
class line : public point
{
private:
point a;
point b;
public:
line ( point aa, point bb ): a(aa), b(bb) {};
float len ()
{
...
}
};
Не понимаю как получить доступ к переменным класса-родителя чтобы вычислить длину отрезка. Хотелось бы чтобы вместо пропуска был код, похожий на этот:
int dx = abs(a.x - b.x);
int dy = abs(a.y - b.y);
float l = sqrt(dx*dx + dy*dy);
return l;
При подстановке данного кода на место пропуска получается
user@user:~$ g++ oop.cpp
oop.cpp: In member function ‘float line::len()’:
oop.cpp:23:28: error: ‘int point::x’ is protected within this context
23 | int dx = abs(a.x - b.x);
| ^
oop.cpp:7:13: note: declared protected here
7 | int x;
| ^
oop.cpp:23:34: error: ‘int point::x’ is protected within this context
23 | int dx = abs(a.x - b.x);
| ^
oop.cpp:7:13: note: declared protected here
7 | int x;
| ^
oop.cpp:24:28: error: ‘int point::y’ is protected within this context
24 | int dy = abs(a.y - b.y);
| ^
oop.cpp:8:13: note: declared protected here
8 | int y;
| ^
oop.cpp:24:34: error: ‘int point::y’ is protected within this context
24 | int dy = abs(a.y - b.y);
| ^
oop.cpp:8:13: note: declared protected here
8 | int y;
| ^