C - Overloading vs Overriding in Inheritance

0 votes

Overriding, as far as I can tell, is when two functions have the same name and function return type (void, int, float, etc.) as well as the same parameter numbers and types.

And overloading is when you have two functions with the same name but different parameter numbers/types or different function return types.

However, today in class, I came across the following slide:

enter image description here

Isn't this a little too much? 

Isn't that overriding? 

Because the return type has changed (from void to float) and the fa1() function in the base class had no parameter, but now has a float parameter in the derived class.

Why is this overriding?

Jun 6, 2022 in C++ by Nicholas
• 7,760 points
586 views

1 answer to this question.

0 votes

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is). 

That is, all arguments must be of the same type, with the same const qualification. 

If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding. 

This is what the "ERROR" in your image is attempting to convey. 

So, in that image, / overrides in a comment is incorrect and misleading.

Yes, many C++ instructors are unaware of these somewhat esoteric details.

Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. 

We could also say that the derived-class method hides the base-class method if it wasn't virtual. 

The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge.

Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. 

There is, however, a syntax sugar that "copies" all fa1 from the base to the derived.

class A
{
public:
    void fa1();
    void fa1(int);
};

class B: public A
{
public:
    using A::fa1;
    void fa1(int, int);
};

...
B b;
b.fa1(); // calls A::fa1()
b.fa1(4); // calls A::fa1(int)
b.fa1(4, 8); // calls B::fa1(int, int)

The section about hiding is rarely, if ever, applicable. 

When overriding, you should tell your compiler about it by using the override keyword. 

The compiler will then verify that your code performs as expected.

class A
{
public:
    virtual void fa1(int) {}
    void fa2(int) {}
};

class B: public A
{
public:
    void fa1(int) override {} // OK
    void fa1() override {} // ERROR: doesn't really override - different signature
    void fa2(int) override {} // ERROR: doesn't really override - not virtual in base
};
answered Jun 7, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

Constructor Overloading in C++

My C++ overloading does not work as I expect it to: #include "Node.h" #include <iostream> Node::Node() { cout ...READ MORE

Jun 2, 2022 in C++ by Nicholas
• 7,760 points
243 views
0 votes
1 answer

Constructor Overloading in C++

In your function Object() { [native code] }, Node(Game(),v); does not work as expected.  It simply creates a temporary without actually using it, and has no effect.  When control passes over the ;, it immediately destroys the temporary. Initializing the members in each function Object() { [native code] } is the correct way to go.  You could put their shared code in a private init() member function and call it from each function Object() { [native code] }, as shown below: class Foo { public: ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
224 views
0 votes
1 answer

*this vs this in C++

This is a pointer, and *this is a pointer that has been dereferenced. If you had a function that returned this, it would be a pointer to the current object, but a function that returned *this would be a "clone" of the current object, created on the stack unless you defined the method's return type to be a reference. A small application that demonstrates the difference between working with copies and working with references: #include <iostream> class Foo { public: ...READ MORE

answered Jun 28, 2022 in C++ by Damon
• 4,960 points
341 views
0 votes
0 answers

Static vs dynamic type checking in C++

I'm curious in the differences between static ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
308 views
0 votes
0 answers
0 votes
1 answer

C++ override inherited methods

Child::init is now concealing Father::init and not overriding it.  In order to receive dynamic dispatch, your init member function must be virtual: virtual void init () { ...READ MORE

answered Jun 20, 2022 in C++ by Damon
• 4,960 points
321 views
0 votes
1 answer

Difference between function overloading and method overloading

They are interchangeable. Some people, on the other hand, prefer calling methods, functions that are part of a class, and functions, free functions. //function overloading void foo(int x); void foo(int x, int ...READ MORE

answered Jun 21, 2022 in C++ by Damon
• 4,960 points
3,225 views
0 votes
0 answers

Differentiate between function overloading and function overriding

Differentiate between function overloading and function overriding ...READ MORE

Aug 17, 2022 in C++ by Nicholas
• 7,760 points
288 views
0 votes
1 answer

C++ - Overloading vs Overriding in Inheritance

In C++, a derived class's method only overrides the base class's method if their declarations match (I say "match," but I'm not sure what the formal term is).  That is, all arguments must be of the same type, with the same const qualification.  If there are any mismatches, the derived class's method hides all methods with the same name rather than overriding.  This is what the "ERROR" in your image is attempting to convey.  So, in that image, / overrides in a comment is incorrect and misleading. Yes, many C++ instructors are unaware of these somewhat esoteric details. Furthermore, if you want to override, your base class's method must be virtual; otherwise, polymorphism will not work. . We could also say that the derived-class method hides the base-class method if it wasn't virtual.  The part about hiding, on the other hand, has almost no meaning here; what this term really means is that you're not in charge. Furthermore, overloading is the presence of multiple methods with the same name but different signatures, as you may have noticed. To be useful, they must all be present in the derived class; otherwise, they will be hidden if the derived class only has one method, fa1, and the other fa1 are in the base. There is, however, a syntax sugar that "copies" all fa1 from base to derived, removing all the hidden semantics: class A { public: void fa1(); ...READ MORE

answered Jun 7, 2022 in C++ by Damon
• 4,960 points
569 views
0 votes
1 answer

What is the difference between operator overloading and operator overriding in C++?

Some people use the latter word to ...READ MORE

answered Aug 2, 2022 in C++ by Damon
• 4,960 points
1,683 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP