Polymorphism is a c++ skill to make many classes that are related to each other by being derived from the same class. Polymorphism means "many forms".
Polymorphism let different objects to respond the same message differently.
Note that the above description is just the original polymorphism, which is inheritance polymorphism. c++ has many ways to implement polymorphism, not only inheritance.
This article talks inheritance polymorphism only.
Step-by-step to write a simple inheritance polymorphism c++ program.
Write the base class, and its method: echo.
echo will be used as the polymorphism response action.
#include <iostream> class base_class { public: virtual void echo() const { std::cout << "echo echo, I am base" << std::endl; } };
Tip | |
---|---|
virtual |
Specifier: virtual
You have noticed that the method echo is marked virtual.
To make polymorphism c++ work, the related method of the base class must be marked virtual .
Write derived class: derived_A, and its method echo,
Note that the method name echo is the same as the related method name of its base class.
And the arguments of echo have to be the same too. In this example, it has no arguments.
And the method const must be the same.
class derived_A: public base_class { public: void echo() const override { std::cout << "echo echo, I am derived_A" << std::endl; } };
To make it polymorphism, the same name method of the derived class and the method of that name of the base class must meet the following conditions:
And also because c++ overloading feature, so if they have a little difference, it makes a different method and it will not polymorphism.
To avoid that mistake, you can add the keyword override
at the end of the method_name() declaration,
then the compiler will look up that if an overrode method exists on the base
class, if not found or mismatched, the compiling will report errors, then you
will know what's up. No keyword override
, no compiling
checking.
Do the same to derived_B.
Note that derived_B and derived_A are derived from the same class.
class derived_B: public base_class { public: void echo() const override { std::cout << "echo echo, I am derived_B" << std::endl; } };
Make a polymorphism object:
How to implement point 1 and point 2 simultaneously ?
There are two ways:
derived_A a_obj; derived_B b_obj; base_class & base_r = a_obj; base_class & base_s = b_obj; base_r.echo(); base_s.echo();
You see, the type of base_r and base_s are base class type: base_class, but they are bound to the objects of derived class derived_A and derived_B.
And base_r.echo();
will
call the method echo from derived_A,
base_s.echo();
will call the method echo from derived_B.
Similar to reference, pointer can be used for polymorphism too.
base_class * base_ptr = new derived_A; base_ptr->echo(); delete base_ptr; base_ptr = new derived_B; base_ptr->echo(); delete base_ptr;
// reference void call_echo_1(base_class & object) { object.echo(); } // pointer void call_echo_2(base_class * object) { object->echo(); } derived_A aa_obj; derived_B bb_obj; call_echo_1(aa_obj); call_echo_1(bb_obj); call_echo_2(&aa_obj); call_echo_2(&bb_obj);
Write a complete inheritance polymorphism c++ program that can be compiled, use both reference and pointer to do the polymorphism response action. Check, modify and compile the code by yourself.
You can also send your exercise code to the discord server to let others check
for you. If you send this exercise code to discord server, the following rules
must be followed:
(Discord server can be found on the foot of this page)
Written on Nov 05, 2024
c++ std::exception:
std::cout.write(err.data(), err.size());
std::cout << std::endl;
caught:
================================================== # The c++ programming language. # # # # Home: cppfx.xyz # # Join c++ Discord: yZcauUAUyC # # Deck # ==================================================