C - Overloading vs Overriding in Inheritance

0 votes

According to what I've learned, overriding occurs when two functions have the same name, function return type (void, int, float, etc.), and parameter numbers and types.

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

types.But today, in class, I noticed this 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 2 in C++ by Nicholas
• 4,720 points
13 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 base to derived, removing all the hidden semantics:

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 part about hiding is rarely, if ever, useful. When overriding, you should tell this to your compiler - use the override keyword for that. The compiler will then check that your code works as you intended.

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 by Damon
• 4,760 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 in C++ by Nicholas
• 4,720 points
23 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 in C++ by Damon
• 4,760 points
20 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 in C++ by Damon
• 4,760 points
16 views
0 votes
0 answers

Static vs dynamic type checking in C++

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

Jul 1 in C++ by Nicholas
• 4,720 points
8 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 in C++ by Damon
• 4,760 points
13 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 in C++ by Damon
• 4,760 points
24 views
0 votes
1 answer

What is the difference between Polymorphism, Overriding and Overloading?

As far as I know, the main ...READ MORE

answered Jun 19, 2018 in Java by scarlett
• 1,290 points
6,400 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 the base to the derived. class A { public: void fa1(); ...READ MORE

answered Jun 7 in C++ by Damon
• 4,760 points
34 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 6 days ago in C++ by Damon
• 4,760 points
11 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP