Home > Back-end >  C - Overloading vs Overriding in Inheritance
C - Overloading vs Overriding in Inheritance

Time:11-24

As far as I learned, Overriding is when you have 2 functions which have the same name and function return type (void, int, float.. etc) and the same parameter numbers and types.

And the overloading is when you have 2 functions which have the same name but either Parameter number/types or function return type should be different.

But today when I was in the class, I saw this slide:

enter image description here Shouldn't this be overloading? Not overriding? Because here the return type changed (from void to float) and fa1() function in the base class had no parameter, but in the derived class it has float parameter.

If this is overriding, why?

CodePudding user response:

In C , any method in a derived class only overrides the method in the base class if their declarations match (I say "match" but I don't know the formal term for that). That is, all arguments must have the same type, and const qualification of this must be the same. If anything there mismatches, the method in the derived class hides all methods with the same name, instead of overriding. This is what the "ERROR" in your picture tries to tell you. So // overrides in a comment in that picture is incorrect and misleading.

Yes, many C teachers actually don't understand these somewhat obscure details.

BTW additionally, if you want to override, the method in your base class should be virtual; otherwise, polymorphism won't work. If it was not virtual, we also say that the derived-class method hides the base-class method. Here, however, the part about hiding has almost no meaning; what this term really wants to express is that you're not overriding.

In addition, overloading is, as you noticed, presence of several methods with the same name but different signatures. They should all be present in the derived class to be useful - if the derived class has only one method fa1, and the other fa1 are in the base, they will be hidden. However, there is syntax sugar which "copies" all fa1 from base to derived, disabling all that hiding 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
};

CodePudding user response:

ia1 not overloading. First you can't overload variables. So ia1 is definitely an override. And it's also a dangerous thing. At my company, our coding standards disallow overriding variable names in any situation. It just leads to confusion.

fa1 though -- that looks like an overload. The base class fa1() takes no arguments, but the subclass version takes a float.

  • Related