Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Polymorphism Content #104

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 143 additions & 5 deletions docs/Concepts/Polymorphism/Polymorphism.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,156 @@
---
layout: default
title: Polymorphism
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."
description: An article dicussing "Polymorphism", which means "Many Forms".
nav_order: 1
parent: Concepts
parent: Polymorphism
grand_parent: Concepts
has_children: false
---

{{ page.title }}
======================

{% include under_construction.html %}
{: .warning }
This article is quite advanced and assumes you have an understanding of Object Oriented Programming.
If not, please refer to the [OOP](/docs/Concepts/OOP/OOP.md) section of this book!

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.

<br>
### 1. Compile-Time Polymorphism
Compile-time polymorphism is achieved using function overloading and operator overloading.

<br>
#### Function Overloading:
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.

{: .code }
```
cpp
// Example of function overloading
int Add(int A, int B)
{
return A + B;
}

float Add(float A, float B)
{
return A + B;
}
```
In this example, `Add` is overloaded to accept both `int` and `float` arguments.

#### Operator Overloading:
Operators such as `+`, `-`, `*`, etc., can be overloaded to work with user-defined types in addition to their built-in functionality.

{: .code }
```
cpp
// Example of operator overloading
class Complex
{

private:
float A;
float B;

public:

Complex operator+(const Complex& other)
{
Complex temp;
temp.A = this->A + other.A;
temp.B = this->B + other.B;
return temp;
}
};
```

Here, the `+` operator is overloaded to add two "Complex" objects.

### 2. Run-Time Polymorphism
Run-time polymorphism in C++ is achieved through virtual functions and abstract classes (interfaces) using inheritance.

#### Virtual Functions:
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.

{: .code }
```
cpp
// Example of virtual functions
class Animal
{

public:

virtual void MakeSound()
{
cout << "Animal sound" << endl;
}
};

class Dog : public Animal
{

public:

void MakeSound() override
{
cout << "Bark!" << endl;
}
};
```
In this example, `MakeSound()` is a virtual function in `Animal` which is overridden in `Dog`.

#### Abstract Classes and Pure Virtual Functions:
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.

{: .code }
```
cpp
// Example of abstract class and pure virtual function
class Shape
{

public:

virtual void Draw() = 0; // Pure virtual function
virtual float Area() const = 0; // Another pure virtual function
};

class Circle : public Shape
{

public:

void Draw() override
{
cout << "Drawing Circle" << endl;
}

float Area() const override
{
return 3.14 * radius * radius;
}

private:

float Radius;
};
```
Here, `Shape` is an abstract class with pure virtual functions `Draw()` and `Area()`. `Circle` inherits from `Shape` and provides implementations for these functions.

### Benefits of Polymorphism
- 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.
- Simplicity and Maintainability: Polymorphism allows for cleaner and more modular code since it encourages reusability and reduces duplication.
- Encapsulation: By using polymorphism, you can hide the implementation details of classes and expose only the required functionality through interfaces.

### Considerations and Best Practices
- Use Interfaces When Appropriate. If you want to define a common interface without any implementation details, use abstract classes (interfaces) with pure virtual functions.
- 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.

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.

---

#### Author: JDSherbert
#### Published: 04/07/2024
19 changes: 14 additions & 5 deletions docs/IDE/Notepad++/Notepad++.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
layout: default
title: Notepad++
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."
description: An article dicussing features, key releases and getting started topics about Notepad++
nav_order: 1
parent: IDE
has_children: false
Expand All @@ -10,10 +10,19 @@ has_children: false
{{ page.title }}
======================

{% include under_construction.html %}

Download: https://notepad-plus-plus.org/

<br>
Notepad++ is a free and open-source text and source code editor for Windows. It is extremely lightweight and is very useful for quick code edits, psuedocoding, or note taking. Notepad++ has a wide support for a wide variety of programming languages. Itsupports syntax highlighting and folding for programming languages (e.g., C++, Python, HTML, CSS, JavaScript). The highlighting feature makes the code more readable by coloring different parts of the syntax, while folding allows users to collapse sections of code to make it easier to manage large files.

It's worth knowing that Notepad++ is primarily a text editor. It does not come with built-in support for compiling or debugging code. Users will need to use external compilers and debuggers, which can be less convenient than integrated development environments (IDEs) like Visual Studio, that provide these tools out of the box. Unlike full-fledged IDEs, Notepad++ also lacks features like project management, integrated version control, and direct execution of code.
This doesn't make it any less useful!

To use notepad, simply download it from the link above, and then install it. Run it and start writing! No other nonsense involved!

![image](https://github.com/JDSherbert/Programming_HandBook/assets/43964243/306f8c72-8027-415c-a00e-13eba781684a)


---

<br>
#### Author: JDSherbert
#### Published: 24/06/2024
Loading