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

another c++ inheretence/virtual quesion

Hi. I have a simple question, but I couldnt find the answer in any
literature online.

i have a base class A, that has some functions. now, my understanding
of virtual functions means that in:

class A {
virtual void foo() = 0;
void bar() {cout<<"bar"<<endl;}
};

class B : public A {
void foo() {cout<<"foo"<<endl;}
void bar() {cout<<"bar2"<<endl;}
};

every class B that derives class A *must* define foo or I get a linker
error.

also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2

What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!

oliver

Jul 23 '05 #1
17 1550
This is pretty much done in your example. Since you have bar() defined in
A, if it is not defined in B the version in the base class will be called
(provided all pure virtual functions are defined which you have done)

On Wed, 13 Apr 2005 20:46:26 -0700, laniik wrote:
Hi. I have a simple question, but I couldnt find the answer in any
literature online.

i have a base class A, that has some functions. now, my understanding
of virtual functions means that in:

class A {
virtual void foo() = 0;
void bar() {cout<<"bar"<<endl;}
};
class B : public A {
void foo() {cout<<"foo"<<endl;}
void bar() {cout<<"bar2"<<endl;}
};

every class B that derives class A *must* define foo or I get a linker
error.

also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2

What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!

oliver


Jul 23 '05 #2
thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"

Jul 23 '05 #3
What platform are you on?

On Wed, 13 Apr 2005 21:08:30 -0700, laniik wrote:
thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"


Jul 23 '05 #4
"laniik" <la****@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
Hi. I have a simple question, but I couldnt find the answer in any
literature online.

i have a base class A, that has some functions. now, my understanding
of virtual functions means that in:

class A {
virtual void foo() = 0;
void bar() {cout<<"bar"<<endl;}
};

class B : public A {
void foo() {cout<<"foo"<<endl;}
void bar() {cout<<"bar2"<<endl;}
};

every class B that derives class A *must* define foo or I get a linker
error.
No. You get a compiler error if you try to create an instance of A, or a
class derived directly or indirectly from A that does not provide or inherit
a non-pure override of A::foo.

You get a linker error if you call a function that you've declared but
haven't defined anywhere, whether it's virtual or not. You might or might
not get a linker error for declaring a non-pure virtual function that you
haven't defined, even if you don't call it. That probably depends on the
linker, but certainly the ones I've used would give an error.
also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2
What do you mean by 'it' in this statement? B::bar hides A::bar (and B::foo
hides A::foo for that matter). If you have a B, B& or B* and you call bar(),
then B::bar will be called.
What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!


That just happens naturally. That's why it's called *inheritance*. If a
given class doesn't have a particular member, then it will inherit it from
the nearest base class that has it. Try removing bar() from B, then create a
B object and call its bar(). A::bar will be called.

DW
Jul 23 '05 #5
I put basically your same code in from your top level message and it
worked fine:
#include <iostream>

using std::cout;

class A
{
public:
A(){}
virtual void foo()=0;
void bar()
{
cout<<"bar\n";
}
};

class B : public A
{
public:
B(){}
virtual void foo()
{
cout<<"Foo\n";
}
void bar()
{
cout<<"bar2\n";
}
};

int main(int argc, char **argv)
{
B b;
b.bar();

return 0;
}

results in "bar2" being printed.
On Wed, 13 Apr 2005 21:08:30 -0700, laniik wrote:
thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"


Jul 23 '05 #6
> also, if class B defines bar, it will call the bar from class A rather
than B. i.e. print out bar rather than bar2
No it won't.

A *pa = new B;
B *pb = new B;

pa->foo(); // will display "foo"
pa->bar(); // will display "bar"

pb->foo(); // will display "foo"
pb->bar(); // will display "bar2"
What i want is to have a function defined in A that if it isnt defined
in B, then it uses the one in A

if it is defined in B, then it uses the one in B. (but it doesnt
*have* to be defined) how do i do this? thanks!
It depends on what object type "it" is.
virtual member functions are dynamically bound.
non-virtual member functions depend on the type of the object.

Stephen Howe
oliver

Jul 23 '05 #7
> thats what I first thought, but when i compiled and tested it out,
calling

B b;
b.bar();

would print out "bar" rather than "bar2"


Something wrong then.
You should see "bar2".

Stephen Howe
Jul 23 '05 #8
i dont know what to say, im looking at it right now, its definitly
calling the function from the base class.

this is the code posted above.

i am using g++ on a solaris machine. any ideas?

Jul 23 '05 #9
found the bug: turns out you need to have bar declared virtual as
well...

Jul 23 '05 #10
"laniik" <la****@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
found the bug: turns out you need to have bar declared virtual as
well...


No you don't. The behaviour you have described is wrong, and it is hard to
believe that any reasonably tried and tested compiler could have such a
fundamental problem. I suggest that you post the complete and exact code
you've compiled and run by pasting it from the file you compiled into your
newsreader (the code you originally posted is not a complete program, nor
does it compile as is).

DW
Jul 23 '05 #11
class A {
public:
void go() {cout<<"1"<<endl;}
};

