C++总结6——继承与多态的笔试题
2017-07-17 02:01
295 查看
1—————————————————
![](https://img-blog.csdn.net/20170717020247933?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
p->Show();函数有默认的参数,在调用过程中没有传递参数。编译期进行形参压栈,已经将基类的Show()函数的10压进栈了,调用派生类中的Show时,不再对其进行初始化。若要对虚函数中的值进行赋值,不要在基类虚函数的参数列表中赋默认值。
2.———————————————————————
![](https://img-blog.csdn.net/20170717153440373?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
在编译时,发现是基类的指针指向派生类的对象,并且该基类中有虚函数,故采用动态绑定。在运行时,Derive调用自己的私有Show()函数。
3.—————————————————————-
![](https://img-blog.csdn.net/20170717154206546?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
程序崩溃!
在Base类的构造函数中调用clear()函数,将刚构造的对象b清空了。p调用show(),非法访问内存,程序崩溃!
看下面一段代码段,结果什么呢?
![](https://img-blog.csdn.net/20170717155128679?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
对象d中,基类Base的内存都被清空了,但是在Derive的构造函数中,又将虚表的地址写在虚表指针的内存里,故可以访问Show()函数。
4.————————————————————————-
![](https://img-blog.csdn.net/20170717155843899?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
在构造函数里调用虚函数,都是静态绑定。Base类构造函数调用Base::Show();Derive类构造函数调用Derive::Show()
5.———————————————————————-
![](https://img-blog.csdn.net/20170717160213741?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuZHlfa2VlcGluZw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Base类的Show函数不是虚函数,Derive类的Show函数是虚函数。p->Show();采用静态绑定,在编译时已经确定调用的是Base类的Show函数。
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; } virtual ~Base() { cout<<"~Base()"<<endl; } virtual void Show(int i= 10)//虚函数 { cout<<"Base::Show(), i="<<i<<endl; } private: int _ma; }; class Derive : public Base { public: Derive(int data1, int data2):Base(data1),_mb(data2) { cout<<"Derive()"<<endl; } ~Derive() { cout<<"~Derive()"<<endl; }; virtual void Show(int i= 20)//虚函数 { cout<<"Derive::Show(), i="<<i<<endl; } private: int _mb; }; int mai 4000 n() { Base *p = new Derive(10,10); p->Show(); delete p; return 0; }
p->Show();函数有默认的参数,在调用过程中没有传递参数。编译期进行形参压栈,已经将基类的Show()函数的10压进栈了,调用派生类中的Show时,不再对其进行初始化。若要对虚函数中的值进行赋值,不要在基类虚函数的参数列表中赋默认值。
2.———————————————————————
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; } virtual ~Base() { cout<<"~Base()"<<endl; } virtual void Show() { cout<<"Base::Show()"<<endl; } void Show(int i) { cout<<"Base:Show(int)"<<endl; } private: int _ma; }; class Derive : public Base { public: Derive(int data1, int data2):Base(data1),_mb(data2) { cout<<"Derive()"<<endl; } ~Derive() { cout<<"~Derive()"<<endl; } private: virtual void Show() { cout<<"Derive::Show()"<<endl; } private: int _mb; }; int main() { Base *p = new Derive(10,20); p->Show(); delete p; return 0; }
在编译时,发现是基类的指针指向派生类的对象,并且该基类中有虚函数,故采用动态绑定。在运行时,Derive调用自己的私有Show()函数。
3.—————————————————————-
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; Clear(); } void Clear() { memset(this, 0 ,sizeof(*this)); } virtual ~Base() { cout<<"~Base()"<<endl; } virtual void Show() { cout<<"Base::Show()"<<endl; } pr` vate: int _ma; }; int main() { Base b(10); Base *p = &b; p->Show(); delete p; return 0; }
程序崩溃!
在Base类的构造函数中调用clear()函数,将刚构造的对象b清空了。p调用show(),非法访问内存,程序崩溃!
看下面一段代码段,结果什么呢?
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; Clear(); } void Clear() { memset(this, 0 ,sizeof(*this)); } virtual ~Base() { cout<<"~Base()"<<endl; } virtual void Show() { cout<<"Base::Show()"<<endl; } private: int _ma; }; class Derive : public Base { public: Derive(int data1, int data2):Base(data1),_mb(data2) { cout<<"Derive()"<<endl; } ~Derive() { cout<<"~Derive()"<<endl; } virtual void Show() { cout<<"Derive::Show()"<<endl; } private: int _mb; }; int main() { Base *p = new Derive(10,10); p->Show(); delete p; return 0; }
对象d中,基类Base的内存都被清空了,但是在Derive的构造函数中,又将虚表的地址写在虚表指针的内存里,故可以访问Show()函数。
4.————————————————————————-
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; Show(); } virtual ~Base() { cout<<"~Base()"<<endl; } virtual void Show() { cout<<"Base::Show()"<<endl; } private: int _ma; }; class Derive : public Base { public: Derive(int data1, int data2):Base(data1),_mb(data2) { cout<<"Derive()"<<endl; Show(); } ~Derive() { cout<<"~Derive()"<<endl; } virtual void Show() { cout<<"Derive::Show()"<<endl; } private: int _mb; }; int main() { Derive d(10,10); return 0; }
在构造函数里调用虚函数,都是静态绑定。Base类构造函数调用Base::Show();Derive类构造函数调用Derive::Show()
5.———————————————————————-
#include <iostream> using namespace std; class Base { public: Base(int data):_ma(data) { cout<<"Base()"<<endl; } virtual ~Base() { cout<<"~Base()"<<endl; } void Show() { cout<<"Base::Show()"<<endl; } private: int _ma; }; class Derive : public Base { public: Derive(int data1, int data2):Base(data1),_mb(data2) { cout<<"Derive()"<<endl; } ~Derive() { cout<<"~Derive()"<<endl; } virtual void Show() { cout<<"Derive::Show()"<<endl; } private: int _mb; }; int main() { Base *p = new Derive(10,10); p->Show(); delete p; return 0; }
Base类的Show函数不是虚函数,Derive类的Show函数是虚函数。p->Show();采用静态绑定,在编译时已经确定调用的是Base类的Show函数。
相关文章推荐
- c++面向对象三大特征封装、继承和多态知识总结
- C++面向对象的抽象 封装继承 多态总结
- C++继承、多态总结
- C++中继承与多态再总结
- C++总结5——继承与多态
- C++封装继承多态总结
- C++ 虚函数、纯虚函数、继承、虚表、多态原理相关知识点总结
- C++封装继承多态总结
- C++封装继承多态总结
- C++封装继承多态总结(转)
- C++封装继承多态总结
- C++封装继承多态总结
- C++封装继承多态总结
- C++封装继承多态总结
- C++构造、析构、继承、多态--一道笔试题都考到了
- C++总结笔记(一)抽象、多态、继承
- c++继承与多态概念的简单总结
- C++封装继承多态总结
- C++封装继承多态总结
- c++面向对象三大特征封装、继承和多态知识总结