virtual function,pure virtual function, abstract class

      Comments Off on virtual function,pure virtual function, abstract class

virtual function: Indicate that the function can be override, and enable polymorphism.

class A{
   virtual void show(){ std::cout <<"A show" <<std::endl;
};
class B{
   virtual void show(){std::cout << "B show" <<std::endl; }; A *a = new B(); a->show();// result: B show //polymorphism.

pure virtual function: Indicate this function is empty and must be implemented in subsclass. ( Lưu ý không có từ khóa pure trong c++)

    virtual f1(int a,int b)=0; // pure virtual function.

Abstract class:
Là class mà có ít nhất 1 hàm pure virtual function

Nếu A là abstract B kế thừa A nhưng ko implement tất cả các pure virtual function thì B cũng là pure virtual function.

https://www.geeksforgeeks.org/pure-virtual-functions-and-abstract-classes/