Virtual functions in "Coral Sea"

In C++, there are three levels of relationship between base classes and inherited classes.

1: Overloading. For overloading, the scope must be the same, the function name is the same, and the parameter list of the function is different.

2: Hidden. The so-called hiding is that the derived class hides the function of the same name of the base class. Call this on a derived class object

 When a function is used, only functions of derived classes can be called. The functions of the base class are hidden.

3: Overlay. When virtual (that is, virtual) is added in front of the function name of the base class, the function becomes a virtual function.

If the derived class has the same name, the same parameters, and the same return value, the function becomes a virtual function.

This is coverage.

E.g:

#include<iostream>

using namespace std;

class A

{
public:
A(int val = 12){}
virtual void show()
{
cout << "A is show()" << endl;
cout << ma <<endl;
}
protected:
int ma;
};
class B:public A
{
public:
B(int size = 10) :mb(size), A(size){}
void show()
{
cout << "B is show()" << endl;
cout << mb << endl;
}
private:
int mb;
};
int main()
{
B b;
A *p =&b;
p->show();
return 0;
}

The memory layout for class A and class B is:






Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324784638&siteId=291194637