User Tools

Site Tools


notes:cprog:fall2021:projects:oop2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
notes:cprog:fall2021:projects:oop2 [2021/11/10 02:15] – [Polymorphism] toverhi1notes:cprog:fall2021:projects:oop2 [2021/11/11 04:58] (current) – [References] hhemler
Line 24: Line 24:
 ===Compile Time Polymorphism=== ===Compile Time Polymorphism===
 Compile time polymorphism is achieved by overloading. This can be in the form of operator overloading, function overloading, or with templates. Compile time polymorphism is achieved by overloading. This can be in the form of operator overloading, function overloading, or with templates.
 +
 +Overloading is takes place when more than one function or operator, with the same name and scope, are given differing definitions.  When this happens, the compiler will determine which definition is most appropriate, which is called overload resolution.  
 +
 +It is called compile-time polymorphism because a function is called at the time of program compilation. 
  
 ==Template Functions== ==Template Functions==
Line 68: Line 72:
 ===Run Time Polymorphism=== ===Run Time Polymorphism===
 Run time polymorphism is achieved by overriding. Overriding occurs when a child class redefines a parent class's member function. Run time polymorphism is achieved by overriding. Overriding occurs when a child class redefines a parent class's member function.
 +
 +In a runtime polymorphism, functions are called at the time of the program's execution.
  
 ==Virtual Functions== ==Virtual Functions==
Line 118: Line 124:
 //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.// //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.  Assigning **public inheritance** will allow all other classes and functions to inherit that data or function.  Assigning **Private inheritance** will restrict access to a class member to functions within that same class.  The only exception taking place when a class is declared as a "friend."  **Protected** is similar to private, with the exception that protected class members can also be accessed by derived classes.  
 +
 +
  
 ====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, this means, that the derived classes are not simply inheriting attributes or methods, but using them in different ways.  The methods and attributes being inherited still depends on that element's access control label.
 +
 +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 'polymorphism' in C++ is called such. Polymorph's word structure would imply multiple shapes, and polymorph in common fantasy/magical settings often relates to something temporarily transforming into a different creature, or shape. This has no relation to the C++ polymorph, which mainly relates to a child class changing an already existing function of its parent classes. It seems that mutation or evolution would be a better term, as if a creature inherited something from its ancestors, such as eyes, and then the child some genetic variation that caused its eyes to be more effective at seeing, that would be a mutation or evolution, we would not call such a thing polymorphing.
 +
 +COOL BUG FACT: After writing this I found that "polymorph" and "polymorphism" have two very different meanings. Polymorphism is the occurrence of different forms among members of a colony, meaning everything I wrote above this is kinda wrong...
 +
 +RELEVANT COOL BUG FACT: If the child class has a child of its own, the polymorphed version of functions will be what the child's child inherits
 =====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 131: Line 150:
   * https://www.learncpp.com/cpp-tutorial/virtual-functions/   * https://www.learncpp.com/cpp-tutorial/virtual-functions/
   * https://www.learncpp.com/cpp-tutorial/function-templates/   * https://www.learncpp.com/cpp-tutorial/function-templates/
 +  * https://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm
 +  * https://www.programiz.com/cpp-programming/access-modifiers
 +  * https://www.w3schools.com/cpp/cpp_polymorphism.asp
 +  * https://www.tutorialspoint.com/cplusplus/cpp_overloading.htm
 +  * https://www.mygreatlearning.com/blog/polymorphism-in-cpp/
 +  * https://www.javatpoint.com/cpp-virtual-function
 =====Submission===== =====Submission=====
 I'll be looking for the following: I'll be looking for the following:
notes/cprog/fall2021/projects/oop2.1636510540.txt.gz · Last modified: 2021/11/10 02:15 by toverhi1