2021年3月31日星期三

What is the specific use of calling derived class object with base class pointer

I have learned a lot about calling the derived class object with the base class pointer. This is possible and how it works. Also learned about object splicing when we do this.

But I am not able to understand why we do this when we can call base object with base class pointer and derived object with the derived class pointer. What is the use of calling derived class objects with the base class pointer?

#include<iostream>  using namespace std;    class Base   { public:      void printBase() { cout<<"Class Base"; }    };    class Derived: public Base  { public:      void printDer() {  cout<<"class Derived"; }    };    int main()  {    Base *bp = new Derived;    bp->printBase();    }    

Here we can create a base class object using a base class pointer. What is the use of calling derived class objects with the base class pointer?

https://stackoverflow.com/questions/66898210/what-is-the-specific-use-of-calling-derived-class-object-with-base-class-pointer April 01, 2021 at 11:54AM

没有评论:

发表评论