-1

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
Kun Wu
  • 139
  • 6

6 Answers6

1
Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) {}

Circle derives from Point, so in the Circle constructor line above when you call:

Point(a, b)

It is initializing the base class Point with the passed in values a and b, who's constructor assigns them to x and y.

William_Wilson
  • 1,244
  • 7
  • 16
0

You do not have access to variables x and y, because they are private. But you have access to public method display(). display() is public method of class Point and have access to private variables.

In other words you can call the method display(), but can not directly access the private variables x and y.

0

Line Point p = c; casts Circle object to Point (actually, slices the object to Point) and invokes default assignment operator for Point class. Default assignment operator copies all values (and private data too) to new object.

see more Copy constructors, assignment operators, and exception safe assignment

Community
  • 1
  • 1
Stamerlan
  • 1
  • 2
0

You can't access private members via Derived class , When you call display() function (which belongs to Point class ) it can show x and y . When you assume Point p = c and invoking p.display() it still invoking Pointer class's method. Because Circle can see her parents' display() method. Make your display() method private and Circle can't invoke it.

Alexandr Sargsyan
  • 656
  • 1
  • 6
  • 21
0

The Point members are accessible "within" the Point subobject of a Circle.

Since you didn't define a copy constructor for Point, copying a Point behaves like this:

Point(const Point& p) : x(p.x), y(p.y) {}

and since Circle inherits from Point, c can be converted to const Point&, and thus Point p = c; copies the Point subobject of c.
This is known as "object slicing".

(On an unrelated note, "a circle is a kind of point" is a strange relationship.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

On this line

Point p = c;

You are calling copy constructor of Point:

Point::Point(const Point& p)

And because you can create reference to Base class from Derived class, your code is equivalent to:

Point &ref = c;
Point p = ref;

Now p will contain copy of Point part of Circle.

And because you initialized Point part of Circle object in Circle constructor like this:

Circle::Circle(float a, float b, float r) :Point(a, b), radius(r) {}

Point(a,b) constructor is called, which will initialize Point part of Circle with paramteters a and b.

PcAF
  • 1,975
  • 12
  • 20