欢迎来到天天文库
浏览记录
ID:52305530
大小:206.06 KB
页数:11页
时间:2020-04-04
《多态性用同样接口访问功能不同的函数静态联编和动态联.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、第五章多态性多态性:用同样接口访问功能不同的函数。静态联编和动态联编动态联编:程序运行时根据指针实际的指向来确定运行函数指向基类的指针+虚函数(在基类中定义)根据赋值兼容规则,可使用派生类的对象代替基类的对象。利用基类类型的指针指向派生类,可以通过这个指针来使用派生类的成名函数。如果这个成员函数是普通的成员函数,通过基类类型的指针访问的是基类的同名函数;如果成员函数是虚函数,则可使用基类类型的指针访问该指针正在指向的派生类的同名函数虚函数的访问权限不影响对它进行动态联编在派生类中定义的同名函数,参数个
2、数和相应类型以及返回类型与基类完全一样,则有无virtual都是虚函数构造函数不能是虚函数,析构函数可以是虚函数虚函数不能是友元函数,不能是静态成员函数classbase1{public:virtualvoidfun(inti){cout<<"base1"<f
3、un(10);}classbase1{public:virtualvoidfun(inti){cout<<"base1"<4、1->fun(10);p2=&obj;p2->fun();}classbase1{public:virtualvoidfun(inti){cout<<"base1"<5、rivedobj;p1=&obj;p1->fun(10);p2=&obj;p2->fun();}第五章多态性在构造函数和析构函数中调用虚函数:采用静态联编,即调用自己或基类的page例5_4纯虚函数与抽象类class类名{virtual类型函数名(参数列表)=0;}纯虚函数不能在构造函数中调用classbase{public:intx;base(inta){x=a;cout<<“base():”<6、irtualvoidfun1(){cout<<“fun1inbase:”<7、rived:”<fun1();pBase->fun2();pDerived->fun1();pDerived->fun2();}base():5derived():25fun1inderived:25fun2inbase:5fun18、inderived:25fun2inderived:25~derived():25~base():5勘误表P100“并且假设有指针pa、pb、pc并说明如下(第二行)B*pb;P102例题第六行pa->fb();例题分析对于类定义classA{public:virtualvoidfunc1(){}voidfunc2(){}};classB:publicA{public:voidfunc1(){cout<<“classBfunc1”<
4、1->fun(10);p2=&obj;p2->fun();}classbase1{public:virtualvoidfun(inti){cout<<"base1"<5、rivedobj;p1=&obj;p1->fun(10);p2=&obj;p2->fun();}第五章多态性在构造函数和析构函数中调用虚函数:采用静态联编,即调用自己或基类的page例5_4纯虚函数与抽象类class类名{virtual类型函数名(参数列表)=0;}纯虚函数不能在构造函数中调用classbase{public:intx;base(inta){x=a;cout<<“base():”<6、irtualvoidfun1(){cout<<“fun1inbase:”<7、rived:”<fun1();pBase->fun2();pDerived->fun1();pDerived->fun2();}base():5derived():25fun1inderived:25fun2inbase:5fun18、inderived:25fun2inderived:25~derived():25~base():5勘误表P100“并且假设有指针pa、pb、pc并说明如下(第二行)B*pb;P102例题第六行pa->fb();例题分析对于类定义classA{public:virtualvoidfunc1(){}voidfunc2(){}};classB:publicA{public:voidfunc1(){cout<<“classBfunc1”<
5、rivedobj;p1=&obj;p1->fun(10);p2=&obj;p2->fun();}第五章多态性在构造函数和析构函数中调用虚函数:采用静态联编,即调用自己或基类的page例5_4纯虚函数与抽象类class类名{virtual类型函数名(参数列表)=0;}纯虚函数不能在构造函数中调用classbase{public:intx;base(inta){x=a;cout<<“base():”<6、irtualvoidfun1(){cout<<“fun1inbase:”<7、rived:”<fun1();pBase->fun2();pDerived->fun1();pDerived->fun2();}base():5derived():25fun1inderived:25fun2inbase:5fun18、inderived:25fun2inderived:25~derived():25~base():5勘误表P100“并且假设有指针pa、pb、pc并说明如下(第二行)B*pb;P102例题第六行pa->fb();例题分析对于类定义classA{public:virtualvoidfunc1(){}voidfunc2(){}};classB:publicA{public:voidfunc1(){cout<<“classBfunc1”<
6、irtualvoidfun1(){cout<<“fun1inbase:”<7、rived:”<fun1();pBase->fun2();pDerived->fun1();pDerived->fun2();}base():5derived():25fun1inderived:25fun2inbase:5fun18、inderived:25fun2inderived:25~derived():25~base():5勘误表P100“并且假设有指针pa、pb、pc并说明如下(第二行)B*pb;P102例题第六行pa->fb();例题分析对于类定义classA{public:virtualvoidfunc1(){}voidfunc2(){}};classB:publicA{public:voidfunc1(){cout<<“classBfunc1”<
7、rived:”<fun1();pBase->fun2();pDerived->fun1();pDerived->fun2();}base():5derived():25fun1inderived:25fun2inbase:5fun1
8、inderived:25fun2inderived:25~derived():25~base():5勘误表P100“并且假设有指针pa、pb、pc并说明如下(第二行)B*pb;P102例题第六行pa->fb();例题分析对于类定义classA{public:virtualvoidfunc1(){}voidfunc2(){}};classB:publicA{public:voidfunc1(){cout<<“classBfunc1”<
此文档下载收益归作者所有