inheriting virtual function call code
This address: Http://blog.csdn.net/caroline_wendy
When a derived class inherits a base class , it is dynamically bound when using virtual , and is overwritten without the virtual keyword .
When you use a base class pointer (base*) to point to a derived class , the function is called by a dynamically bound function that calls the derived class, and the function that is not dynamically bound calls the base class .
Code:
/* * main.cpp * * Created on:2014.9.12 * author:spike *//*eclipse CDT, gcc 4.8.1*/#include <iostream>using Namespace Std;class Base {public:base (int J): I (j) {}virtual ~base () {}void func1 () {i*=10; Func2 ();} int GetValue () {return i;} protected:virtual void Func2 () {i++;} Protected:int i;}; Class Child:public Base {public:child (int J): Base (j) {}void func1 () {i*=100; Func2 ();} Protected:void Func2 () {i+=2;}}; int main (void) {base* PB = new Child (1);p b->func1 (); cout << pb->getvalue () << Endl;return 0;}
Output:
12
C + +-inherit virtual function call code