Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

C++ : overload and override ..

To C++ expert :

   I have some questions about the overloading and overriding. In the following code :

class Base{
  public:
    Base(){}
    ~Base(){}
    int read_a(){}
  private:
    int a ;
};

class Derived:public Base{
   public:
     Derived(){}
     ~Derived(){}
     float read_a(){}
   private:
     float a ;
} ;

so is the "read_a()" in the Derived class overloading or overriding ? or neither ?

what if in the Base class: "int read_a()" becomes "virtual int read_a()" and int the Derived class: "float read_a()" becomes "virtual float read_a()", is it a overloading or overriding ?

Thanks a lot ~~~

meow ....
Avatar of zechariahs
zechariahs

read_a() in the Derived class is overloaded since it uses the same function name but has a different function definition.

If you use the "virtual" keyword in the base class, that tells the compiler that that function can be overridden in a derived class.
In your second question, I don't beleive that Derived::float read_a() is either overloaded or overriden.
The difference between overload and override is the following

Overload means 2 functions with the same name but with different signature (different parameters)

for instance (It's not code in any language, just an example)

public:
class MyClass
{
public:
     void printText( string text );
     void printText( string text, Font font );
     void printText( string text, Font font, Color color );

}

Here the methods are the same, but it's parameters are different, so its overload

In your case read_a() is overload

Override occurs when a derived class implements a method already implemented in the base class

for instance

class Base
{
public:
         virtual void printText( string text );
}

class Derived : public Base
{
         void printText( string text );
}

In this case Derived class implements the same method than the base class, so its override.


In your case, I'm not sure if that code would compile

as in the derived class you would have (including functions implemented in Base)

float read_a(); and int read_a();

and you're not allowed to have 2 functions with the same name and the same parameters that returns different types

Instead if you would have


class Base{
 public:
   Base(){}
   ~Base(){}
   virtual int read_a(){}
 private:
   int a ;
};

class Derived:public Base{
  public:
    Derived(){}
    ~Derived(){}
    int read_a(){}
  private:
    int a ;
} ;

it would be Override (functions with the same name and the same parameters, re implemented in the derived class)

and if you would have

class Base{
 public:
   Base(){}
   ~Base(){}
   int read_a(){}
 private:
   int a ;
};

class Derived:public Base{
  public:
    Derived(){}
    ~Derived(){}
    float read_a( bool noNeedForThis ){}
  private:
    float a ;
} ;

in this case it would be overload (functions with the same name and different parameters)
Avatar of meow00

ASKER

I am a little confused here ... then in the derived class ...
there are "int read_a()" and "float read_a()" both functions take no arguments ..... is that true overloading functions "must"
take different arguments ? or did I miss understand anything here ?
Yes, you understood ok

From C++ Primer 3rd Edition(Lippman)

"Two functions are overloaded if they have the same name, are declared in the same scope, and have different parameter lists."
Avatar of meow00

ASKER

I feel kind of more confused now ....
the code did compile :

#include <iostream>

class Base{
  public:
    Base(){}
    ~Base(){}
    int read_a(){ cout <<"Base int" << endl;}
  private:
    int a ;
};

class Derived:public Base{
   public:
     Derived(){}
     ~Derived(){}
     float read_a(){ cout <<"Derived float" << endl ;}
   private:
     float a ;
} ;

int main()
{
   Derived d ;
   d.read_a() ;

   return 0 ;
}
-----------
the output is actually "Derived float". I assume the Derived class has both "int read_a()" and "float read_a()". If it is overloading, why made it choose "float read_a()" instead of "int read_a()" ?
And it's not overriding because  there is not virtual function here ???

Thanks a lot !!!
This its definitely not overloading, as the parameter list is just the same.

And you're right....... it does compile....... my mistake

As the read_a() method is implemented it executes directly, as any other method of the class.

In this particular code, you would say that Derived::read_a function does not override Base::read_a because read_a is not a virtual function as you said.

In fact, you would say that it hides Base::read_a.

Conceptually you would say override you mean "the read_a function that in the base class does this thing, in this one does this other thing".

In this case you mean something like "the read_a function in the base class does this thing, and the read_a function in the derived class does this other thing". There is no link between both functions, they could be considered completely different functions, but you can only access the one implemented in the derived class, as when the compiler looks for the method to run, it first look in the methods implemented in the derived class, and as it finds it there, it will never go to look for it in the function virtual table, so it will never be able to call the other one.

I hope I've been clear enoughj, otherwise, let me know and I'll try to explain it a little more.

Tincho
ASKER CERTIFIED SOLUTION
Avatar of Mustak_Shaikh
Mustak_Shaikh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial