Visual C++ .NET/Class/virtual method
Содержание
Explicit override
#include "stdafx.h"
using namespace System;
ref class MyClass{
public:
void Initialize(bool fromFile)
{
if (fromFile)
{
Read();
}
}
virtual void Read()
{
Console::WriteLine("MyClass::Read");
}
};
ref class MySubClass : MyClass
{
};
ref class MySubSubClass : MySubClass{
public:
virtual void MyClassRead() = MyClass::Read
{
Console::WriteLine("MySubSubClass::MyClassRead");
}
virtual void Read() new
{
Console::WriteLine("MySubSubClass::Read");
}
};
int main()
{
MySubSubClass^ scroll = gcnew MySubSubClass();
MySubClass^ item = scroll;
item->Initialize(true);
scroll->Read();
}
Native managed virtual
#include "stdafx.h"
class MyBaseClass
{
public:
MyBaseClass()
{
myvirtual();
}
virtual void myvirtual()
{
System::Console::WriteLine("MyBaseClass::myvirtual");
}
};
class NativeDerived : public MyBaseClass
{
public:
NativeDerived()
{
myvirtual();
}
virtual void myvirtual()
{
System::Console::WriteLine("NativeDerived::myvirtual");
}
};
ref class ManagedBase
{
public:
ManagedBase()
{
myvirtual();
}
virtual void myvirtual()
{
System::Console::WriteLine("ManagedBase::myvirtual");
}
};
ref class ManagedDerived : ManagedBase
{
public:
ManagedDerived()
{
myvirtual();
}
virtual void myvirtual() override
{
System::Console::WriteLine("ManagedDerived::myvirtual");
}
};
int main()
{
NativeDerived nd;
ManagedDerived rd;
}
new method
#include "stdafx.h"
using namespace System;
ref class MyClass
{
public:
void Initialize(bool fromFile)
{
if (fromFile)
{
Read();
}
}
virtual void Read()
{
Console::WriteLine("MyClass::Read");
}
};
ref class MySubClass : MyClass
{
};
ref class MySubSubClass : MySubClass
{
public:
virtual void Read() new
{
Console::WriteLine("MySubSubClass::Read");
}
};
int main()
{
MySubSubClass^ scroll = gcnew MySubSubClass();
MySubClass^ item = scroll;
item->Initialize(true);
scroll->Read();
}
override and virtual method
#include "stdafx.h"
using namespace System;
ref class MyClass{
public:
void Initialize(bool fromFile)
{
if (fromFile)
{
Read();
}
}
virtual void Read()
{
Console::WriteLine("MyClass::Read");
}
};
ref class MySubClass : MyClass
{
};
ref class MySubSubClass : MySubClass
{
public:
virtual void Read() override
{
Console::WriteLine("MySubSubClass::Read");
}
};
int main()
{
MySubSubClass^ scroll = gcnew MySubSubClass();
MySubClass^ item = scroll;
item->Initialize(true);
scroll->Read();
}
Virtual methods
#include "stdafx.h"
using namespace System;
ref class Animal{
public:
virtual void Speak (){
Console::WriteLine("Animal");
}
};
ref class Dog : public Animal{
public:
virtual void Speak() override{
Console::WriteLine("Dog");
}
};
ref class Puppy : public Dog{
public:
virtual void Yip() = Dog::Speak
{
Console::WriteLine("Puppy");
}
};
ref class Cat : public Animal{
public:
virtual void Speak() new
{
Console::WriteLine("Cat");
}
};
ref class Tiger : public Cat{
public:
virtual void Growl() = Animal::Speak, Cat::Speak
{
Console::WriteLine("Tiger");
}
};
void main()
{
array<Animal^>^ animals = gcnew array<Animal^> {
gcnew Animal(),
gcnew Dog(),
gcnew Puppy(),
gcnew Cat(),
gcnew Tiger()
};
for each ( Animal ^a in animals){
a->Speak();
}
Console::WriteLine();
Animal^ cat1 = gcnew Cat();
Cat^ cat2 = gcnew Cat();
Cat^ tiger = gcnew Tiger();
cat1->Speak();
cat2->Speak();
tiger->Speak();
}