Question about Polymorphism

Please take a look at the code below. I wonder what happens when the virtual keyword is not in the base class but in the derived class? will the run() function from class A override the run() function in class B?

Also what I don't understand is the line: A *a = new C();;
Does that make a of Class A or C? and why does it use two cast operators, static and dynamic? the cast operator takes the argument (a and b) as of which class type?

Sorry for a bit more questions, this virtual and casting things are so confusing.
Thanks.

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

    class A { 
    public:
        A() : val(0) {}
        int val;
        void run() { cout << val; } 
    };

    class B : public A {
    public:
        virtual void run() { cout << val + 2; } 
    };

    class C : public B {
    };

    void Do(A *a) {
        B *b;
        C *c;
        if(b = static_cast<B *>(a))
            b->run();
        if(c = dynamic_cast<C *>(b))
            c->run();
        a->run();
    }

    int main() {
        A *a = new C();;
        Do(a);
        return 0;
    }
Topic archived. No new replies allowed.