classCounterBase{public:CounterBase()=default;virtual~CounterBase()=default;virtualvoidIncrement(){ count_++;}intcount()const{return count_;}protected:int count_ =0;};classCounterDouble:publicCounterBase{public:CounterDouble():CounterBase(){}virtual~CounterDouble()=default;voidIncrement(){ count_ +=2;}};intmain(){{
CounterDouble counter_double;
CounterBase& counter_base = counter_double;// Polymorphism with reference.
counter_base.Increment();
cout << counter_base.count()<< endl;// 2.}{
CounterDouble counter_double;
CounterBase* counter_base =&counter_double;// Polymorphism with pointer.
counter_base->Increment();
cout << counter_base->count()<< endl;// 2.}// If there is no virtual for the function in base class, there will be no polymorphism.// Dynamic binding condition:// (1) virtual function// (2) inherit// (3) assign derive type to base type with pointer or reference.return0;}