Related usage of virtual functions in c

  • 2020-05-09 19:05:07
  • OfStack

If an instance method is declared with the virtual keyword, the method is a virtual method.

The biggest difference between virtual methods and non-virtual methods is that the implementation of virtual methods can be replaced by derived classes through method overrides (more on that later)
Features of virtual methods:
No static,abstract, or override modifiers are allowed before virtual methods
Virtual methods cannot be private and therefore cannot use the private modifier
Implementation of virtual method:
We know that a 1 function is statically compiled into an executable at compile time, and its relative address does not change during the program's run.
However, virtual functions are not statically compiled during compilation, and their relative addresses are uncertain. It will dynamically determine the function to be called based on the object instance at runtime.
The declared class is called the declaration class, and the executed class is called the instance class.
A a =new B(); Where A is the declaration class and B is the instance class.
1. When a function of an object is called, the system will directly check the class defined by the object declaration, namely the declaration class, to see whether the function called is a virtual function;
2. If it is not a virtual function, it simply executes the function. If it is a virtual function, it will not execute the function immediately, but will start to check the instance class of the object.
3. In the instance class, he checks whether the definition of the instance class has methods to implement the virtual function or to re-implement the virtual function (via the override keyword).
If it does, it stops looking and immediately executes the methods of the virtual functions implemented in the instance class. And if it doesn't, the system keeps looking up for the parent of the instance class,
Repeat the check in the instance class until you find the first parent class that overloads the virtual function, and then execute the overloaded function in the parent class.
Case 1:


class A
    {
        publicvirtualvoid Sum()
        {
            Console.WriteLine("I am A Class,I am virtual sum().");
        }
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             A a=new A();   //  define 1 a a this A The object of the class . this A is a Declaration class, instantiation a object ,A is a An instance of the class    
             a.Sum();
             Console.Read();
        }
    }

  performs a.Sum:
1. Check the declaration class A 2. Check that sum is a virtual method 3. Go to the instance class A, which is the problem itself
5. Output results I am A Class,I am virtual sum()
Example 2:


class A
    {
        publicvirtualvoid Sum()
        {
            Console.WriteLine("I am A Class,I am virtual sum().");
        }
    }
    class B : A    
    {
        publicoverridevoid Sum() //  Virtual functions are re-implemented    
        {
            Console.WriteLine("I am B Class,I am override sum().");
        }  
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             A a=new B();  //  define 1 a a this A The object of the class . this A is a Declaration class, instantiation a object ,B is a An instance of the class               
             a.Sum();
             Console.Read();
        }
    }

Perform a. Sum:
1. Check the declaration class A 2. Check that it is a virtual method 3. Go to the instance class B, which has a rewritten method 4. Execute the method in the instance class B 5. Output I am B Class,I override override sum().
Example 3:

class A
    {
        publicvirtualvoid Sum()
        {
            Console.WriteLine("I am A Class,I am virtual sum().");
        }
    }
    class B : A    
    {
        publicoverridevoid Sum() //  Virtual functions are re-implemented    
        {
            Console.WriteLine("I am B Class,I am override sum().");
        }  
    }
    class C : B
    {
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             A a=new C();//  define 1 a a this A The object of the class . this A is a Declaration class, instantiation a object ,C is a An instance of the class               
             a.Sum();
             Console.Read();
        }
    }

Perform a. Sum:
1. Check the declaration class A 2. Check that it is a virtual method 3. Go to the instance class C, no override method 4. Go to the parent class B of class C, there is an override method
6. Output results I am B Class,I am override sum().  
Example 4:


class A
    {
        publicvirtualvoid Sum()
        {
            Console.WriteLine("I am A Class,I am virtual sum().");
        }
    }
    class B : A    
    {
        publicnewvoid Sum() // Override the function of the same name in the parent class, rather than re-implement it   
        {
            Console.WriteLine("I am B Class,I am new sum().");
        }  
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             A a=new B();
             a.Sum();
             Console.Read();
        }
    }

Perform a. Sum:
1. Check the declaration class A 2. Check that it is a virtual method 3. Go to the instance class B, no overrides (note that B implements Sum(), but does not use the override keyword, so it is not considered overwrites) 4. Go to the parent class A of B, 6. Output results I am A Class,I am virtual sum().  
What if, in example 4, the class B is declared?

class A
    {
        publicvirtualvoid Sum()
        {
            Console.WriteLine("I am A Class,I am virtual sum().");
        }
    }
    class B : A    
    {
        publicnewvoid Sum() // Override the function of the same name in the parent class, rather than re-implement it   
        {
            Console.WriteLine("I am B Class,I am new sum().");
        }  
    }
    class Program
    {
        staticvoid Main(string[] args)
        {
             B b=new B();
             b.Sum();
             Console.Read();
        }
    }

Execute Sum() in the B class, and output I am B Class,I am new sum().
Can you override virtual functions in a base class using abstract functions?
The answer is yes.


class A
    {
        publicvirtualvoid PrintFriends()
        {
            Console.WriteLine("A.PrintFriends()");   
        }  
    }
    abstractclass B : A    
    {
        public abstract override void PrintFriends();   // use override  Modifier to indicate that the abstract overrides the implementation of the function in the base class 
    }
    abstract class C : A
    {
        public abstract new void PrintFriends();        // use  new  The modifier explicitly declares that the implementation of the function in the base class is hidden 
    }

Can sealed classes have virtual functions?
Yes, virtual functions in the base class convert implicit virtual functions to non-virtual functions, but the sealed class itself cannot add new virtual functions


class A
    {
        publicvirtualvoid Fun()
        {
            Console.WriteLine("I am A.");
        }
    }
    sealedclass Program:A
    {
        public override void Fun()
        {
            Console.WriteLine("I am B.");
        }
        staticvoid Main(string[] args)
        {
            Program p =new Program();
            p.Fun();
            Console.Read();
        }
    }


Related articles: