[原创]声明和使用虚函数
#include "stdafx.h"
#using <mscorlib.dll>
using namespace System;
__gc class parent
{
protected:
int x;
public:
parent(int a) // 带参数的虚函数
{
x=a;
};
virtual void ShowX( )=0; // 纯虚函数
};
__gc class son1:public parent
{
public:
son1(int a):parent(a)
{
};
void ShowX( )
{
Console::Write(S"Method1's show X:");
Console::WriteLine(x);
};
};
__gc class son2:public parent
{
public:
son2(int a):parent(a)
{
};
void ShowX( )
{
Console::Write(S"Method2's show X:");
Console::WriteLine(x);
};
};
int _tmain()
{
son1 *sonA;
son2 *sonB;
sonA=new son1(1);
sonB=new son2(2);
sonA->ShowX( );
sonB->ShowX( );
return 0;
}