// EX_EXAM.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class A
{
public:
virtual void foo() { cout<<"A foo"<<endl; }
void pp() { cout<<"A pp"<<endl; }
};
class B: public A
{
public:
void foo() { cout<<"B foo"<<endl; }
void pp() { cout<<"B pp"<<endl; }
void FunctionB() { cout<<"Excute FunctionB!"<<endl; }
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;
A *pa=&b;
pa->foo();
pa->pp();
if (B *pb=dynamic_cast<B*>(pa))
{
cout<<"Convert from B to A successfully"<<endl;
(dynamic_cast<B*>(pa))->FunctionB();
(dynamic_cast<B*>(pa))->foo();
(dynamic_cast<B*>(pa))->pp();
(*pa).foo();
}
else
cout<<"Cannot convert from B to A"<<endl;
return 0;
}
C++ 中【子类转父类】和【父类转子类】的方法
最新推荐文章于 2024-02-06 17:24:17 发布