Does a pure virtual function (that is defined in a derived class) have access to the private members of the base class?

I thought pure virtual functions have complete access to the private members of the base class, since the prototype of the function is declared in the base class but then later defined in a derived class.

Please look at the example below to see what I mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//base class
#define PUREABSTRACTBASECLASS_H

using namespace std;

class BasicShape
{
    double area;

public:

    double getArea ()
    {
        return area;
    }

    virtual void calcArea () = 0;    //pure virtual function

};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Circle : public BasicShape
{

    long int centerX;
    long int centerY;
    double radius;

public:

    Circle (long int x, long int y, double r)
    {
        centerX = x;
        centerY = y;
        radius = r;
        calcArea ();
    }

    long int getCenterX ()
    {
        return centerX;
    }

    long int getCenterY ()
    {
        return centerY;
    }

    virtual void calcArea ()
    {
        area = (3.14159 * radius * radius);   //shouldn't this function have access to the private member area?
    }

};


p.s I know i can simply just declare the member area a protected member to make this work, but I want to find out whether or not a pure virtual function will have access to a private member.
Last edited on
[quote]//shouldn't this function have access to the private member area?[/quote
NO.
Its is not a member of the base class nor a friend of it. Its a function of the derived class that overrides that of the base class.
the area-variable would have to be protected, not private, as far as I know
Last edited on
Having a pure virtual method just means that the derived class MUST provide a definition for that function, but the inheritance still works like it would otherwise.
Topic archived. No new replies allowed.