473,856 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Polymorphism and inheritance

Hello,

I created the following inheritance:

class Parent {
public:
void foo(int i);
};

class Child : public Parent {
public:
void foo(int i, int i);
};

The following code fragment does not work (it doesn't compile, g++
complains about 'no matching function call for Child::foo(int) ':

....
Child c;
int k = 0;
c.foo(k);
....

I assumed that by inheriting the base class, the 'Child' class would
have two 'foo' methods, with different parameters. Apparently not. Adding

void foo(int i) { Parent::foo(i); }

to the Child class, fixes it, but is that how it should be done? Why is
the Parent's foo() not polymorphised-inherited by Child?

TIA,
Bart
Sep 8 '08 #1
17 3892
Bart Friederichs wrote:
Hello,

I created the following inheritance:

class Parent {
public:
void foo(int i);
};

class Child : public Parent {
public:
void foo(int i, int i);
};

The following code fragment does not work (it doesn't compile, g++
complains about 'no matching function call for Child::foo(int) ':
http://www.parashift.com/c++-faq-lit....html#faq-23.9
...
Child c;
int k = 0;
c.foo(k);
...

I assumed that by inheriting the base class, the 'Child' class would
have two 'foo' methods, with different parameters. Apparently not. Adding

void foo(int i) { Parent::foo(i); }

to the Child class, fixes it, but is that how it should be done? Why is
the Parent's foo() not polymorphised-inherited by Child?
The compiler only looked in the derived class for functions with name
"foo", found one and stopped. You have to tell it that there are more
functions of the same name, for example with a "using" declaration.

--
Thomas
Sep 8 '08 #2
On 8 Sep, 11:22, Bart Friederichs <b...@tbwb.nlwr ote:
Hello,

I created the following inheritance:

class Parent {
public:
* * * * void foo(int i);

};

class Child : public Parent {
public:
* * * * void foo(int i, int i);

};

The following code fragment does not work (it doesn't compile, g++
complains about 'no matching function call for Child::foo(int) ':

...
Child c;
int k = 0;
c.foo(k);
...

I assumed that by inheriting the base class, the 'Child' class would
have two 'foo' methods, with different parameters. Apparently not. Adding

void foo(int i) { Parent::foo(i); }

to the Child class, fixes it, but is that how it should be done? Why is
the Parent's foo() not polymorphised-inherited by Child?

TIA,
Bart
Bart,

Couple of things to get out of the way first:
1) There is no polymorphism at all. Polymorphism is when you have
virtual functions.
2) Child::foo() is illegal as it has two parameters called "i".
What you're attempting is overloading. When Child introduces a method
called foo, it hides all the other methods from its base classes with
the same name. You can still call it like this:

int main()
{
Child c;
int k = 0;
c.Parent::foo(k ); // <--- We want to call Parent's foo()
}

Another alternative is to unhide foo with Parent like this:
class Parent {
public:
void foo(int i)
{
}
};
class Child : public Parent {
public:
void foo(int i, int j)
{
}
using Parent::foo; // <--- Unhide foo() from Parent
};

int main()
{
Child c;
int k = 0;
c.foo(k);
}
Sep 8 '08 #3
Sam
Bart Friederichs writes:
Hello,

I created the following inheritance:

class Parent {
public:
void foo(int i);
};

class Child : public Parent {
public:
void foo(int i, int i);
};

The following code fragment does not work (it doesn't compile, g++
complains about 'no matching function call for Child::foo(int) ':

...
Child c;
int k = 0;
c.foo(k);
...

I assumed that by inheriting the base class, the 'Child' class would
have two 'foo' methods, with different parameters. Apparently not. Adding

void foo(int i) { Parent::foo(i); }

to the Child class, fixes it, but is that how it should be done? Why is
the Parent's foo() not polymorphised-inherited by Child?
Because if a function of the given name is declared in the child, the
compiler tries to match its signature to the function call. The parent
superclass is matched only if no function of the given name exists in the
child subclass. In your example, removing the declaration from the child
superclass will also work.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEUEABECAAYFAkj FBpQACgkQx9p3GY HlUOIDHgCWNlsLR k9+IpsV8plDNBVO O/N/
dgCeMmXwmdpCpHN XGFzdrKLnCd/cFfg=
=melY
-----END PGP SIGNATURE-----

Sep 8 '08 #4
ne*******@gmail .com wrote:
Couple of things to get out of the way first:
1) There is no polymorphism at all. Polymorphism is when you have
virtual functions.
Ah. I thought polymorphism was having the same method name with
different parameters. Guess I mixed up some terminology.
2) Child::foo() is illegal as it has two parameters called "i".
That was a typo.
Another alternative is to unhide foo with Parent like this:
class Parent {
public:
void foo(int i)
{
}
};
class Child : public Parent {
public:
void foo(int i, int j)
{
}
using Parent::foo; // <--- Unhide foo() from Parent
};
I'd assume it is automatically unhidden, because it has a different
prototype.

