in inheritence concept overridden concept is there ,there i faced a problem i.e child class object is assigned to the parent class reference .so we r accessing the methods with same signature in parent class and child class those only we r accessing. but we r unable acess the other memberes of child class. actually my doubt is in memory point of view we r considered the what we r access the child class memberes by using parent class reference those r also same memory location why can't we r access the other methods of child class
for example
class A //parent class
{
public void add() //method
{
statments;
}
class B:A //inheriting
{
public void add() //assume overridden method we r access only these method only from parent class reference
{
statements;
}
public void sub() //unable to access the method through parent class reference why?add and sub methods r stored in same memory location but we r access only add method why not sub method
{
statements;
}
}
please help me the answer for my question.
Loading

Sam HobbsPosted Aug 5, 2010, 11:32 AM
I really, really don't understand "we r considered the what we r access the child class memberes".
I thik you are saying that you want to execute the sub function from an instance of the A class and instead of explaining why that won't work, the answer is that it just won't work; you cannot do that. You can make sub a virtual function in the A class and then you probably can do what you need to do.
Jaish MathewsPosted Aug 5, 2010, 3:11 AM
bhanu prasannakumarPosted Aug 5, 2010, 2:47 AM
Jaish MathewsPosted Jul 20, 2010, 3:40 PM
Sub() can not access from parent. It's following below OOPS theory
Child classes may access parent members, but parent classes can not access child members.
In real world example, Teacher is base class and Maths teacher is sub class. Maths Teacher will get all behaviour of base class Teacher besides specialized in Maths. But it's meaningless to say base class Teacher will get all behaviour of a Maths Teacher.