473,326 Members | 2,128 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,326 software developers and data experts.

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<<"Default implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"Derived 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 3015
Please take a look at the code snippet given below:#include<iostream>
using namespace std;

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

};

class B:public A
{
private:
void test(){cout<<"Derived 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*********@gmail.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<<"Default implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"Derived 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*********@gmail.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<<"Default implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"Derived 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*********@gmail.com writes:
class A
{
public:
virtual void test(){cout<<"Default implementation called"<<endl;}

};

class B:public A
{
private:
void test(){cout<<"Derived 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
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...
5
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
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...
6
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" <<...
4
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...
4
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...
6
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
2
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...
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.