Not long ago, a bit of Jinshan, one side more smoothly. Two face when the interviewer mainly asked the question of C + +, seemingly the answer is not good, and then asked about the problem of polymorphism, let me describe, or write an example out, I was 2, so review it here.
Polymorphic, literally means "multiple states". In OOP (object-oriented), many different implementations of interfaces are polymorphic. Polymorphism is a technique that allows you to set a parent object to be equal to one or more of his child objects, and after the assignment, the parent can operate differently depending on the attributes of the child object that is currently assigned to it. Simply put, it is: allows pointers of subtypes to be assigned to a pointer of the parent type.
In C + +, the implementation of polymorphism has the following methods: virtual function, abstract class, rewrite, overwrite, template.
I'm going to implement a virtual function first.
#include <iostream>using namespace Std;class a{public:a () {}~a () {}virtual void fun () {cout<< "This is A" < <endl;}}; Class B:public A{public:b () {}~b () {}void fun () {cout<< "This is B" <<endl;}}; int main () {A *a = new A (); A *b = new B (); A->fun (); B->fun (); if (a = null) Delete a;if (b! = null) delete B;return 0;}
The results of the operation are as follows:
About the polymorphism of C + +