virtual vs pure virtual (C++)

virt.cpp

#include <iostream>
using namespace std;


// Keyword virtual

class A{
  /* Pure virtual function (because of the = 0).
   * This does the class abstract, thus no instances
   * of this class can be instantiated.
   * The classes that inherit from this class, must
   * implement the func1(), or else they are abstract too.
   * If ones tries to do A a;
   * he will get this:
   * main.cpp: In function `int main()':
   * main.cpp:17: error: cannot declare variable `a' to be of type `A'
   * main.cpp:17: error:   because the following virtual functions are abstract:
   * main.cpp:12: error:  virtual void A::func1(int)
   */ 
    
public:
    virtual void func1(int a) = 0;
};

// class B inherits from A
class B : public A{
public:
    // Define pure virtual functions of base class A.
    virtual void func1(int a) {
        cout << "Hello from class B, func1(),  with a = " << a << endl;
    }
    
    /* A member (e.g. a function) of a class that can be redefined in its derived
     * classes is known as a virtual member. In order to declare a member of a 
     * class as virtual, we must precede its declaration with the keyword virtual.
     */
    
    /* func2() is virtual (not pure virtual), thus we instantiate an instance of 
     * class B.
     * If a class inherits from B, then it can redefine func2(). If it does, 
     * the new implementation is taken into account. If not, the previous 
     * implementation is taken into account.
     */
    virtual void func2() { 
        cout << "Hello from class B, func2()" << endl;
    }
    
    virtual void func3() { 
        cout << "Hello from class B, func3()" << endl;
    }
};

// class C inherits from B
class C : public B{
public:    
    /* Redefine func3(). Thus when we call func3() from an instance of class C,
     * the function below will be executed.
     * Notice, that func2() and func1() are inherited, from class B.
    */
    virtual void func3() { 
        cout << "Hello from class C, func3()" << endl;
    }
};

int main()
{
    // A a; <-- error, class A is abstract
    B b;    // class B inherits from class A,
    // but implements the pure virtual function(s)
    // of base class (here class A).
    
    b.func1(5);   // Hello from class B, func1(),  with a = 5
    b.func2();    // Hello from class B, func2()
    b.func3();    // Hello from class B, func3()
    
    C c; 
    
    c.func1(4000); // Hello from class B, func1(),  with a = 4000
    c.func2();          // Hello from class B, func2()
    c.func3();          // Hello from class C, func3()

    return 0;
}

This code was developed by me, G. Samaras.

Have questions about this code? Comments? Did you find a bug? Let me know!😀
Page created by G. (George) Samaras (DIT)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s