virtual

14.1. Polymorphism

There are three types of polymorphism

  • Overloading functions and operators
  • Inherritance
  • virtual function and abstract class

14.2. Virtual functions

virtual function ทำให้ polygon มีฟังก์ชัน area() ซึ่งจะกำหนดอีกครั้งหลังการสืบทอด

        #include <iostream>
        using namespace std;

        class Polygon {
          protected:
            int width, height;
          public:
            void set_values (int a, int b)
              { width=a; height=b; }
            virtual int area ()
              { return 0; }
        };

        class Rectangle: public Polygon {
          public:
            int area ()
              { return width * height; }
        };

        class Triangle: public Polygon {
          public:
            int area ()
              { return (width * height / 2); }
        };

        int main () {
          Rectangle rect;
          Triangle trgl;
          Polygon poly;
          Polygon * ppoly1 = &rect;
          Polygon * ppoly2 = &trgl;
          Polygon * ppoly3 = &poly;
          ppoly1->set_values (4,5);
          ppoly2->set_values (4,5);
          ppoly3->set_values (4,5);
          cout << ppoly1->area() << '\n';
          cout << ppoly2->area() << '\n';
          cout << ppoly3->area() << '\n';
          return 0;
        }

output

20
10
0

Accessing the virtual function

        #include <iostream>
        class Base {
        public:
           virtual void f() {
               std::cout << "base\n";
           }
        };
        struct Derived : public Base {
            public:
            void f() override { // 'override' is optional
                std::cout << "derived\n";
            }
        };
        int main()
        {
            Base b;
            Derived d;

            // virtual function call through reference
            Base& br = b; // the type of br is Base&
            Base& dr = d; // the type of dr is Base& as  well
            br.f(); // prints "base"
            dr.f(); // prints "derived"

            // virtual function call through pointer
            Base* bp = &b; // the type of bp is Base*
            Base* dp = &d; // the type of dp is Base* as  well
            bp->f(); // prints "base"
            dp->f(); // prints "derived"

            // non-virtual function call
            br.Base::f(); // prints "base"
            dr.Base::f(); // prints "base"
        }

output


Virtual function as Overriding

Difinition from Cambridge dictionary:
override verb (CONTROL) : to take control over something, especially in order to change the way it operates.

14.3. Abstract class

An abstract class is unable to construct an object. The "=0" indicates a null function. Therefore, the function member is incomplete requiring further implementation.

        #include <iostream>
        using namespace std;
        class Base{
            public:
            virtual void print()=0;
        };
        class Derived:public Base{
            public:
            void print(){
                cout<<"From Derived"<<endl;
            }
        };
        int main(){
            Base * pb= new Derived();
            //Base * pb2= new Base(); //cannot allocate an object of abstract type 'Base'
            pb->print();
            return 0;
        }

14.4. Virtual destructor

        #include <iostream>
        using namespace std;
        class Base{

        public:
            Base(){cout<<"Base Constructor Called\n";}
            virtual ~Base(){cout<<"Base Destructor called\n";}

        };
        class Derived:public Base{

        public:
            Derived(){cout<<"Derived constructor called\n";}
            ~Derived(){cout<<"Derived destructor called\n";}

        };
        int main(){

            base* b = new Derived;
            delete b;

        }

results matching ""

    No results matching ""