Accessing Private Members from Main

A

ambar.shome

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
 
S

Shantanu Garg

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
 
I

Ian

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
 
P

peter.koch.larsen

(e-mail address removed) 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
 
I

Imre Palik

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top