标签:
● 编译时的多态性
编译时的多态性是通过重载来实现的。对于非虚的成员来说,系统在编译时,根据传递的参数、返回的类型等信息决定实现何种操作。
● 运行时的多态性
运行时的多态性就是指直到系统运行时,才根据实际情况决定实现何种操作。C#中,运行时的多态性通过虚成员实现。
● 特点
编译时的多态性为我们提供了运行速度快的特点,而运行时的多态性则带来了高度灵活和抽象的特点。
#include"cstdio" #include"cstdlib" #include"algorithm" #include"iostream" #include"sstream" #include"cstring" #include"string" #include"algorithm" using namespace std; class Father{ public: Father(){ cout<<this<<":"<<"create Father object"<<endl; } virtual ~Father(){ cout<<"out of class Father"<<endl; } void fun(){ cout<<"this is a common fun"<<endl; } virtual void DoSomething(){ cout<<"do something in class Fahter"<<endl; } }; class Son:public Father{ public: Son(){ cout<<this<<":"<<"create Son object"<<endl; } ~Son(){ cout<<"out of the class Son"<<endl; } void DoSomething(){ cout<<"do something in class Son"<<endl; } }; int main() { Son *f=new Son(); f->DoSomething(); f->fun(); delete f; }
运行结果:
0x9cca008:create Father object
0x9cca008:create Son object
do something in class Son
this is a common fun
out of the class Son
out of class Father
标签:
原文地址:http://www.cnblogs.com/mypsq/p/4871257.html