C++ polymorphic keyword virtual virtual function

1. Static and dynamic: operator parameters, function overloading

2. Dynamic polymorphism: subclasses and virtual functions realize runtime polymorphism

class A

{

public:

         virtual void add()//Declare a virtual function, which can be overridden by subclasses

{

}

}

 

class B:public:A//Inherit class A

{

public:

       virtual void add()//Rewrite the add function of the parent class A virtual can be omitted

{

}

}

int main ()

{

B b;

A&a=b;//The parent class reference points to the child class object

 

}

 

Guess you like

Origin blog.csdn.net/qq_40776805/article/details/108446407