The inheritance and polymorphism in C + + is an important point to explain the characteristics of inheritance and polymorphism. Inheritance is a subclass of the use of the members of the base class and member functions, the way of inheritance is divided into public,protected and private, the main explanation today is polymorphic, polymorphic is the base class function must have virtual. And a pointer to the parent class that points to the child class object, for example:
#include <iostream>
#include <string>
using namespace Std;
//
Class A
{
Public
A ()
{
cout << "A ()" << Endl;
}
~a ()
{
cout << "~a ()" << Endl;
}
virtual void fun1 ()
{
cout << "a::fun1 ()" << Endl;
}
virtual void fun2 ()
{
cout << "a::fun2 ()" << Endl;
}
int A;
};
Class A1:public A
{
Public
A1 ()
{
cout << "A1 ()" << Endl;
}
~A1 ()
{
cout << "~a1 ()" << Endl;
}
virtual void fun1 ()
{
cout << "a1::fun1 ()" << Endl;
}
void Fun2 ()
{
cout << "a1::fun2 ()" << Endl;
}
int A1;
};
void Show (A *PB)
{
Pb->fun1 ();
}
int main ()
{
A;
A1 A1;
Show (&A1);
/*A1.FUN1 (); */
/*cout << sizeof (A1) <<endl;*/
GetChar ();
return 0;
}
A pointer to the parent class in the show function will produce a function of the parent class, and a pointer to the child class will produce a function of the subclass, which is polymorphic.
This article is from the "local and static variables" blog, so be sure to keep this source http://10810512.blog.51cto.com/10800512/1754279
Inheritance and polymorphism in C + +