Overriding Virtual Function clarification...

A

Achintya

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
#include<iostream>
using namespace std;

class A
{
int i;
virtual void f(){ cout<<endl<<"i= "<<i; }
public:
A(int j) { i = j; }
};

class B : public A
{
int j;
virtual void g() { cout<<endl<<"j= "<<j; }
public:
virtual void f(){ cout<<"I'm in B::f()"; }
B(int k):A(k+10){ j = k; }

};

int main()
{
A* a_base;
B* b_derived;

a_base = new B(1);
a_base->f();
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Hi,

In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn't tried this earlier.

-praveen.
 
T

Tobias Blomkvist

In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn't tried this earlier.

-praveen.

Private base class member functions are not accessable to derived
classes, try to make them protected instead.

Tobias
 
A

Achintya

Tobias said:
Private base class member functions are not accessable to derived
classes, try to make them protected instead.

Tobias

Hi,

Thanks..Thats fine, I tried that before posting here and was
successful. but I am interested in reason behind...

-praveen
 
R

Rob Williscroft

Achintya wrote in @g43g2000cwa.googlegroups.com in comp.lang.c++:
int main()
{
A* a_base;
B* b_derived;

a_base = new B(1);
a_base->f();
}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

Hi,

In the above code the virtual function declaration in A is causing a
compiler error since its private. Does this mean that the virtual
function overring requires the base delaration also to be public.
Kindly explain...as i hadn't tried this earlier.

No, public is required as you call A::f() from main(), i.e. a
context where public access to A::f() is required.

When you make a call to A::f() the compiler doesn't care
wether A::f() is virtual, or if its been overriden, it only
checks the accessability of A::f().

The fact that in this case a_base actually points to a B with
an accessable f() doesen't matter. Type and access checking is
done by the compiler at compile time, not at runtime, and in
general the compiler doesn't know the dynamic type (B - above)
that a pointer (A *a_base - above) points too.

HTH.

Rob.
 
T

Tobias Blomkvist

Hi,
Thanks..Thats fine, I tried that before posting here and was
successful. but I am interested in reason behind...

-praveen

Everything declared private is supposed to be private, unaccessible to
others. Protected means that it is still shielded from outside access,
but accessible to derived classes. These are design options provided to
separate intentional functionality, thus making the interface clearer.

Additionally you can affect the inherited access level by deriving
with public, protected and private:

class x1 : public y {};
class x2 : protected y {};
class x3 : private y {};

Private is the default base class access specifier:

// private access, no of y's functions will be accessible to x4
class x4 : y {};

Tobias
 
G

Girish Shetty

#include said:
using namespace std;

class A
{
int i;
virtual void f(){ cout<<endl<<"i= "<<i; }
public:
A(int j) { i = j; }
};

class B : public A
{
int j;
virtual void g() { cout<<endl<<"j= "<<j; }
public:
virtual void f(){ cout<<"I'm in B::f()"; }
B(int k):A(k+10){ j = k; }

};

The problem is, you are trying to change the access specifier of Base class
private function to public and trying to access it from mail using
polymorphism.
In C++, we can not increase the access of a variable/function from lower
level to upper level (example from private to public)

But, if your base class f() was public, then you can change your derived
class overloaded f()'s access specifier as private.

Now when you call your Derived function f() using base pointer, it does not
give you any error.

Regards
Girish
 
A

Alf P. Steinbach

* Tobias Blomkvist:
Private base class member functions are not accessable to derived
classes, try to make them protected instead.

Private virtual member functions can be overridden.

The problem in the OP's code is not that, but that he's trying to access a
member function through a pointer to a class where that function is private.
 
A

Alf P. Steinbach

* Girish Shetty:
The problem is, you are trying to change the access specifier of Base class
private function to public and trying to access it from mail using
polymorphism.
In C++, we can not increase the access of a variable/function from lower
level to upper level

Sorry, that's incorrect.

(example from private to public)

Right, the function must be accessible to do anything except overriding it.

Although, if the private function is virtual it can be overridden by a
forwarder function, which you can give any access specifier you want.
 
A

Alf P. Steinbach

* Alf P. Steinbach:
Although, if the private function is virtual it can be overridden by a
forwarder function, which you can give any access specifier you want.

Strike that. It can be overridden but of course not forwarded to. *banging
head against keyboard, consuming several litres of coffee to wake up*
 
A

Achintya

Hi,

Thanks all for dicussing this topic. Now I can reason out the cause.

Anybody can suggest good resource on net on how VTABLEs work?

-vs_p...
 
A

Achintya

Hi,

Thanks all for dicussing this topic. Now I can reason out the cause.

Anybody can suggest good resource on net on how VTABLEs work?

-vs_p...
 

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
473,776
Messages
2,569,603
Members
45,186
Latest member
vinaykumar_nevatia

Latest Threads

Top