阅读背景:

分享一道类继承的笔试题目

来源:互联网 
 
 
#include<iostream>
using namespace std;
class A
{
public:
	virtual void fun1()
	{
		cout<<"A fun1"<<endl;
	}
	void fun2()
	{
		cout<<"A fun2"<<endl;
	}
};
class B :public A
{
public:
	void fun1()
	{
		cout<<"B fun1"<<endl;
	}
	void fun2()
	{
		cout<<"B fun2"<<endl;
	}
};
int main()
{
	B *obB=new B;
	A *obA=(A*)obB;
	obA->fun1();
	obA->fun2();
	cout<<endl;
	obA = (B*)obA;
	obA->fun1();
	obA->fun2();cout<<endl;
	A *a1=new A;
	B *b1=(B*)a1;
	a1->fun1();
	a1->fun2();cout<<endl;
	a1=(A*)a1;
	a1->fun1();
	a1->fun2();
	cout<<endl;
	B b2;
	A a2=b2;//对象类型进行了转换
	a2.fun1();
	a2.fun2();cout<<endl;
	A a3;
//	B b3=(B)a3; 不可以转换
	return 0;

}#include<iostream>
using namespace std;
clas



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: