473,416 Members | 1,498 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,416 software developers and data experts.

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 23976
> 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*********@hotmail.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*********@hotmail.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*********@hotmail.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*********@hotmail.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
Victor Bazarov wrote:
provided that
> the return type that it does declare is a virtual subclass of the
> return type of the function declared in the base.


Sorry, my brain has an auto delete feature that tends to remove words from
my train of thought before it becomes committed in writing. It's the term
"virtual subclass". I take that to mean the base class was declared
virtual. It's not the wording I would have used, but it's the only meaning
I can derive from the expression.

--
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 #11

Victor Bazarov wrote:
Greg wrote:
Victor Bazarov wrote:
ve*********@hotmail.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


The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Jul 23 '05 #12
Greg wrote:
The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Not that I knew the answer before looking it up, but the standard does not
state that the return type is part of a function's signature:

1.3.10 signature
[defns.signature]
the information about a function that participates in overload resolution
(13.3): the types of its parameters and, if the function is a class member,
the cv-qualifiers (if any) on the function itself and the class in which
the member function is declared. The signature of a function template
specialization includes the types of its template arguments (14.5.5.1).

--
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 #13

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


I'm trying to recall exactly where in the Standard I read about this
feature. I'm pretty sure it was in Chapter 10 or thereabouts.. probably
Section 3, and I have the impression the relevant description was
pretty close, if not exactly at, paragraph 5... as best as I can
recall...

Jul 23 '05 #14


Victor Bazarov wrote:
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


I was under the impression that the return type had to polymorphically
inherit from the return type in the base. But the actual requirement
may be only that the return type in the derived function publically
inherit from the return type in the base.

Jul 23 '05 #15
Steven T. Hatton wrote:
Greg wrote:
The "but" was to point out a special exception to the claim that the
override function needed to have the same [which I read as "identical"]
signature as the overriden function in the base [a function's signature
includes its return type, whether the return type is considered
significant or not]. The signatures of a function and its override need
not be the same in every case, they need only be compatible to the
extent that the covariant return type rule allows.

A much more interesting discussion regarding covariant return types
would be to consider functions that return smart pointers parameterized
on base and derived types. Would it make sense to have the derived
smart pointer virtually inherit from the base smart pointer in order to
be able to return one for the other in an overridden virtual function?
Why or why not? [Actually I haven't thought the question through
myself].
Greg

Not that I knew the answer before looking it up, but the standard does not
state that the return type is part of a function's signature:

1.3.10 signature
[defns.signature]
the information about a function that participates in overload resolution
(13.3): the types of its parameters and, if the function is a class member,
the cv-qualifiers (if any) on the function itself and the class in which
the member function is declared. The signature of a function template
specialization includes the types of its template arguments (14.5.5.1).


For the sake of argument, I'll stipulate that a function signature does
not include its return type. But then the original claim that a
function declared in a derived class that has the same name and
signature of a function declared in a base class, overrides the
function in the base class, is incomplete and not wholly accurate.

The function declared in the derived class may in fact override the
function in the base, or the declaration may not be legal at all. And
one cannot assess the legality of the declaration of the function
override, without taking into consideration the requirements imposed on
both functions' return types.

Jul 23 '05 #16

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

Similar topics

1
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...
1
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....
11
by: iceColdFire | last post by:
Hi, What is the Diff btwn Function overloading and overriding thanks, a.a.cpp
3
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
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
12
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...
8
by: yashwant pinge | last post by:
#include<iostream> using namespace std; class base { public: void display() { } };
1
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...

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.