#include "stdafx.h"
#include <iostream>
using namespace System;
using namespace std;
class a
{
public:
a(){
std::cout<<"this is a;";
}
virtual void fun();
};
void a::fun()
{
std::cout<<"this is a::fun;";
}
class b:public a
{
public:
b(){
std::cout<<"this is b.";
}
void fun()
{
std::cout<<"this is b:fun"<<std::endl;
}
};
void abtext(a x)
{
x.fun();
}
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
a* x1=new b();
abtext(*x1);
return 0;
}
父类指针指向子类,构造函数调用顺序,首先调用父类构造函数,再调用子类构造函数,因此本程序先输出this is a;再输出this is b.。完成初始化。调用abtext(*x1);时,由于形参是类a,故应该调用父类的fun()函数,输出this is a::fun;
转载于:https://blog.51cto.com/2198407/1198340