473,732 Members | 2,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Overloading/Overriding ?

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 overriding?
2. When is one preferable to use as opposed to the other?
3. How are virtual functions related to this topic
(overloading/overriding) - a real world example of using virtual
functions would be very much appreciated.

Thank you

Jul 23 '05 #1
15 24008
> 1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);

overriding is writing a different body (in a derived class) for a
function defined in a base class.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};
2. When is one preferable to use as opposed to the other? They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class. Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).
3. How are virtual functions related to this topic

I'd suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html

Jul 23 '05 #2
ve*********@hot mail.com wrote:
1. Whats the difference between overloading and overriding?


overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Just to add,
Overloading can be done without usage of classes. However if you are
using a class then these functions have to be in the same class.

Also, the return type is not relevant. Soo, as said above only the
function name should be same while paramter type/number must be
differnt.

Jul 23 '05 #3
ve*********@hot mail.com wrote:
1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.
.... and *only* if the function in the base class is declared _virtual_
and *only* if the function in the derived class has the same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};
Here you have an example of name hiding since 'base::Foo' isn't virtual.
2. When is one preferable to use as opposed to the other?


They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class.


This is incorrect. All functions in the derived class _hide_ the base
class functions with the same name, except in the case of virtual ones,
which override the base class functions with the _same_signature _.

Also overriding only works when a class is derived from
another class where overloading can be used inside a class (or for non
class function definitions).


Overloading only relates to the functions in the same _scope_, i.e. in
the same class or outside of any class.
3. How are virtual functions related to this topic


I'd suggest to read the FAQ at
http://www.parashift.com/c++-faq-lite/index.html


It's always a GOOD IDEA(tm).

V
Jul 23 '05 #4


Victor Bazarov wrote:
ve*********@hot mail.com wrote:
1. Whats the difference between overloading and overriding?

overloading is the definition of several functions with the same name
but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.


... and *only* if the function in the base class is declared _virtual_
and *only* if the function in the derived class has the same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};


Here you have an example of name hiding since 'base::Foo' isn't virtual.
2. When is one preferable to use as opposed to the other?


They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in a
derived class.


This is incorrect. All functions in the derived class _hide_ the base
class functions with the same name, except in the case of virtual ones,
which override the base class functions with the _same_signature _.


But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that the
return type that it does declare is a virtual subclass of the return
type of the function declared in the base. [assuming compilation with a
C++ compiler that supports "covariant return types"]

Greg

Jul 23 '05 #5
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that the
return type that it does declare is a virtual subclass of the return
type of the function declared in the base. [assuming compilation with a
C++ compiler that supports "covariant return types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not that I
doubt what you assert, I would simply like to see how it's actually stated
in the Standard.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #6
Greg wrote:
Victor Bazarov wrote:
ve*********@hot mail.com wrote:
1. Whats the difference between overloading and overriding?
overloading is the definition of several functions with the same
name but different arguments and/or a different number of arguments.

void Foo(int);
void Foo(int, int);
void Foo(char);


Overloading _only_ applies to names of functions in the *same scope*.
overriding is writing a different body (in a derived class) for a
function defined in a base class.


... and *only* if the function in the base class is declared
_virtual_ and *only* if the function in the derived class has the
same signature.

class base
{
public:
int Foo()
{
return 1;
}
};

class derived: public base
{
public:
int Foo()
{
return 2;
}
};


Here you have an example of name hiding since 'base::Foo' isn't
virtual.
2. When is one preferable to use as opposed to the other?

They are not exclusive but complimentary. It is possible to have
several overloaded functions in a base class that are overridden in
a derived class.


This is incorrect. All functions in the derived class _hide_ the
base class functions with the same name, except in the case of
virtual ones, which override the base class functions with the
_same_signature _.


But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]


Yes. Covariant return types are part of the definition of "the same
signature" when talking about virtual functions. So, I don't really
understand the "But" in the beginning of your reply.

V
Jul 23 '05 #7
Steven T. Hatton wrote:
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.


Can't you search your copy of the Standard for "covariant" ?
Jul 23 '05 #8
Victor Bazarov wrote:
Steven T. Hatton wrote:
Greg wrote:
But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg


Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.


Can't you search your copy of the Standard for "covariant" ?


I don't see the part about requiring the return type to be virtual.

"The return type of an overriding function shall be either identical to the
return type of the overridden function or covariant with the classes of the
functions. If a function D::f overrides a function B::f, the
return types of the functions are covariant if they satisfy the following
criteria:
? both are pointers to classes or references to classes98)
? the class in the return type of B::f is the same class as the class in the
return type of D::f, or is an
unambiguous and accessible direct or indirect base class of the class in
the return type of D::f
? both pointers or references have the same cv-qualification and the class
type in the return type of D::f
has the same cv-qualification as or less cv-qualification than the class
type in the return type of B::f.
If the return type of D::f differs from the return type of B::f, the class
type in the return type of D::f
shall be complete at the point of declaration of D::f or shall be the class
type D. When the overriding
function is called as the final overrider of the overridden function, its
result is converted to the type returned
by the (statically chosen) overridden function (5.2.2)."

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #9
Steven T. Hatton wrote:
Victor Bazarov wrote:
Steven T. Hatton wrote:
Greg wrote:

But the signature of a virtual function override can declare a
different return type than the function it overrides, provided that
the return type that it does declare is a virtual subclass of the
return type of the function declared in the base. [assuming
compilation with a C++ compiler that supports "covariant return
types"]

Greg

Care to provide ISO/IEC 14882:2003 chapter and verse on that? Not
that I doubt what you assert, I would simply like to see how it's
actually stated in the Standard.
Can't you search your copy of the Standard for "covariant" ?


I don't see the part about requiring the return type to be virtual.


Come again... How can a return type be virtual?
[...]


V
Jul 23 '05 #10

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

Similar topics

1
1622
by: BDob | last post by:
Standard documents "Access control is not considered in determining overriding." To further explain what's puzzling me, I put up an example below: class Base { public: virtual void Method1(int a); void Method2(int b);
1
2370
by: Xiangliang Meng | last post by:
Hi, all. When reading C++ books, I'm alway confused by those terms "redefining functions", "overloading functions" and "overriding functions". Please give me some comments on those terms. Thanks. If giving more strategy hehind them, more helpful. Best Regards,
11
8918
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
3
2888
by: Iyer, Prasad C | last post by:
I am new to python. I have few questions a. Is there something like function overloading in python? b. Can I overload __init__ method Thanks in advance regards
45
3289
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
12
8087
by: Achim Domma | last post by:
Hi, I want to use Python to script some formulas in my application. The user should be able to write something like A = B * C where A,B,C are instances of some wrapper classes. Overloading * is no problem but I cannot overload the assignment of A. I understand that this is due to the nature of Python, but is there a trick to work around
8
3221
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
1
3073
by: Zach | last post by:
Consider the following code: void Test(int i) { System.Console.WriteLine("int function"); } void Test(object o) { System.Console.WriteLine("object function");
20
2955
by: ramadeviirrigireddy | last post by:
Can anyone tell me whether the return type of the method will also be considered when you are overriding a method? what i mean exactly is whether the return type can be different when you are overriding a method.and somewhere i read like overloading will not come under polymorphism. is this true?
0
9447
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
9235
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
9181
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
8186
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
6031
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
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
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
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.