Visual C++ .NET/Class/abstract class
Abstract classes
#include "stdafx.h"
ref class MyClass abstract{
int ID;
public:
virtual void ReadFromFile() abstract;
virtual void UpdateID(int id){
ID = id;
}
};
ref class MySubClass : MyClass{
public:
virtual void ReadFromFile() override
{
}
};
Abstract sealed class
#include "stdafx.h"
using namespace System;
ref class A abstract sealed
{
static int i = 1;
static int j = 2;
public:
static A() {
Console::WriteLine("A::A()");
}
static void f() {
Console::WriteLine("A::f " + i);
}
static void g() {
Console::WriteLine("A::g " + j);
}
};
int main()
{
A::f();
A::g();
}
Extends abstract class
#include "stdafx.h"
using namespace System;
ref class AbstractBaseClass abstract{
protected:
int AbstractVar;
AbstractBaseClass(int val): AbstractVar(val) {}
public:
virtual void Method1() = 0; // unimplemented method
virtual void Method2() = 0;
void Method3()
{
Console::WriteLine(AbstractVar.ToString());
}
};
ref class MidAbstractBaseClass abstract : public AbstractBaseClass
{
public:
virtual void Method1() override sealed
{
Console::WriteLine((AbstractVar * 3).ToString());
}
protected:
MidAbstractBaseClass(int val) : AbstractBaseClass(val) {}
};
ref class DerivedExClass : public MidAbstractBaseClass
{
public:
DerivedExClass(int val) : MidAbstractBaseClass(val) {}
virtual void Method2() override
{
Console::WriteLine((AbstractVar * 2).ToString());
}
};
void testMethod(AbstractBaseClass ^aec)
{
aec->Method1();
aec->Method2();
aec->Method3();
}
void main()
{
AbstractBaseClass ^Ab1 = gcnew DerivedExClass(5);
Ab1->Method1();
Ab1->Method2();
Ab1->Method3();
AbstractBaseClass ^Ab2 = gcnew DerivedExClass(6);
testMethod(Ab2);
DerivedExClass ^dc = gcnew DerivedExClass(7);
testMethod(dc);
}