Jump to content

Search the Community

Showing results for tags 'virtual functions'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

There are no results to display.

There are no results to display.


Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Website URL


LinkedIn Profile URL


About Me


Cloud Platforms


Cloud Experience


Development Experience


Current Role


Skills


Certifications


Favourite Tools


Interests

Found 1 result

  1. A virtual function in C# may be overridden in subclasses. A virtual function in C# has both a parent and child class implementation. Whenever the core functionality of a method is almost the same, however, the subclass requires additional capability, and it is utilized. The virtual phrase is used to define a virtual function in the superclass, which is then overridden by the override phrase in the subclass. Whenever a function is specified as a virtual function in a superclass, the subclass can choose whether or not to override it. The overriding methodology allows a function to have many forms. As a result, it is a good illustration of polymorphism. Let’s get started with using some of the C# examples to elaborate the working of the “virtual” keyword to create and use the virtual functions in the code. We have to create a C# file in which we can add the code for virtual functions with the “touch” query as below. Example # 01 Let’s get started with the most basic example of using the virtual method in C#. We will be utilizing the System library within our code in the first line by using the “using” keyword as displayed. After this, we created a public base class named “Parent” using the keyword “class”. This base class contains a single function named “Show” of void return type and public access modifier, i.e., virtual function. This virtual function contains a simple WriteLine() function statement of the Console class to print out the simple text sentence on execution at our screen, i.e., “Virtual method in Parent Class”. Another public class named “Child” was created after that. As shown, this class has been derived from the “Parent” class. The method “Show” has been implemented within it that has been overridden from the base class “Parent” using the keyword “override” followed by the “void” return type and started with the “public” access modifier. As this virtual function has been overridden from the base class, it must contain some extra implementation. Thus, we have been using the WriteLine() statement of the Console class here to display a new text string at the shell stating “Overriding Virtual Method in Child Class”. Both the parent and child class functions are of the same name but a little different implementation. All this implementation will work after using the main() method in the code. So, we have created a new independent class named “New” with the keyword “class”. This class contains a static type Main() method with a void return type. The execution started from creating a Parent class object “o1” with the keyword “new” followed by the class name. We have used this new object o1 to call the virtual function “Show” from the Parent class. After this, we have created another object o2 for the Child class using the keyword “new,” starting with the base class name and ending with the Child class name. This shows the actual inheritance has been taking place in the C# code. The child class object “o2” has been used to call the overridden “Show” virtual function of the child class. The function call must not be mixed up while executing the main() method. Also, we should get the show() function of the Parent class executed first and then the show() function of the child class function. The class New has ended here, and our code is completed now. After saving this code within the text editor, we returned to the terminal as our code needs to be compiled with the “mcs” C# compiler. After the successful compilation, we have tried the execution of its “exe” file just created in the home folder with the “mono” runtime of C#. It executed the code and displayed the string of the virtual method from the parent class first and then the string text of the overridden show() method of the child class. Example # 02 Let’s make our code a little advanced and different from the above example. We have been starting it with the System library usage in the first line of code. After that, we utilized a global class names Test containing three more classes and a main() function. The first class, “Rectangle,” is a parent class of the other two classes. We have declared 3 double type public variables “h”, “w”, and “r” in it with 10, 4, and 3, respectively. A double type constant variable “p” has been declared with a built-in “Pi” value of mathematics. A virtual method “Area()” of double return type has been defined here that has been returning the multiplication result of “h” and “w” to the main() function of the Test class, i.e., the area of a rectangle is, height multiply by width. The class “Square” has been derived from the “Rectangle” class. This class is overriding the virtual function “Area” from its base class and returning the multiplication result of “h” and “h,” i.e., the area of a square is one side multiplied by the second side or height into height. Then, we have a derived class “Circle” overriding the virtual function “Area” from the base class Rectangle. This function returns the multiplication result of “p” and “r*r,” i.e., the area of a circle is “pi” multiplied by the square of a radius. Three classes have been defined and implemented in the code fully. Now, it’s the turn of the Main() function of C# to start its execution and gradually run the classes. Within the Main() method of Test class, we have created an object “o1” of parent class Rectangle at the first line using the name of a class with the “new” keyword. Using the Rectangle class, we have created objects o2 and o3 for derived classes Square and Circle, respectively. Three WriteLine() statements from the Console class have been used here to display the area of Rectangle, Square, and Circle, respectively. For calculating the area for three shapes, we have used the respective objects of each shape class. These objects have been calling their respective virtual functions defined in the Rectangle class and overridden within the derived classes, i.e., Square and Circle. It’s time to compile this newly created C# code in the shell with the “mcs” command instruction. After that, use the mono runtime instruction to execute the “exe” file that has been generated as a result of compilation. On execution of this “exe” file for the code, we have got the area of rectangle, square, and circle displayed on our screen as we have used the virtual function in one class and overridden it in the derived classes. Conclusion We have described how a simple C# virtual function can be used within the C# code and overridden within its subclasses. We have demonstrated its working within this article with the help of some examples. The first example illustrated the usage of virtual function within the single inheritance program, while the second example demonstrates that with the help of multiple inheritances. This is how we have shown how a simple method can be used to perform multiple calculations with the single definition of a function in C#. View the full article
  • Forum Statistics

    63.6k
    Total Topics
    61.7k
    Total Posts
×
×
  • Create New...