Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure virtual methods in C#?

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 } 
like image 632
David Brewer Avatar asked Feb 09 '11 18:02

David Brewer


People also ask

What is pure virtual function example?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax. For example: class Base {

What are virtual methods in C?

C# virtual method is a method that can be redefined in derived classes. In C#, a virtual method has an implementation in a base class as well as derived the class. It is used when a method's basic functionality is the same but sometimes more functionality is needed in the derived class.

What is virtual and pure virtual function in C?

A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.

How do you create a pure virtual function?

A pure virtual function is declared by assigning 0 in declaration. See the following example.


2 Answers

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.

like image 67
Jon Skeet Avatar answered Sep 23 '22 09:09

Jon Skeet


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

like image 39
David Yaw Avatar answered Sep 24 '22 09:09

David Yaw