您的位置:首页 > 编程语言 > C语言/C++

C++总结6——继承与多态的笔试题

2017-07-17 02:01 295 查看
1—————————————————

#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函数。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: