Is this a decent example of using virtual functions?

(Sorry if this is considered a duplicate)

I am trying to understand "polymorphing" and so far, the code below is what i have managed so far.

Please correct me if i'm wrong about this, but i'll try to explain virtual functions. A virtual function is a function that can be redefined and also can maintain its parameters when it is redefined in a derived class

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
57
58
59
#include <iostream>
using namespace std;

class Base
{

protected:

    int num1;
    int num2;

public:

    void setMembers(int a, int num)
    {
        num1 = a;
        num2 = num;
    }

    virtual int showMembers ()
    {
        return 0;
    }
};

class Derived : public Base
{

public:

    int showMembers ()
    {
        return (num1 * num2);
    }
};

int main ()
{
    int number;
    int number2;

    Base object1;
    Derived object2;

    cout << "Enter a number" << endl;
    cin >> number;

    cout << "Enter another number" << endl;
    cin >> number2;

    object1.setMembers (number, number2);

    object2.setMembers (number, number2);

    cout << object1.showMembers() << endl;

    cout << object2.showMembers();
}
You have the concept right, but your code doesn't really reflect that - you aren't using polymorphism at all.

Here I'll re-write your code using polymorphism as an example:
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
#include <iostream>

class Base {
    public:
        virtual ~Base() {} // a good idea for polymorphic classes

        void setMembers(int a, int b) {
            num1 = a;
            num2 = b;
        }

        virtual void showMembers() const { 
            return 0; 
        }

    protected:
        int num1 = 0;
        int num2 = 0;
};

class Derived : public Base {
    public:
        void showMembers() const override {    // you don't need the 'override', but I like it.
            return num1 * num2;
        }
};

int main() {
    Base* object1 = new Base;
    Base* object2 = new Derived;

    int num1;
    int num2;
    std::cout << "Enter two numbers: ";
    std::cin >> num1 >> num2;

    object1->setMembers(num1, num2);
    object2->setMembers(num1, num2);

    std::cout << object1->showMembers() << std::endl;
    std::cout << object2->showMembers() << std::endl;

    // EDIT: Make sure you delete your objects properly... (or use smart pointers)
    delete object1;
    delete object2;

    return 0;
}


EDIT:
In essence, for an object to be polymorphic you need to have a pointer/reference to an object, and that is generally reflected through the base class (as shown above).
Last edited on
Ah thank you very much for your response, i finally understand how to "polymorph". But i still have a few questions.

Why should I use the polymorph/virtual functions method? and for what?

What use does it have in high level programming?

EDIT

by the way, are there other ways to "polymorph"? or is using a pointer to a base class(and assigning it the address of a derived class object) to call the functions for both derived and base classes the only way to polymorph?
Last edited on
Generally, you would use polymorphism as one way of having one set of code doing different things, but you don't need to know exactly what is going on. A typical example of this:
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
#include <iostream>

struct Animal {
    virtual ~Animal() {}
    virtual void say() const { std::cout << "ANIMAL"; }
};

struct Dog : Animal {
    void say() const override { std::cout << "DOG"; }
};

struct Cat : Animal {
    void say() const override { std::cout << "CAT"; }
};

void saySomething(const Animal& a) {
    std::cout << "Animal: ";
    a.say();
    std::cout << std::endl;
}

int main() {
    Animal a;
    Cat c;
    Dog d;

    saySomething(a);
    saySomething(c);
    saySomething(d);

    return 0;
}


In the above code, the saySomething function doesn't know (or care) what type of animal it is receiving; it is just repeating the code. This can often help structure code and as a way to add functionality easily without having to modify large portions of your code.

As for how to 'polymorph', generally using a pointer / reference to the base class is the only way to call virtual functions. However, similar effects can often be gotten with the use of templates, through techniques like 'compile-time polymorphism' (rather than 'runtime polymorphism' as above).
Thank you so much! i finally understand polymorphing!
Topic archived. No new replies allowed.