473,848 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Private Members from Main

Hi,

Please take a look at the code snippet given below:#include< iostream>
using namespace std;

class A
{
public:
virtual void test(){cout<<"D efault implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"D erived class called"<<endl;}

};
int main()
{
A *a = new B;
a->test();
return 1;
}

The output is :
Derived class called
Now, my question is how can I access a private function of a class from
main? I accept that I did it via dynamic polymorphism. Still, should I
be allowed to access a member function which is private?

Thanks & Regards
Ambar

Jul 28 '05 #1
4 3040
Please take a look at the code snippet given below:#include< iostream>
using namespace std;

class A
{
public:
virtual void test(){cout<<"D efault implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"D erived class called"<<endl;}

};
int main()
{
A *a = new B;
a->test();
return 1;
}

The output is :
Derived class called
Now, my question is how can I access a private function of a class from
main? I accept that I did it via dynamic polymorphism. Still, should I
be allowed to access a member function which is private?


There are two answers for your question. One is how the method gets
called? The accesibility of a method is defined through the static type
of the pointer used for calling the method. So in your case, since the
dynamic type of 'A* a' is 'B' and static type is 'A'. The static type
'A' is used to define the accesibility of the method. And hence this
call is perfectly legal.
Should it be allowed or is it breaking encapsulation? The answer to this
question is that when you derive your class from 'A' you are actually
abiding by the constraints of 'A'. If you dont want your method to be
visible outside why are you overloading the same method defined in 'A'.
You can anyway create a new method name and can do your job. When you
are overloading a method from 'A', you are actually want to overload
test() and want to use the polymorphism.
So, from language point of view this is justified. It is a design
issue. You should avoid overloading the test() method if you dont want
it to be called.

-Shantanu Garg

Jul 28 '05 #2
Ian
am*********@gma il.com wrote:
Hi,

Please take a look at the code snippet given below:#include< iostream>
using namespace std;

class A
{
public:
virtual void test(){cout<<"D efault implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"D erived class called"<<endl;}

};
int main()
{
A *a = new B;
a->test();
return 1;
}

The output is :
Derived class called
Now, my question is how can I access a private function of a class from
main? I accept that I did it via dynamic polymorphism. Still, should I
be allowed to access a member function which is private?

No, there is nothing special about main in this context.

You have to make it a friend.

Ian
Jul 28 '05 #3


am*********@gma il.com skrev:
Hi,

Please take a look at the code snippet given below:#include< iostream>
using namespace std;

class A
{
public:
virtual void test(){cout<<"D efault implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"D erived class called"<<endl;}

};
int main()
{
A *a = new B;
a->test();
return 1;
}

The output is :
Derived class called
Now, my question is how can I access a private function of a class from
main? I accept that I did it via dynamic polymorphism. Still, should I
be allowed to access a member function which is private?
Yes. Nothing prevents anyone to access private variables when access is
granted. And you gave access to test in B, where the function is
public.

It is normally not a good idea to change access-level on virtual
functions which is what you just did.

Thanks & Regards
Ambar


/Peter

Jul 28 '05 #4
am*********@gma il.com writes:
class A
{
public:
virtual void test(){cout<<"D efault implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"D erived class called"<<endl;}

};
int main()
{
A *a = new B;
a->test();
return 1;
}

The output is :
Derived class called
Now, my question is how can I access a private function of a class from
main? I accept that I did it via dynamic polymorphism. Still, should I
be allowed to access a member function which is private?


No. But you shouldn't be allowed to use public inheritance to modell
non is-a relations either.

Puting the standardese arguments aside, consider this from the design
perspective. You tries to restrict the base class interface in the
derived class. Making them non compattible. I.e., if you write,

int main()
{
B *b = new B;
b->test();
return 1;
}

you get a compiler error. But with the line

class B: public A

you are telling the compiler that class B is compattible with class
A. And the poor thing belives this. So it tries its best to find a
matching method in class B, ending with calling your private method.

Regards

ImRe
Jul 28 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3660
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
5
2420
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
2
2318
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
6
2630
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << endl; } }; class Drive : public Base1{
4
1923
by: Amit Limaye | last post by:
Hello, i have a class with around 10-15 properties and i want to write a single set get pair to access all these properties. Any suggestions. BTW this is not some question for an interview. I was thinking of implementing this in some of the code i have been writing. I was asked this question a couple of years ago and i jumped onto member offsets since the class is laid out as a structure. But tht thing rips apart everything C++/OOPS...
4
1428
by: John | last post by:
I am having a problem accessing the members in a structure via an interface and Class. ***Development Code*** Structure Person Public Name as String End Structure Interface IData Public Property Employee As Person
6
5037
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
2
2605
by: mickey22 | last post by:
Hi all, I have some data members and member functions that are declared as private in a class A. Now I want to use these data members and functions(which are private) in my new C++ source file(.cpp file).I have created pointer to this class A, and tried to use the functions but I am getting an errror that: cannot access private data member. Could anyone please help me with this?Thanks Example : ///abc.cpp file (my source file)
2
1611
by: agendum97 | last post by:
Is there a way of constructing a "class" in JavaScript to access a private member of a different scope of the same type? It is important that the variable remain private, and not accessible in anyway publically. For example: function SomeObj(privateKey) { this.process = function(someObj) { if (!(someObj instanceof SomeObj))
0
9892
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9735
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
10997
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
10718
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
10347
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...
1
7889
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7067
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
5731
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3172
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.