0

I would like to get the exact terminology for the following in C++.

Please consider the following toy example:

class A{
public:
   virtual void f(){ std::cout << "This is A" << std::endl; };
};

class B: public A{
public:
   virtual void f(){ std::cout << "This is B" << std::endl; };
};

int main(int argc, char** argv){
   A* ptr = new A(); //Base class pointer pointing to base class object
   ptr->f(); //Base class method is called
   delete ptr;
   ptr = new B(); ////Base class pointer pointing to derived class object
   ptr->f(); //Derived class method is called
   delete ptr;
   return 0;
}

The output is: This is A This is B

I would like to know the exact C++ terminology for this. Is it polymorphism or method overriding or something else?

The main thing is that I am using a base class pointer. When the pointer points to a base class object, the base class method is called. When it points to the derived class object, the derived class method is called, given the method is declared as virtual in both the classes.

Thanks

4 Answers4

4

It is both, polymorphism and method-overriding:

Polymorphism: The ability (in programming) to present the same interface for differing underlying forms (data types).

Example of the Shape class and all the classes that can inherit from it (square, circle, etc...).

Method-Overriding: It's a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its superclasses or parent classes.

In your case, B is a specific form of A (polymorphism). And B provide a specific implementation of the method f() already implemented in the base class (method-overridding).


And not about the question, but your code is leaking:

int main(int argc, char** argv)
{
   A* ptr = new A(); //Base class pointer pointing to base class object
   ptr->f(); //Base class method is called
   delete ptr;
// ^^^^^^^^^^^
   ptr = new B(); ////Base class pointer pointing to derived class object
   ptr->f(); //Derived class method is called
   delete ptr;
// ^^^^^^^^^^^
   return 1;
   //     ^ Indicate that your program failed ?
}

Don't forget to deallocate memory allocated dynamically.

And when it's possible don't use new/delete at all. Here is an explanation.

The return 1; indicates that your program failed too.

Community
  • 1
  • 1
Pierre Fourgeaud
  • 13,652
  • 1
  • 34
  • 58
1

This is both polymorphism and overriding: overriding is a specific use of polymorphism.

Jean Logeart
  • 48,718
  • 10
  • 76
  • 108
0

It is polymorphism, and memory-leaking.

Would one delete have been too much to ask?

int main(int argc, char** argv){
   A* ptr = new A(); //Base class pointer pointing to base class object
   ptr->f(); //Base class method is called
   delete ptr;  // Free up allocated memory.
   ptr = new B(); ////Base class pointer pointing to derived class object
   ptr->f(); //Derived class method is called
abelenky
  • 58,532
  • 22
  • 99
  • 149
0

Yes, that is polymorphism. But you can still do the same and only with a few changes in your code using C++11 features (if those are available to your compiler) turn it less prone to errors such as memory leaks.

class A {
public:
   virtual void f() { std::cout << "This is A" << std::endl; };
};

class B: public A {
public:
   virtual void f() override { std::cout << "This is B" << std::endl; };
};

int main()
{
   auto ptr = std::unique_ptr<A>(new A());
   ptr->f();
   ptr = std::unique_ptr<B>(new B());
   ptr->f();

   return 0;
}
Marius Bancila
  • 15,287
  • 7
  • 44
  • 84