The concept of polymorphism inC++
Polymorphism or polymorphism(Polymorphism)It is just a method of writing code that is intended to build a function that executes different commands depending on the object that we pass to it when it is called.
Usually polymorphism is mainly related to inheritance, where the function is based on the parent class, but when we call it we pass an object from one of the classes it inherits from.
How to apply the principle of polymorphism inC++
In the following example, we defined a class whose name Country
is just the basic class for any class that represents a country, and therefore any class we will create to represent a country must inherit from it. In this class we have also prepared 3 abstract functions.
Then we defined a class whose name is a class, and a class Egypt
whose name Australia
inherits from the class Country
and they doOverrideFor all functions that they inherited from him.
Then we created a function named printCountryInfo()
its task to call all the functions in the object that we pass to it provided that this object was created from a class that inherits from the classCountry
.
Finally, we created an object from the class Egypt
and an object from the class Australia
and passed each of them to the functionprintCountryInfo()
.
Example
#include <iostream> using namespace std; // Contains 3 abstract functions Country Here we have created a class called class Country { public: virtual void name() = 0; virtual void capital() = 0; virtual void language() = 0; }; // For the three existing functions that Override inherited from him, and he did Country inherits from the Australia class, here we created an empty class named class Australia : public Country { public: void name() override { cout << "Country: Australia\n"; } void capital() override { cout << "Capital: Canberra\n"; } void language() override { cout << "Language: English\n"; } }; // For the three existing functions that Override inherited from him, and he did Country inherits from the Egypt class, here we created an empty class named class Egypt : public Country { public: void name() override { cout << "Country: Egypt\n"; } void capital() override { cout << "Capital: Cairo\n"; } void language() override { cout << "Language: Arabic\n"; } }; // it executes the three functions in it, Country. When called, we pass it the address of an object that inherits from the printCountryInfo class. void printCountryInfo(Country& country) { country.name(); country.capital(); country.language(); cout << "----------------\n"; } // main() Here we have defined the function int main() { // eg its name is Egypt and a class object au its name is Australiaypt here we created an object of class Australia au; Egypt eg; // In order to call the three functions, au and au, we pass the two objects printCountryInfo() here we call the function printCountryInfo(au); printCountryInfo(eg); return 0; }
• We will get the following result when running.
Country: Australia Capital: Canberra Language: English ---------------- Country: Egypt Capital: Cairo Language: Arabic ----------------
How to apply the principle of polymorphism with matrices inC++
Ordinary arrays cannot store objects of different classes in them and ensure that this command does not cause any problems because it is not configured for this command, so we will use an advanced type of array to handle in order to teach you how to apply the principle of polymorphism with arrays.
One of the first types of advanced matrices is the type vector
, which is the type that we will use shortly, noting that we will explain it in boring detail later in the course.
The object that you create from the class vector
can store an unlimited number of normal values or objects inside it, and whenever you want, you can add more or delete the ones in it, unlike ordinary arrays that you have to specify its size at the moment of its creation.
In order to create objects from classes vector
you must include the class vector
first.
To put any value inside vector
we use the function push_back()
and to delete any value we use the functionerase()
.
In the following example, we have repeated the previous example, but this time in the function, main()
we have created an object from the class vector
named countries
and stored the objects that we created from the class Australia
and Egypt
in it.
Example
#include <iostream> #include <vector> using namespace std; // Contains 3 abstract functions Country Here we have created a class called class Country { public: virtual void name() = 0; virtual void capital() = 0; virtual void language() = 0; }; // For the three existing functions that Override inherited from him, and he did Country inherits from the Australia class, here we created an empty class named class Australia : public Country { public: void name() override { cout << "Country: Australia\n"; } void capital() override { cout << "Capital: Canberra\n"; } void language() override { cout << "Language: English\n"; } }; // For the three existing functions that Override inherited from him, and he did Country inherits from the Egypt class, here we created an empty class named class Egypt : public Country { public: void name() override { cout << "Country: Egypt\n"; } void capital() override { cout << "Capital: Cairo\n"; } void language() override { cout << "Language: Arabic\n"; } }; // it executes the three functions in it, Country. When called, we pass it the address of an object that inherits from the printCountryInfo class. void printCountryInfo(Country& country) { country.name(); country.capital(); country.language(); cout << "----------------\n"; } // main() Here we have defined the function int main() { // eg its name is Egypt and a class object au its name is Australiaypt here we created an object of class Australia au; Egypt eg; // Country we will put in it pointers to objects that inherit from countries called vector Here we have created an object from the class vector<Country*> countries; // countries in the object eg and au here we have added the addresses of the two objects countries.push_back(&au); countries.push_back(&eg); // and pass the address of the object to it printCountryInfo() and then it calls the countries function here we created a loop every time it passes an element in the object for (unsigned i=0; i<countries.size(); i++) { printCountryInfo(*countries[i]); } return 0; }
• We will get the following result when running.
Country: Australia Capital: Canberra Language: English ---------------- Country: Egypt Capital: Cairo Language: Arabic ----------------