0

I was looking at tutorials on OOP using C++, and to explain polymorphism and abstract classes, following slide was used:

Slide from tutorial

So, you can see, a base class called 'base' and two derived classes 'savings' and 'current' are defined, and then address of a 'savings' object (and later 'current' object) is assigned to the 'base' pointer.

Although I understand how the function behaves according to context, I don't quite get why and where I would want to do such a pointer assignment. Can someone please explain? Also, if there is a better example to demonstrate the same concept, that will be appreciated too.

Epsilon7
  • 45
  • 1
  • 8
  • 1
    You are basically asking to explain you object-oriented programming. This is far too broad. Read one of the zillion books written on the subject, or a dozen. – n. m. could be an AI Aug 03 '18 at 13:05
  • I'd recommend [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for learning C++. You generally want to use polymorphism when you can have several different implementations of a particular feature. Consider base class `Animal` with virtual function `eat()` and derived classes `Mammal` and `Bird`. `Mammal` would use it's mouth to `eat()`, while `Bird` would use it's beak. Zookeeper doesn't care **how** each of them eats. He would only care if they do. – Yksisarvinen Aug 03 '18 at 13:05
  • Thanks a lot for the help guys. I will definitely read the books. – Epsilon7 Aug 03 '18 at 13:09

3 Answers3

2

Why? Because you want to iterate over a list/array of current and savings who are mixed up. Also by having the same base type you can pass it to a function taking a base pointer that does the same concept to the different derived classes.

Surt
  • 15,501
  • 3
  • 23
  • 39
1

Consider this method:

void foo(base* b) {
    // ...some stuff...
    b->call();               // virtual function call
                             // calls either savings::call or 
                             // current::call, depening on the 
                             // actual type of b
    // ...more stuff...
}

This method does not care whether you pass a pointer to a savings or a pointer to a current. You can call it like that:

savings s;
current c;
foo(&s);
foo(&c);

or if you want the pointer assigment more explicitly:

savings s;
current c;
base* b_ptr = &s;
foo(b_ptr);

b_ptr = &c;
foo(b_ptr);
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • This explain HOW, but not WHY. The OP asked WHY someone would want to do this. You should flesh out the `foo()` implementation to show WHY this could be useful to do. – Remy Lebeau Aug 03 '18 at 18:18
0

By default base class pointer can hold the address of base class as well as derived call we all know. But if we don't use virtual keyword in base class then the base class pointer will call methods based on the type of pointer not on based on the value it holding.

so for achieving polymorphic behavior it must to use virtual in base class. if you need to call different methods based on the type which base class pointer is holding , you need to assign object address to base class pointer.

and also if we have a pure virtual function in a class then it is called abstract class. and the object of abstract class is not created.