In the following code, Circle
is a public inheritance of Point
, in which the private members x
, y
of Point
is inaccessible. But while assigning Circle c
to Point p
, I found p
can actually get the right value for x
and y
. Why is this happening?
#include<iostream>
using namespace std;
class Point {
public: Point(float x = 0, float y = 0);
void display();
private:
float x, y;
};
Point::Point(float a, float b)
{
x = a; y = b;
}
void Point::display() {
cout << x <<'\t'<< y << endl;
}
class Circle :public Point
{
public: Circle(float x = 0, float y = 0, float r = 0);
private:
float radius;
};
Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) {}
int main()
{
Circle c(10, 10, 15);
Point p = c;
p.display();
return 0;
}
with the output:
10 10