By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,922 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,922 IT Pros & Developers. It's quick & easy.

Accessing Private Members from Main

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.