473,387 Members | 1,290 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

very intersting:derived class private member accessed???

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
3 2660
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
3
by: Teis Draiby | last post by:
I want to write a base class that includes a member function that creates an instance of a derrived class and returns a pointer to it. Problem: The derived class definition has to follow the base...
4
by: Siemel Naran | last post by:
Can Derived class static member access protected member from base class? class Base { protected: void setvariable(int); }; class Derived : Base { public: static std::auto_ptr<Base> out(new...
6
by: John Glover | last post by:
I'm having a very strange problem with XML serialization. I'm writing web services which pass instances of various classes back and forth as parameters and return values of web methods. The...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
2
by: TEK | last post by:
Hello We're having a issue with a part of our app that is very strange, and I'm unable to see that it should be possible. We have: A collection (ActivityCollection) that is inherited from a...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
32
by: Immortal Nephi | last post by:
I want to know if the practice is the best. Do I need to place inline keyword inside class definition or outside member function definition. For example class A { public: A(); ~A();
6
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.