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

Overriding Virtual Function clarification...

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& &&&&&&&&&&&&&&&
#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.

Jul 23 '05 #1
11 1756
>
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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 23 '05 #2


Tobias Blomkvist wrote:

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


Hi,

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

-praveen --
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.


Jul 23 '05 #3
Achintya wrote in news:1122128095.067742.271170
@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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #4
> 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
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 23 '05 #5
> #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; }

};


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
Jul 25 '05 #6
* 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.


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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #7
* Girish Shetty:
#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; }

};

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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #8
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 25 '05 #9
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...

Alf P. Steinbach wrote:
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 25 '05 #10
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...

Alf P. Steinbach wrote:
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Jul 25 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Edward Diener | last post by:
Is it possible for a derived class to override a property and/or event of its base class ?
5
by: Hongzheng Wang | last post by:
Hi, I have a problem about the overriding of private methods of base class. That is, if a method f() of base class is private, can the derived class overriding f() be overriding? For...
11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
15
by: Susan Baker | last post by:
Hello everybody, I'm new to C++ (I have some C background). I've read up on this topic a few times but it just dosen't seem to be sinking in. 1. Whats the difference between overloading and...
3
by: David Scarlett | last post by:
Hi all, I've got a question regarding overriding const member functions with non-const functions. Let's say I've got a base class defined as follows: /*******************/ class Foo {...
13
by: ctor | last post by:
Hi, I'm experiencing an annoying issue. Here is a simplified idea of what I am trying to do. Inclusion guards aren't shown for readability. I hope this isn't too confusing; I don't think a...
3
by: a | last post by:
Hi, I need clarification for virtual method and pure virtual method. e.g Class Base{ virtual void func(){ ---- } } Class Child : public Base{ void func()
3
by: Rick | last post by:
One of the rules I have recommended for our new coding standard is as follows: "When overriding a base class virtual function, all overloads of that base class virtual function should also be...
12
by: danil52 | last post by:
Hello there, I have the following code: class Base { public: virtual void f() {cout << "Base::f()" << endl;} virtual void f(int) {cout << "Base::f(int)" << endl;} };
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: 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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.