summaryrefslogtreecommitdiffstats
path: root/C++/Virtual Functions
diff options
context:
space:
mode:
Diffstat (limited to 'C++/Virtual Functions')
-rw-r--r--C++/Virtual Functions/INSTRUCTIONS34
-rw-r--r--C++/Virtual Functions/VirtualFunctions.cpp56
2 files changed, 90 insertions, 0 deletions
diff --git a/C++/Virtual Functions/INSTRUCTIONS b/C++/Virtual Functions/INSTRUCTIONS
new file mode 100644
index 0000000..cbd1553
--- /dev/null
+++ b/C++/Virtual Functions/INSTRUCTIONS
@@ -0,0 +1,34 @@
+
+HW3: Virtual Functions
+Due November 29th, 2022 before midnight
+50 Points
+Virtual functions are only introduced in this course. You will use them in data structures instead. Therefore, this assignment is meant to let you become familiar with the concept before you move onto that class.
+In this assignment, you will watch the videos about virtual functions. Then, you will answer the below questions:
+
+ What is a virtual function?
+ What is the purpose of a virtual function?
+ Write about a real world situation where you would need to use a virtual function in a program
+ Write a program of your choice demonstrating how a virtual function works
+
+Assignment Notes:
+
+ Look up information using any resource you wish, but you must write your answers in your own words. Copy/paste = 0/50
+ Since you are writing a short program to demonstrate virtual functions, you may put answers to questions 1 - 3 as comments within your program
+ Short/vague answers will receive few, if any, points
+
+General Notes:
+Be sure to use comments in your program: Name, Program Description, Date and anywhere else in the program you deem necessary.
+Grading Rubric:
+
+ If you do not include comments at the top of the program (name, program description, date), you will lose 15 points
+ If your program is not object-oriented, you will receive a 0/50(OOP is requred for this assignment)
+ If your work is late (within 48 hours of the due date), you will lose 25 points
+ If your program is late beyond the 48 hour due date, I will typically still accept it, but you will lose far more points. Depends on when you turn it in
+ If your program is not formatted nicely (code all over the place, ugly), you will lose up to 25 points depending on the extent
+ If your program stops working when I run it, you will lose points. The exact amount depends on the severity of the error
+ If your program still has your friend's name on it, I will send you a message asking you to try harder while giving you a 0/50
+ If you submit a file type I cannot open, such as .sln, you will receive a 0/50. You will be able to resubmit for credit, but you will lose points (depending on how late it is)
+ If your program looks like a a professional programmer wrote it, I will write to you to ask if you want a job. Well, maybe not. But, I will ask about the code
+ This is just a list of typical issues I run into when grading to give you some idea of where your points go. Points can be taken off for other reasons.
+
+
diff --git a/C++/Virtual Functions/VirtualFunctions.cpp b/C++/Virtual Functions/VirtualFunctions.cpp
new file mode 100644
index 0000000..57fafca
--- /dev/null
+++ b/C++/Virtual Functions/VirtualFunctions.cpp
@@ -0,0 +1,56 @@
+// Name: msglm
+// Date: Nov 25th 2022
+// Program Name: Virtual Functions
+// Description: a look into virtual functions
+
+
+// What is a virtual function?
+//Virtual fucntions are functions that can be redefined and are primarily used for runtime polymorphism. Typically, these functions are accessed via pointers.
+
+// What is the purpose of a virtual function?
+//From what I have seen, the only utility this can serve is for situations when you making an instance of a parent class point to the child class and you need that pointer to call a standardized function within the class' children. I hope that makes sense.
+
+// Write about a real world situation where you would need to use a virtual function in a program
+//The only time I could forsee teaching this is if it's necessary for cirriculum because, as it stands, this is a disgusting enigma of C++ that should not exist. However, to come up with a scenario, let's say this happens:
+//For some reason, we are creating objects in memory in a program and later allocating them. In such as situation, we are dealing with multiple classes of somewhat related concepts, such as animals. All animals have the class "consume", which is unique per animals since animals are of different sizes and can consume different things. The pointer that we created before can then be assigned to an animal and then be used to call the specific consume function of an animal.
+//However, if this is our implementation, then something has gone terribly wrong. Anything universal should be outside of an object and, any objects should be represented by data that is managed by the rest of the program. I.e, an animal should just be data about the animal, the animal itself and the animal's target is passed to a master function that will figure out what happens, and from there the function spits out what happens. If your language supports UFCS (https://en.wikipedia.org/wiki/Nim_(programming_language)#Uniform_Function_Call_Syntax), you can even pretend the master function is possessed by the animal if it makes it easier to think about.
+
+// Write a program of your choice demonstrating how a virtual function works
+
+#include <iostream>
+#include <string>
+using namespace std;
+
+//classes
+class animal {
+ public:
+ int size;
+ virtual void consume(int targetSize) {
+ cout << "this function shouldn't be called, something went wrong";
+ }
+};
+
+class pidgon : public animal {
+ public:
+ void consume(int targetSize) {
+ if (targetSize < size) {
+ cout << "target was consumed\n";
+ }
+ }
+};
+
+int main() {
+ animal *placeholder;
+ pidgon seaPidgon;
+ placeholder = &seaPidgon;
+ placeholder->size = 500;
+ placeholder->consume(400);
+
+}
+/*
+ This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */