very intersting:derived class private member accessed???

B

Bangalore

Hi,

In the following program, eventhogh two member function declared under
private section of the derived class are accessable by derived class
pointer.
Please clarify me how can derived class pointer acess private member
functions.

private member functions
#include <stdio.h>
#include <iostream>
using namespace std;


class Base
{

public :

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Base :: virFun1(int i)
{
cout <<" Base :: virFun1 ::
"<<i<<endl;

}

void Base :: virFun2(double d)
{
cout <<" Base :: virFun2 ::
"<<d<<endl;
}

class Derived : public Base
{
private : // PRIVATE ????????????

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Derived :: virFun1(int i)
{
cout <<" Derived :: virFun1 ::
"<<i<<endl;

}

void Derived :: virFun2(double d)
{
cout <<" Derived :: virFun2 ::
"<<d<<endl;
}


int main ()
{
Base *d = new Derived;
d -> virFun1 (10);
d -> virFun2 (10.10);
}

Thanks in advance
Bangalore
 
M

mlimber

Bangalore said:
Hi,

In the following program, eventhogh two member function declared under
private section of the derived class are accessable by derived class
pointer.
Please clarify me how can derived class pointer acess private member
functions.

private member functions
#include <stdio.h>
#include <iostream>
using namespace std;


class Base
{

public :

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Base :: virFun1(int i)
{
cout <<" Base :: virFun1 ::
"<<i<<endl;

}

void Base :: virFun2(double d)
{
cout <<" Base :: virFun2 ::
"<<d<<endl;
}

class Derived : public Base
{
private : // PRIVATE ????????????

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Derived :: virFun1(int i)
{
cout <<" Derived :: virFun1 ::
"<<i<<endl;

}

void Derived :: virFun2(double d)
{
cout <<" Derived :: virFun2 ::
"<<d<<endl;
}


int main ()
{
Base *d = new Derived;
d -> virFun1 (10);
d -> virFun2 (10.10);
}

Thanks in advance
Bangalore

See this FAQ:

http://www.parashift.com/c++-faq-lite/proper-inheritance.html#faq-21.1

Cheers! --M
 
S

Sabiyur

Bangalore said:
Hi,

In the following program, eventhogh two member function declared under
private section of the derived class are accessable by derived class
pointer.
Please clarify me how can derived class pointer acess private member
functions.

private member functions
#include <stdio.h>
#include <iostream>
using namespace std;


class Base
{

public :

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Base :: virFun1(int i)
{
cout <<" Base :: virFun1 ::
"<<i<<endl;

}

void Base :: virFun2(double d)
{
cout <<" Base :: virFun2 ::
"<<d<<endl;
}

class Derived : public Base
{
private : // PRIVATE ????????????

virtual void virFun1 (int i);
virtual void virFun2 (double d);
};


void Derived :: virFun1(int i)
{
cout <<" Derived :: virFun1 ::
"<<i<<endl;

}

void Derived :: virFun2(double d)
{
cout <<" Derived :: virFun2 ::
"<<d<<endl;
}


int main ()
{
Base *d = new Derived;
d -> virFun1 (10);
d -> virFun2 (10.10);
}

Thanks in advance
Bangalore

Hello,
In your example, Base class has virtual functions . At run time "d"
points to derived class. So it has all rights to access its
members.....

-Sabi-
 
W

werasm

Bangalore said:
Hi,

In the following program, eventhogh two member function declared under
private section of the derived class are accessable by derived class
pointer.

They are accessed via the base-class pointer. They are public in the
base class, therefore when using the base as interface (or accessing
them via base pointer), they should be public. Accessing them through
the derived pointer, this would not work.

class Base
{
public:
virtual void v1() = 0;
};
class Derived
{
public:
// ...constructor etc.
private:
//Note: v1 not private when accessed through baseclass reference
or pointer!
virtual void v1()
{
//...some code
};
}

Base* bp( new Derived() );
Derived* dp( new Derived() );

bp->v1(); //Ok, as public when accessed through base ptr
dp->v1(); //Error - private!
Please clarify me how can derived class pointer acess private member
functions.

You weren't using the derived pointer in your example, you were using
the base
private member functions
#include <stdio.h>

Use #include <cstdio> over here...

Kind regards,

Werner
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top