0

What is the difference between:

a)

class base{
   int a;
public:
   virtual int function();
}; 
class derived : public base{
   int b;
public:
   int function();
};

b)

class base{
   int a;
public:
   int function();
};    
class derived : public base{
   int b;
public:
   int function();
};

Why would you use (a) and why would you use (b)?

Is (b) a kind of polymorphism?

Cristi
  • 1,195
  • 6
  • 17
  • 24

2 Answers2

3

a) overrides the method in the base class. b) hides it. b) is not polymorphism.

Here's a useful link: The Definitive C++ Book Guide and List

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
1

First is overidding while second is method hiding.

First is used for dynamic dispatch and dynamic polymorphism. i.e: To call appropriate method depending on actual type of the object at run-time.

Second is used for method hiding.
Good Read:
What's the meaning of, Warning: Derived::f(char) hides Base::f(double)?

Alok Save
  • 202,538
  • 53
  • 430
  • 533