1

I have a base class having a private, a protected and a public member. This base class is publicly derived.

class base
{
    int private_c;
protected:
    int protected_c;
public:
    int public_c;

    base()
    {
        private_c = protected_c = public_c = 0;
    }

    base(int private_c, int protected_c, int public_c)
    {
        this->private_c = private_c;
        this->protected_c = protected_c;
        this->public_c = public_c;
    }

    int get_privateC()
    {
        return(private_c);
    }

    int get_publicC()
    {
        return(public_c);
    }

    int get_protectedC()
    {
        return(protected_c);
    }

    virtual void func3(base *bp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << protected_c << ", public_c = " << public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->protected_c << ", public_c = " << bp->public_c << "\n";
    }
};

class derived: public base
{
    int dr_c;
public:
    derived():base(43, 73, 93)
    {

    }

    derived(int c, int d, int e):base(c, d, e)
    {

    }

    void func1(derived dp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << dp.get_privateC() << ", protected_c = " << dp.protected_c << ", public_c = " << dp.public_c << "\n\n";
    }

    void func2(derived *dp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
        cout << __FUNCTION__ "(): " << "private_c = " << dp->get_privateC() << ", protected_c = " << dp->protected_c << ", public_c = " << dp->public_c << "\n\n";
    }

    void func3(base *bp)
    {
        cout << __FUNCTION__ "(): " << "private_c = " << this->get_privateC() << ", protected_c = " << this->protected_c << ", public_c = " << this->public_c << "\n";
//      cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->protected_c << ", public_c = " << bp->public_c << "\n";
//      cout << __FUNCTION__ "(): " << "private_c = " << bp->get_privateC() << ", protected_c = " << bp->get_protectedC() << ", public_c = " << bp->public_c << "\n";
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    cout << __FUNCTION__ "()\n";

    derived dobj1(2,4,6), dobj2(3,5,7);

    derived *dptr1 = &dobj1;
    derived *dptr2 = &dobj2;

    base *bptr1 = &dobj1;
    base *bptr2 = &dobj2;

    dobj1.func1(dobj2);
    dptr1->func2(dptr2);
    bptr1->func3(bptr2);

    return(0);
}

This gives the output as:

wmain()
derived::func1(): private_c = 2, protected_c = 4, public_c = 6
derived::func1(): private_c = 3, protected_c = 5, public_c = 7

derived::func2(): private_c = 2, protected_c = 4, public_c = 6
derived::func2(): private_c = 3, protected_c = 5, public_c = 7

derived::func3(): private_c = 2, protected_c = 4, public_c = 6
Press any key to continue . . .

I have to access the protected members of two derived class objects in a derived function. For this I tried three functions func1(), func2() and func3().

If I use a function func1() passing derived object to it I am able to access the protected base class members OF THE PARAMETER. In function func2() I passed derived class pointer and was able to access the protected member OF THE PARAMETER.

But when I use base class pointer as parameter as in function func3() and try to access(by uncommenting the 2nd cout infunc3()) the protected member OF THE PARAMETER then it gives me compilation error as:

error C2248: 'base::protected_c' : cannot access protected member declared in class 'base'

I have to use(as seen in the 3rd cout statement of func3()) a public function get_protectedC() to access protected member.

Why am I not able to access the protected member directly in derived class using the base pointer?

Mateusz Grzejek
  • 11,698
  • 3
  • 32
  • 49
RKum
  • 758
  • 2
  • 12
  • 33

0 Answers0