Pure virtual methods in C#?
Asked Answered
H

7

51

I've been told to make my class abstract:

public abstract class Airplane_Abstract

And to make a method called move virtual

 public virtual void Move()
        {
            //use the property to ensure that there is a valid position object
            double radians = PlanePosition.Direction * (Math.PI / 180.0);

            // change the x location by the x vector of the speed
            PlanePosition.X_Coordinate += (int)(PlanePosition.Speed * Math.Cos(radians));

            // change the y location by the y vector of the speed
            PlanePosition.Y_Coordinate += (int)(PlanePosition.Speed * Math.Sin(radians));
        }

And that 4 other methods should be "pure virtual methods." What is that exactly?

They all look like this right now:

public virtual void TurnRight()
{
    // turn right relative to the airplane
    if (PlanePosition.Direction >= 0 && PlanePosition.Direction < Position.MAX_COMPASS_DIRECTION)
        PlanePosition.Direction += 1;
    else
        PlanePosition.Direction = Position.MIN_COMPASS_DIRECTION;  //due north
}
Halve answered 9/2, 2011 at 18:35 Comment(1)
When interested, for a discussion on pure virtual and non-pure virtual functions (advantages and disadvantages), I will selfishly redirect to a blog post of me. :)Leelah
S
97

My guess is that whoever told you to write a "pure virtual" method was a C++ programmer rather than a C# programmer... but the equivalent is an abstract method:

public abstract void TurnRight();

That forces concrete subclasses to override TurnRight with a real implementation.

Supat answered 9/2, 2011 at 18:36 Comment(9)
So does that mean every bit of content it has should be taken out? o.o That's what its looking like.Halve
@allthosemiles: For the abstract methods, yes. The point of an abstract method is that it doesn't have a body.Supat
pure virtual is a general name, and not language specificLeelah
@Steven: Hmm... possibly, but I've only ever seen it in the context of C++ before. I suspect anyone talking about them is likely to have a C++ background :)Supat
@Jon Skeet: I'm guilty there. :) Maybe it simply originated from C++.Leelah
@Steven: Yup, I think that's almost certainly the explanation.Supat
@Jon Skeet: Just out of curiosity, I asked the question on Programmers.SE. :)Leelah
@Steven: Thanks - it'll be interesting to see what comes out of it.Supat
@Jon Skeet: Seems you were right. It also seems there is a lack of consistent naming, although I did find a scientific article which tries to generalize some terms. Oh, and probably someone should edit wikipedia. :)Leelah
L
13

"Pure virtual" is C++ terminology. The C# equivalent is an abstract method.

Limb answered 9/2, 2011 at 18:36 Comment(0)
S
11

They probably mean that the methods should be marked abstract.

 public abstract void TurnRight();

You will then need to implement them in the subclasses, as opposed to an empty virtual method, where the subclass would not have to override it.

Sheathing answered 9/2, 2011 at 18:36 Comment(0)
M
8

A pure virtual function is terminology of C++ but in C# if a function which is implemented in Derived class and that derived class is not an Abstract class.

abstract class PureVirtual
{
    public abstract void PureVirtualMethod();
}

class Derivered : PureVirtual
{
    public override void PureVirtualMethod()
    {
        Console.WriteLine("I'm pure virtual function");
    }
}
Mohamedmohammad answered 22/1, 2016 at 8:29 Comment(0)
P
5

Or even you can go interface, thought some little limitation is required:

public interface IControllable
{
    void Move(int step);
    void Backward(int step);
}

public interface ITurnable
{
   void TurnLeft(float angle);
   void TurnRight(float angle);
}

public class Airplane : IControllable, ITurnable
{
   void Move(int step)
   {
       // TODO: Implement code here...
   }
   void Backward(int step)
   {
       // TODO: Implement code here...
   }
   void TurnLeft(float angle)
   {
       // TODO: Implement code here...
   }
   void TurnRight(float angle)
   {
       // TODO: Implement code here...
   }
}

However, you will have to implement all the function declaration of both IControllable and ITurnable have assigned, otherwise an compiler error will occur. If you want optional virutal implementation, you would have to go abstract class for virtual method with interface for pure virtual method.

In fact, there is a difference between interface function and abstract function, that interface only declares function, all interface function have to go all public so there's no fancy class property such as private or protected so it's very fast, while abstract function are actual class method without implementation and forces the implementation in the derived class, so you can put private, protected and access member variables with abstract functions, and most of the time it's slower because the class inheritance relations are analyzed in run time. (aka vtable)

Patrinapatriot answered 16/3, 2015 at 5:50 Comment(0)
E
2

You then don't have an implementation in the Airplane_Abstract Class but are forcing consumers "inheritors" of the Class to implement them.

The Airplane_Abstract Class is unusable until you inherit and implement the abstract functions.

Everard answered 9/2, 2011 at 18:38 Comment(0)
C
-2

http://en.wikipedia.org/wiki/Virtual_function

"In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature."

Google is always your friend.

Christology answered 9/2, 2011 at 18:38 Comment(1)
This is not an answer, as it describes a virtual, not a pure virtual method. A pure virtual method has no implementation and thus must be overridden rather than can be overridden.Counterfoil

© 2022 - 2024 — McMap. All rights reserved.