summaryrefslogtreecommitdiffstats
path: root/C++/Virtual Functions/VirtualFunctions.cpp
blob: 57fafca61c63903cf262c09653925459688f812d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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/>.
   */