This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
notes:cprog:fall2021:projects:oop2 [2021/10/18 13:27] – created wedge | notes:cprog:fall2021:projects:oop2 [2021/11/11 04:58] (current) – [References] hhemler | ||
---|---|---|---|
Line 20: | Line 20: | ||
====Polymorphism==== | ====Polymorphism==== | ||
+ | Polymorphism, | ||
+ | |||
+ | ===Compile Time Polymorphism=== | ||
+ | Compile time polymorphism is achieved by overloading. This can be in the form of operator overloading, | ||
+ | |||
+ | Overloading is takes place when more than one function or operator, with the same name and scope, are given differing definitions. | ||
+ | |||
+ | It is called compile-time polymorphism because a function is called at the time of program compilation. | ||
+ | |||
+ | ==Template Functions== | ||
+ | Templates are used to create functions that work with multiple types. At compile time the compiler will substitute the template type with the type given at the function' | ||
+ | |||
+ | <code cpp> | ||
+ | #include < | ||
+ | |||
+ | struct Foo; | ||
+ | |||
+ | template< | ||
+ | const char* is_nullptr(T var) | ||
+ | { | ||
+ | if (var == nullptr) | ||
+ | return " | ||
+ | else | ||
+ | return "NOT nullptr"; | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | int x{ 5 }; | ||
+ | int* xPtr{ &x }; | ||
+ | long* nullPtr{ nullptr }; | ||
+ | Foo* fooPtr{ nullptr }; | ||
+ | |||
+ | std:: | ||
+ | std:: | ||
+ | std:: | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | In this example you can call is_nullptr with any type, at compile time the compiler will create three separate functions: one that takes int pointers, one that takes long pointers, and one that takes Foo pointers. | ||
+ | |||
+ | This example produces this result: | ||
+ | <cli> | ||
+ | xPtr is NOT nullptr | ||
+ | nullPtr is nullptr | ||
+ | fooPtr is nullptr | ||
+ | </ | ||
+ | Templates help clean up your code and can be extremely powerful, especially when combined with type traits. | ||
+ | |||
+ | ===Run Time Polymorphism=== | ||
+ | Run time polymorphism is achieved by overriding. Overriding occurs when a child class redefines a parent class' | ||
+ | |||
+ | In a runtime polymorphism, | ||
+ | |||
+ | ==Virtual Functions== | ||
+ | Using the __virtual__ keyword marks your functions as virtual. Virtual functions always call the most derived version of a member function. | ||
+ | |||
+ | <code cpp># | ||
+ | |||
+ | struct Rectangle | ||
+ | { | ||
+ | const char* type() const { return " | ||
+ | }; | ||
+ | |||
+ | struct Square : Rectangle | ||
+ | { | ||
+ | const char* type() const { return " | ||
+ | }; | ||
+ | |||
+ | void print_type(const Rectangle& | ||
+ | { | ||
+ | std:: | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | const Rectangle rect; | ||
+ | const Square square; | ||
+ | |||
+ | std:: | ||
+ | print_type(rect); | ||
+ | std:: | ||
+ | print_type(square); | ||
+ | |||
+ | return 0; | ||
+ | } | ||
+ | </ | ||
+ | Running this code produces this result: | ||
+ | <cli> | ||
+ | Rectangle: Rectangle | ||
+ | Square: Rectangle | ||
+ | </ | ||
+ | However, with the addition of the __virtual__ keyword: | ||
+ | <code cpp> | ||
+ | virtual const char* type() const { return " | ||
+ | </ | ||
+ | We now get the desired result: | ||
+ | <cli> | ||
+ | Rectangle: Rectangle | ||
+ | Square: Square | ||
+ | </ | ||
+ | //Notice objects deriving from Rectangle are passed by reference because if you pass by value a new Rectangle object will be created, therefore using Rectangle methods.// | ||
====Access Control implications==== | ====Access Control implications==== | ||
+ | |||
+ | Access control is important in polymorphism as it restricts or allows access to class members. | ||
+ | |||
+ | |||
====Parent-Child Relationships==== | ====Parent-Child Relationships==== | ||
+ | Parent child relationships refer to base classes and the derived classes that inherit from them. In regards to polymorphism, | ||
+ | |||
+ | Inheritance lets us inherit attributes and methods from another class. Polymorphism uses those methods to perform different tasks. | ||
+ | ====Similarities to word origins==== | ||
+ | It is strange that what is called ' | ||
+ | |||
+ | COOL BUG FACT: After writing this I found that " | ||
+ | |||
+ | RELEVANT COOL BUG FACT: If the child class has a child of its own, the polymorphed version of functions will be what the child' | ||
=====Program===== | =====Program===== | ||
Write a program that makes use of polymorphism. It can be anything, so long as you genuinely implement it and it works. | Write a program that makes use of polymorphism. It can be anything, so long as you genuinely implement it and it works. | ||
Line 32: | Line 148: | ||
=====References===== | =====References===== | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * https:// | ||
=====Submission===== | =====Submission===== | ||
I'll be looking for the following: | I'll be looking for the following: |