I never saw that use of 'using'. Thanks.

Bart
Sep 8 '08 #5
Bart Friederichs <bf@tbwb.nlwrot e:
I created the following inheritance:

class Parent {
public:
void foo(int i);
};

class Child : public Parent {
public:
void foo(int i, int i);
};

The following code fragment does not work (it doesn't compile, g++
complains about 'no matching function call for Child::foo(int) ':

...
Child c;
int k = 0;
c.foo(k);
...

I assumed that by inheriting the base class, the 'Child' class would
have two 'foo' methods, with different parameters. Apparently not.
It does, but the client code can't see the Parent::foo(int )
member-function because it is blinded by the child's foo(int, int)
function.
Adding

void foo(int i) { Parent::foo(i); }

to the Child class, fixes it, but is that how it should be done?
"using foo;" would also work.
Why is the Parent's foo() not polymorphised-inherited by Child?
Polymorphism isn't involved (note, no use of the word 'virtual' in the
code...

The child does inherit the foo(int) method, as witnessed by this code:

Child c;
Parent* p = &c;
p->foo(k);

it's just that the compiler stops looking after it finds a 'foo'
identifier in the first place it looks. "using foo;" tells it to keep
looking.
Sep 8 '08 #6
On 8 Sep, 12:12, Bart Friederichs <b...@tbwb.nlwr ote:
Ah. I thought polymorphism was having the same method name with
different parameters. Guess I mixed up some terminology.
The terminology can be confusing.

Having functions with the same name but different parameters is called
overloading.

Having a member function with the same name and same parameters as in
a base function, and getting the computer to call the right one
depending on what type of object you actually have, is called
polymorphism, or virtual functions, or overriding.
I'd assume it is automatically unhidden, because it has a different
prototype.
I never saw that use of 'using'. Thanks.
The reason it's in the FAQ is because lots of people assume that.
Don't be too hard on yourself.
Sep 8 '08 #7
On 8 Eylül, 22:15, gw7...@aol.com wrote:
On 8 Sep, 12:12, Bart Friederichs <b...@tbwb.nlwr ote:
Ah. I thought polymorphism was having the same method name with
different parameters. Guess I mixed up some terminology.

The terminology can be confusing.

Having functions with the same name but different parameters is called
overloading.
This is not an overloading. You can only overload a function in a same
scope. But foo( int ) and foo (int, int) are in different scope.
>int main()
{
Child c;
int k = 0;
c.foo(k);
}
If you call the foo function via Child obcejt the complier look up the
foo function in the Child class but there in no function foo (int,
int). So that was an error. Child::foo function is a viable function
but it is not a candidate function because of parameters.

Karaali
Sep 8 '08 #8
On Sep 9, 5:26 am, Ali Karaali <ali...@gmail.c omwrote:
On 8 Eylül, 22:15, gw7...@aol.com wrote:
On 8 Sep, 12:12, Bart Friederichs <b...@tbwb.nlwr ote:
Ah. I thought polymorphism was having the same method name with
different parameters. Guess I mixed up some terminology.
The terminology can be confusing.
Having functions with the same name but different parameters is called
overloading.

This is not an overloading. You can only overload a function in a same
scope. But foo( int ) and foo (int, int) are in different scope.
Bart's expectation was that they'd be visible in the same scope.

Anyway, exactly what polymorphism is is popularly disputed. Some
people use it to refer to virtual dispatch based run-time polymorphism
by default, and explicitly say "compile-time polymorphism" when
thinking of templates, and would argue themselves blue in the face
that C++ doesn't have any other polymorphic mechanisms. With so many
computing "experts" floating around, polymorphism has inevitably been
redefined to mean whatever somebody meant when they said it, but IMHO
the best criteria is that it allows the same piece of source code to
generate different code depending on the types to which the variable
involved resolve. For example...

a = x + y

....is valid code whether a, x and y are ints or doubles. This relies
on compile-time polymorphism provided by the compiler itself. A good
test for polymorphic language features is can they be used to write...

is_less_than(x, y)

....where x and y are not both locked in to one specific type. C++
mechanisms that allow this include:

- preprocessor macros
- overloading
- virtual dispatch
- templates

All these mechanisms are polymorphic.

Still, the test isn't perfect: you could write an is_less_than(x, y)
implementation using memcmp() and sizeof that produced results that
were meaningful only in the sense that they were consistent (at least
for types without structure padding), and therefore useful for sorting/
searching, but it wouldn't actually be polymorphic because there
hasn't been type-driven code generation. That's more in line with C-
style generic programming ala the bsearch() or qsort() functions
common on UNIX platforms.

Of course there have been many other uses of the term "polymorphi sm"
over the years, such as to describe self-mutating virus code, a usage
that has nothing to do with types at all....

Tony
Sep 9 '08 #9
On Sep 9, 6:36 am, tony_in_da...@y ahoo.co.uk wrote:
On Sep 9, 5:26 am, Ali Karaali <ali...@gmail.c omwrote:
On 8 Eylül, 22:15, gw7...@aol.com wrote:
On 8 Sep, 12:12, Bart Friederichs <b...@tbwb.nlwr ote:
Ah. I thought polymorphism was having the same method name with
different parameters. Guess I mixed up some terminology.
The terminology can be confusing.
Having functions with the same name but different parameters is called
overloading.
This is not an overloading. You can only overload a function in a same
scope. But foo( int ) and foo (int, int) are in different scope.

Bart's expectation was that they'd be visible in the same scope.

Anyway, exactly what polymorphism is is popularly disputed. Some
people use it to refer to virtual dispatch based run-time polymorphism
by default, and explicitly say "compile-time polymorphism" when
thinking of templates, and would argue themselves blue in the face
that C++ doesn't have any other polymorphic mechanisms. With so many
computing "experts" floating around, polymorphism has inevitably been
redefined to mean whatever somebody meant when they said it, but IMHO
the best criteria is that it allows the same piece of source code to
generate different code depending on the types to which the variable
involved resolve. For example...

a = x + y

...is valid code whether a, x and y are ints or doubles. This relies
on compile-time polymorphism provided by the compiler itself. A good
test for polymorphic language features is can they be used to write...

is_less_than(x, y)

...where x and y are not both locked in to one specific type. C++
mechanisms that allow this include:

- preprocessor macros
- overloading
- virtual dispatch
- templates

All these mechanisms are polymorphic.

Still, the test isn't perfect: you could write an is_less_than(x, y)
implementation using memcmp() and sizeof that produced results that
were meaningful only in the sense that they were consistent (at least
for types without structure padding), and therefore useful for sorting/
searching, but it wouldn't actually be polymorphic because there
hasn't been type-driven code generation. That's more in line with C-
style generic programming ala the bsearch() or qsort() functions
common on UNIX platforms.

Of course there have been many other uses of the term "polymorphi sm"
over the years, such as to describe self-mutating virus code, a usage
that has nothing to do with types at all....

Tony
Preprocessor Macros are preprocessed before compilation, How can you
say that they are polymorphic in nature, Preprocessor directives are
resolved while preprocessing.. !!
Sep 9 '08 #10

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

Similar topics

37
2868
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily use the instance of this base class (or its children class). How can I ensure the instance IS-A base class instance, since Python is a fully dynamic typing language? I searched and found several different ways to do this:
12
7072
by: Steve Jorgensen | last post by:
The classing Visual Basic and VBA support for polymorphism, let's face it, is a bit on the weak side, and built-in support for inheritance is non-existent. This little essay is about some patterns I've ended up using successfully for certain kinds of inheritance and polymorphism, and some that have not worked out so well. Let's start with obvious things that turn out not to work well: 1. Use interface classes and "Implements" for...
3
41545
by: enchantingdb | last post by:
I have an exam tomorrow that covers the perceived advantages and disadvantages of object oriented programming, in particular polymorphism, inheritance and encapsulation. I know the advantages but am not clear on the disadvantages. I have had a look on the Web and in newsgroups but couldn't find much. As time is running out, I thought I would post here and hope that someone would reply. Thanks Rob
10
3048
by: Lino Barreca | last post by:
Take a look at this code: Class clsAnagrafica Public Overridable ReadOnly Property Codice() As Integer Get Return 1 End Get End Property End Class
13
3272
by: Fao | last post by:
Hello, I am having some problems with inheritance. The compiler does not not return any error messages, but when I execute the program, it only allows me to enter the number, but nothing else happend. I think the problem may be in my input function or in the main function. If anyone out there can help me it woul be greatly appreciated. Here is the code: #include <iostream>
18
3878
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 respond to different messages in
11
2989
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++, that anybody who doesn't use it might as well just program in plain C. I totally disagree with this, because I think C++ has a lot of great features apart from polymorphism, such as the ability to organize code into classes, code reuse through...
8
20620
by: weird0 | last post by:
Can anyone explain briefly what is the difference between inheritance and polymorphism? i read and seem to forget it again and again... Can anyone along with good examples of c# explain the fundanmental concept so i can remember it forever as it is a common interview question.... Thanks in advance
1
1618
by: mattmao | last post by:
I am brand new to C#.NET so here is my trial on this lab exercise: using System; using System.Collections.Generic; using System.Text; using System.Collections; namespace lab02exec { public class Program
1
10115
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 seriously limit your ability to use hierarchies like the example in a real program. Then, the article...
0
9920
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11063
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10790
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10390
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9535
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7098
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5960
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4580
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3203
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.