By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

very intersting:derived class private member accessed???

P: n/a
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

Jun 23 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Bangalore wrote:
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-lit....html#faq-21.1

Cheers! --M

Jun 23 '06 #2

P: n/a

Bangalore wrote:
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-

Jun 23 '06 #3

P: n/a

Bangalore wrote:
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

Jun 23 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.