473,770 Members | 4,198 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

very intersting:deri ved 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 2688
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
3945
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 private members of nesting class. template <typename T> class Container { // NO friendship given to any other public: class ContainerIterator;
3
2351
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 class definition. Therefore I can't specify the return type of the function, that returns a pointer to the derived class, because the derived class is not yet known at that point. Hov can I solve this problem? In a nice manner :)
4
2993
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 Derived());
6
6055
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 problem is that in my derived classes, the XML that is automatically generated is lacking the properties of the base class. For example: public class MyBaseClass { public MyBaseClass ( ) { } private string myVariable;
15
2159
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 have an attribute which can be applied to a class definition of a class which inherits from a base, mustinherit class. I want to define methods in the base class which will access the contents of the attribute as it is applied to
2
2131
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 general, abstract collection class that implements ITypedList and IBindingList When the program execute, the ITypedList.GetItemProperties method is
15
3852
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* populate the variable, but the population is not *required* by the derived class (which must be the case). What would meet the requirements is if I create an abstract method in the base class that populates the member variable. In this case the...
32
2087
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
8161
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 situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is common for subclasses but the members are common within subclass only (static member of subclass) but...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10231
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6679
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.