注册 登录
编程论坛 VC.NET论坛

[原创]声明和使用虚函数

zhangzujin 发布于 2006-01-11 21:31, 1101 次点击

#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;
}

1 回复
#2
zhangzujin2006-01-11 21:36
回复
只有本站会员才能查看附件,请 登录

1