Prashanth
Prashanth

Reputation: 11

Function Overloading in Derived Class

I want to know the below code is correct or not

class A
{
public :
    int show (int x, int y);
};

class B : public A
{
public : 
    float show (int a, int b); // can i overload this function ?
};

the show function is present in both base and derived class with different written types. I know function overloading concept (can not overload with different return types).

Is this possible to do so?

Upvotes: 1

Views: 1445

Answers (3)

Ritz Ganguly
Ritz Ganguly

Reputation: 361

If you create a derived class object like

1)

B b; 
b.show(); 

OR

2)

A* b = new B();
b->show();

Will always look into derived class, and call B::show().

This is for your particular example, where no virtual functions are present, if virtual functions are present in the base class, the second case can give different results in other cases, but in this particular example even base class virtual functions will make no difference.

Upvotes: 0

Viktor Apoyan
Viktor Apoyan

Reputation: 10755

Check this link or this link

Class A
{
Public :
virtual int show (int x, inty) = 0;
};

class B:Public A
{
Public : 
float show (int x, int y);
};

Upvotes: 0

harper
harper

Reputation: 13690

The code will be compiled successfully. The method A::show will not be overloaded but hidden.

You can call this method with the scope operator.

Upvotes: 1

Related Questions