class B : public A{
public:
void go() {cout<<"2"<<endl;}
};

int main()
{
B b;
b.go();
return 0;
}

tried this in visual studio .net 2003, printed "2"
tried this on solaris (not sure what version) with g++ (also not sure
what version) printed "1"

Jul 23 '05 #12
"laniik" <la****@yahoo.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...
class A {
public:
void go() {cout<<"1"<<endl;}
};

class B : public A{
public:
void go() {cout<<"2"<<endl;}
};

int main()
{
B b;
b.go();
return 0;
}
This still isn't complete and can't be what you compiled. You didn't
#include <iostream> and you haven't got any std::. It probably doesn't
matter, assuming that all that's missing from your post is the bare minimum
to get it to compile, but when a program behaves as wrongly as you describe,
it's best to post _exactly_ what you compiled to remove all doubt that
something you haven't included in your post is causing the problem.
tried this in visual studio .net 2003, printed "2"
As it should.
tried this on solaris (not sure what version) with g++ (also not sure
what version) printed "1"


Well, that's amazing.

DW
Jul 23 '05 #13
yea i cant cut and paste because i am using a differnt computer for the
newsgroups as the coding. but regardless, all that was missing was the
#include and the namespace.

so, it does perform as desired when i make the functions virtual, is
there any reason i wouldnt want to just do that?

thanks

Jul 23 '05 #14
laniik wrote:
class A {
public:
void go() {cout<<"1"<<endl;}
};

class B : public A{
public:
void go() {cout<<"2"<<endl;}
};

int main()
{
B b;
b.go();
return 0;
}

tried this in visual studio .net 2003, printed "2"
tried this on solaris (not sure what version) with g++ (also not sure
what version) printed "1"


This prints "2" when compiled with GCC g++ v3.3.4 on
linux (SuSE Pro v9.2):

#include <iostream>

class A {
public:
void go() {std::cout << "1" << std::endl;}
};

class B : public A{
public:
void go() {std::cout << "2" << std::endl;}
};

int main()
{
B b;
b.go();
return 0;
}

Regards,
Larry

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #15
"laniik" <la****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
yea i cant cut and paste because i am using a differnt computer for the
newsgroups as the coding. but regardless, all that was missing was the
#include and the namespace.

so, it does perform as desired when i make the functions virtual, is
there any reason i wouldnt want to just do that?


It is very unusual for a function such as your 'go' not to be virtual. If
your object is really a B then almost always you would want to call B::go
even if you access it through an A& or A*, e.g.,
B b;
b.go(); // 1
A &a = b;
a.go(); // 2

Only if you actually wanted B::go to be called in case 1 and A::go to be
called in case 2 would you not make it virtual. The reason is that it's the
same object in each case, so you normally want the same function to be
called. This is object-oriented polymorphism. So, unless you have a
compelling reason not to, you should make it virtual (and you'll need a
virtual destructor in A if you do).

DW
Jul 23 '05 #16
laniik wrote:

yea i cant cut and paste because i am using a differnt computer for the
newsgroups as the coding. but regardless, all that was missing was the
#include and the namespace.

so, it does perform as desired when i make the functions virtual, is
there any reason i wouldnt want to just do that?


If that code really perfomrs that way on solaris, then there
is only one thing you want to do: get a different compiler, it
has a strange and serious bug. There is no point in using
it any longer.

But honestly: I doubt that this compiler has that bug. A bug like
this would be discovered in even the simplest compiler tests.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #17
well, i went and got a new g++, and it works fine now. soooo dunno why
it wasnt working. before

but thanks david, that helps me understand the virtual/nonvirtual
distinction more.

oliver

Jul 23 '05 #18

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

Similar topics

8
by: laniik | last post by:
Hi. I have a base class class base { public: static int x; base() {cout<<x<<endl;} virtual void hi(); };
2
by: MLH | last post by:
On the subject of switching between virtual consoles to get out of a 'hung' situation in the primary console... What I have been doing is pressing LEFTALT-F4 to bring up a 2nd virtual console,...
6
by: maadhuu | last post by:
The following is a piece of code and the output of the code is pasted after it . I dont understand how it works (while giving the output) .Can someone enlighten me on this ? thanking you ....
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
31
by: Peter E. Granger | last post by:
I'm fairly new to C++ and VC++, but for the most part it seems to do most of the same things that can be done in Java, with just some syntactic and structural adjustments. However, one thing I...
27
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: ...
2
by: Rob Dob | last post by:
Hi, How do I go about installing another Web Site Project inside my existing VS2005 website project. I currently have both a forum WSP and my main WSP application within the same solution. Both...
9
by: Stimp | last post by:
I'm trying to copy an asp.net project onto another computer in order to work on it from there as well. Whatever way I set up the directory in IIS the project cannot be run on the new computer (I...
10
by: Dan | last post by:
Hi, I create 5 dropdownlist in code-behind. I want to insert their selectedvalues in a table by making a string separated with ":" I can get their selecedvalues but i'm stuck when i want to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.