0

Assume the following code:

class base {
public:
 int getAge(){
  return 20;
 }
};

class dri : public base {
public:
 int getAge(){
  return 30;
 }
};

int main(){

 base *b = new dri();
 std::cout << b->getAge() << std::endl;

 return 0;
}

I know with the magic of polymorphism the above code is compiled and you will get 20 in your console, you can also override them using the virtual keyword.

My question is, why and when would you use this? if you need an object of type dri why won't you just do dri d; or dri *d = new dri(), since it already includes all the functionalities of base.

Joe
  • 19
  • 2
  • You could make a vector of `base *`, and store both `base` and `dri` instances in it. E.g. if you were making a game, you could make all enemy classes derived from a single base, and store them all in a single `vector`. – HolyBlackCat Feb 03 '19 at 14:32
  • Polymorphism is a way to reuse code. Let's say you have a data consumer which is fed data from a text file comma-separated value (CSV) parser. Later, you instead have an SQLite backend, so you pass in the different SQL parser to the data consumer. The data consumer code does not have to change AT ALL even though it was created back in the day where there was only a CSV parser. The hard part is knowing when to abstract out an interface, and when not to over engineer a solution. – Eljay Feb 03 '19 at 14:38
  • Just think of any example where you have several classes that derives from the base one. You base class could be "vehicle" and your derived classes could be "car", "truck" or "motorcycle". They all have need to have a method "startupRoutine()" but their implementations will all be different. Your base class can hold a "default" implementation or may not even if the method is pure virtual. – Norgannon Feb 03 '19 at 14:38

1 Answers1

1

If you want a dri, then just go ahead and declare your variable as such.

The benefit of polymorphism is that if all you need are the methods that are defined on base, then you can define your variable (or most likely, function parameter) as base * (or base &) and your code will work with any class derived from base including dri.

If there are only two classes in your system (base and dri) then it's difficult to see the benefits. But imagine that the base class is something more general like DataSource and it has a method called readData. Now you can write code that reads data from a source without having to worry that data source is a Database or a Keyboard or Sensor or anything else. You can write the data-handling logic and someone else can come along later and add new sources by creating new DataSource subclasses.

In order for this to work, the methods involved have to be virtual. If you define a non-virtual base method and call it with a base pointer or reference, you'll always get that function, even if a subclass tries to override it. The virtual keyword tells C++ to call the method defined for the the actual object type rather than the declared variable type.

Willis Blackburn
  • 8,068
  • 19
  • 36