473,569 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Polymorphism problem with virtual functions ...

Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+ne wton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );
};

class SubClass1 : public BaseClass {
int getValue( int index ) { return 1; };
};

....

int main() {
BaseClass* oskar = new SubClass1;
cout << oskar->getValue(4); // supposed to be 1
delete oskar;
}

Now the compiler tells me: "undefined reference to `vtable for BaseClass'"
and "undefined reference to `typeinfo for BaseClass'"

I use g++-4.1. as a compiler, although I suppose the problem is due to my
lack of understandding C++ ...

Thanks in advance and greetings from Germany,
Jonas
Feb 12 '07 #1
3 1539
On 12 Feb., 12:15, Jonas Huckestein <Jonas.Huckest. ..@web.dewrote:
Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+ne wton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );

};
either make the method pure virtual, like this:
virtual int getValue( int index )=0;
or provide a body:
virtual int getValue( int index ){return 0;}
lg Rudi

Feb 12 '07 #2
On Feb 12, 12:15 pm, Jonas Huckestein <Jonas.Huckest. ..@web.dewrote:
Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the operators
*, /, -, +. Now I want two classes SubClass1 and SubClass2 which extend the
BaseClass and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+ne wton/oskar;
Note that this will not do what you want it to do. This will perform
arithmetic operations with the pointers and not with the objects, you
have to dereference the pointers first:

BaseClass *pete = *oskar * (*newton) + *newton / *oskar;

And unless SubClass + SubClass returns a pointer you should use

BaseClass *pete = new SubClass(*oskar * (*newton) + *newton / *oskar);

It would probably be easier to use references/local variables instead.

--
Erik Wikström

Feb 12 '07 #3
Jonas Huckestein wrote:
Hello,

I have the following problem: I have one BaseClass, which defines the
virtual functions getValue(int index) and virtually overloads the
operators *, /, -, +.
Making those operators virtual is most often not useful.
Now I want two classes SubClass1 and SubClass2 which extend the BaseClass
and implement the virtual functions, so that I can go like this:

BaseClass *oskar = new SubClass1;
BaseClass *newton = new SubClass2;
BaseClass *pete = oskar*newton+ne wton/oskar;

Is there a way I can accomplish this? Somehow I can't even get the easiest
situation straight, in which I have only two classes and 1 virtual
function:

class BaseClass {
public:
virtual int getValue( int index );
};
That class is missing a virtual destructor, so the result of your delete in
main is undefined.
class SubClass1 : public BaseClass {
int getValue( int index ) { return 1; };
};

...

int main() {
BaseClass* oskar = new SubClass1;
cout << oskar->getValue(4); // supposed to be 1
delete oskar;
}

Now the compiler tells me: "undefined reference to `vtable for BaseClass'"
and "undefined reference to `typeinfo for BaseClass'"

I use g++-4.1. as a compiler, although I suppose the problem is due to my
lack of understandding C++ ...
Except for the non-virtual destructor, your code is ok, so it must be
something else.
Thanks in advance and greetings from Germany,
Greetings back from Germany ;-)

Feb 12 '07 #4

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

Similar topics

4
2389
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a hierarchy of algebraic matrices with the addition operation. Thus, I want to have a virtual base class class Matr;
4
4414
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their reuse for various template parameters. I wonder if there exist techniques for achieving a dynamic polymorphism using the generic programming. Is this...
3
7427
by: E. Robert Tisdale | last post by:
polymorph just means "many form(s)". The definition in plain English http://www.bartleby.com/61/66/P0426600.html and narrower definitions in the context of computer programming http://en.wikipedia.org/wiki/Polymorphism http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?query=polymorphism&action=Search
7
5376
by: Samee Zahur | last post by:
Hello, The other day I was rather shocked to find that I couldn't find a good use for runtime polymorphism! Let me explain this a bit further before you get shocked. Any function that I could previously write like this: void func1(Base& obj) { //... obj.virmeth(); //Call virtual method //...
8
1689
by: Locia | last post by:
What are the techniques used to implement parametric polymorphism in C++? A Polymorphic method can be virtual?
18
3843
by: Seigfried | last post by:
I have to write a paper about object oriented programming and I'm doing some reading to make sure I understand it. In a book I'm reading, however, polymorphism is defined as: "the ability of two different objects to respond to the same request message in their own unique way" I thought that it was: "the ability of same object to...
8
2879
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
7
2466
by: desktop | last post by:
This page: http://www.eptacom.net/pubblicazioni/pub_eng/mdisp.html start with the line: "Virtual functions allow polymorphism on a single argument". What does that exactly mean? I guess it has nothing to do with making multiple arguments in a declaration like:
1
10069
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which you may have seen many times in one form or another. An analysis of this example produces several problems that are not obvious but which will...
17
3839
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
7700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7676
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.