pure virtual function query

if my interface header is like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class MyMathFuncs
    {
    public:
        // Returns a + b
        virtual double Add(double a, double b)=0;

        // Returns a - b
        virtual double Subtract(double a, double b)=0;

        // Returns a * b
       virtual double Multiply(double a, double b)=0;

        // Returns a / b
        // Throws DivideByZeroException if b is 0
       virtual double Divide(double a, double b)=0;
    };


Would I have to write the key word virtual when I define it threw inheritance?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 class MyMathFuncs:public MyMathFuncs
    {
    public:
        // Returns a + b
        static __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        static __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        static __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        static __declspec(dllexport) double Divide(double a, double b);
    };
static functions and virtual functions are different functions.
Last edited on
Hi Blessman11,

virtual is not necessary in the derived class.

Just wondering why you need to have them as pure virtual functions, this means you have to redefine them for every derived class. If you don't, then you won't be able to create an object for that class because it is "incomplete". You may well have a valid reason for doing this, I am just wondering.
+1 @ vlad.

virtual functions cannot be static. A virtual function needs an instance of an object so it can know which version to call. static functions by definition have no instance. Therefore the two concepts are mutually exclusive.
Topic archived. No new replies allowed.