|
1 | 1 | ---
|
2 | 2 | layout: default
|
3 | 3 | title: Polymorphism
|
4 |
| -description: "Warning: The following page is currently under construction, find more about the details in future patches, or if you choose to add in the article see info on the bottom of the page." |
| 4 | +description: An article dicussing "Polymorphism", which means "Many Forms". |
5 | 5 | nav_order: 1
|
6 |
| -parent: Concepts |
| 6 | +parent: Polymorphism |
| 7 | +grand_parent: Concepts |
7 | 8 | has_children: false
|
8 | 9 | ---
|
9 | 10 |
|
10 | 11 | {{ page.title }}
|
11 | 12 | ======================
|
12 | 13 |
|
13 |
| -{% include under_construction.html %} |
| 14 | +{: .warning } |
| 15 | +This article is quite advanced and assumes you have an understanding of Object Oriented Programming. |
| 16 | +If not, please refer to the [OOP](/docs/Concepts/OOP/OOP.md) section of this book! |
14 | 17 |
|
| 18 | +Polymorphism is a fundamental concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables a single interface to be used for entities of different types, promoting flexibility, extensibility, and code reuse. Polymorphism can be achieved through two main mechanisms: compile-time (or static) polymorphism and run-time (or dynamic) polymorphism. |
15 | 19 |
|
16 |
| -<br> |
| 20 | +### 1. Compile-Time Polymorphism |
| 21 | +Compile-time polymorphism is achieved using function overloading and operator overloading. |
17 | 22 |
|
18 |
| -<br> |
| 23 | +#### Function Overloading: |
| 24 | +This allows multiple functions with the same name but different parameter lists to be defined within the same scope. The appropriate function to call is determined by the number and types of arguments passed to it at compile-time. |
| 25 | + |
| 26 | +{: .code } |
| 27 | +``` |
| 28 | +cpp |
| 29 | +// Example of function overloading |
| 30 | +int Add(int A, int B) |
| 31 | +{ |
| 32 | + return A + B; |
| 33 | +} |
| 34 | +
|
| 35 | +float Add(float A, float B) |
| 36 | +{ |
| 37 | + return A + B; |
| 38 | +} |
| 39 | +``` |
| 40 | +In this example, `Add` is overloaded to accept both `int` and `float` arguments. |
| 41 | + |
| 42 | +#### Operator Overloading: |
| 43 | +Operators such as `+`, `-`, `*`, etc., can be overloaded to work with user-defined types in addition to their built-in functionality. |
| 44 | + |
| 45 | +{: .code } |
| 46 | +``` |
| 47 | +cpp |
| 48 | +// Example of operator overloading |
| 49 | +class Complex |
| 50 | +{ |
| 51 | +
|
| 52 | +private: |
| 53 | + float A; |
| 54 | + float B; |
| 55 | +
|
| 56 | +public: |
| 57 | +
|
| 58 | + Complex operator+(const Complex& other) |
| 59 | + { |
| 60 | + Complex temp; |
| 61 | + temp.A = this->A + other.A; |
| 62 | + temp.B = this->B + other.B; |
| 63 | + return temp; |
| 64 | + } |
| 65 | +}; |
| 66 | +``` |
| 67 | + |
| 68 | +Here, the `+` operator is overloaded to add two "Complex" objects. |
| 69 | + |
| 70 | +### 2. Run-Time Polymorphism |
| 71 | +Run-time polymorphism in C++ is achieved through virtual functions and abstract classes (interfaces) using inheritance. |
| 72 | + |
| 73 | +#### Virtual Functions: |
| 74 | +A virtual function is a member function in a base class that you expect to override in derived classes. When you refer to a derived class object using a pointer or reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function. |
| 75 | + |
| 76 | +{: .code } |
| 77 | +``` |
| 78 | +cpp |
| 79 | +// Example of virtual functions |
| 80 | +class Animal |
| 81 | +{ |
| 82 | +
|
| 83 | +public: |
| 84 | +
|
| 85 | + virtual void MakeSound() |
| 86 | + { |
| 87 | + cout << "Animal sound" << endl; |
| 88 | + } |
| 89 | +}; |
| 90 | +
|
| 91 | +class Dog : public Animal |
| 92 | +{ |
| 93 | +
|
| 94 | +public: |
| 95 | +
|
| 96 | + void MakeSound() override |
| 97 | + { |
| 98 | + cout << "Bark!" << endl; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | +In this example, `MakeSound()` is a virtual function in `Animal` which is overridden in `Dog`. |
| 103 | + |
| 104 | +#### Abstract Classes and Pure Virtual Functions: |
| 105 | +An abstract class is a class that has at least one pure virtual function. A pure virtual function is a virtual function that is declared in a base class but has no definition. Abstract classes cannot be instantiated; they are meant to be inherited from by other classes. |
| 106 | + |
| 107 | +{: .code } |
| 108 | +``` |
| 109 | +cpp |
| 110 | +// Example of abstract class and pure virtual function |
| 111 | +class Shape |
| 112 | +{ |
| 113 | +
|
| 114 | +public: |
| 115 | +
|
| 116 | + virtual void Draw() = 0; // Pure virtual function |
| 117 | + virtual float Area() const = 0; // Another pure virtual function |
| 118 | +}; |
| 119 | +
|
| 120 | +class Circle : public Shape |
| 121 | +{ |
| 122 | +
|
| 123 | +public: |
| 124 | +
|
| 125 | + void Draw() override |
| 126 | + { |
| 127 | + cout << "Drawing Circle" << endl; |
| 128 | + } |
| 129 | +
|
| 130 | + float Area() const override |
| 131 | + { |
| 132 | + return 3.14 * radius * radius; |
| 133 | + } |
| 134 | +
|
| 135 | +private: |
| 136 | +
|
| 137 | + float Radius; |
| 138 | +}; |
| 139 | +``` |
| 140 | +Here, `Shape` is an abstract class with pure virtual functions `Draw()` and `Area()`. `Circle` inherits from `Shape` and provides implementations for these functions. |
| 141 | + |
| 142 | +### Benefits of Polymorphism |
| 143 | +- Flexibility and Extensibility: Code written using polymorphism can be easily extended by adding new classes that implement the same interfaces or inherit from the same base class. |
| 144 | +- Simplicity and Maintainability: Polymorphism allows for cleaner and more modular code since it encourages reusability and reduces duplication. |
| 145 | +- Encapsulation: By using polymorphism, you can hide the implementation details of classes and expose only the required functionality through interfaces. |
| 146 | + |
| 147 | +### Considerations and Best Practices |
| 148 | +- Use Interfaces When Appropriate. If you want to define a common interface without any implementation details, use abstract classes (interfaces) with pure virtual functions. |
| 149 | +- Avoid Slicing: When working with polymorphic objects through pointers or references to the base class, be mindful of object slicing, where the derived class's specific attributes can be lost if assigned to a base class object. |
| 150 | + |
| 151 | +Polymorphism is a powerful concept that enables elegant and efficient design of object-oriented systems. It promotes code reusability, maintainability, and flexibility, making it a cornerstone of modern software development practices. |
| 152 | + |
| 153 | +--- |
| 154 | + |
| 155 | +#### Author: JDSherbert |
| 156 | +#### Published: 04/07/2024 |
0 commit comments