Comparison of virtual functions, pure virtual functions and abstract classes in C + + and C # __jquery

Source: Internet
Author: User
Tags instance method
First, C + + in 1, virtual function (virtual)

The first time you introduce a base class for a virtual function, you must specify the virtual keyword in the class declaration. If the definition of a virtual function is placed outside the class, the keyword virtual cannot be specified again. Suppose you have the following class hierarchy:

Class A
{public
:
virtual void foo () {cout << ' A::foo () is called ' << Endl;} Define a virtual function
};

Class B:public A
{public
:
//Memo: As long as virtual is declared in the base class, the default is a virtual function, even if you do not use the virtual keyword,
if there is a subclass derived from B, The corresponding member function is also virtual function virtual
void foo () {cout << "B::foo () is called" << Endl;}
;
Then, when used, we can:
A * a = new B ();
A->foo ()//Here, A is a pointer to a, but the called function (foo) is B!

This example is a typical application of virtual functions. The so-called virtual function, virtual on the "deferred" or "dynamic", the invocation of a class function is not determined at compile time, but is determined at run time. Because it is not possible to write code that is called a function of a base class or a derived class, it is called a "virtual" function. 2. Pure virtual function

It is a special kind of virtual function, its general form is as follows:

Class   < class name >  
          {  
                  virtual   < type >< function name > (< parameter table >) =0;  
                  ...  
          

In many cases, a virtual function cannot be given a meaningful implementation in a base class, and it is described as a pure virtual function, and its implementation is left to the derived class of the base class.   This is the function of pure virtual functions. 3 abstract class

(1) Classes with pure virtual functions are called abstract classes.

(2) Abstract class is a special class, it is for the purpose of abstraction and design, it is in the upper layer of inheritance hierarchy.

(3) An abstract class is an object that cannot be instantiated, and in practice, to emphasize that a class is an abstract class, the constructor of the class can be described as a protected access control permission.

(4) The main function of an abstract class is to have the organization concerned in an inheritance hierarchy, by which it provides them with a common root, and the dependent subclass is derived from this root. Second, C # 1. Virtual function

(1) If an instance method is declared with the virtual keyword, then this method is a virtual function.

(2) Static,abstract is not allowed before virtual methods, or override modifiers

(3) A virtual method cannot be private, so the private modifier cannot be used

    Class A
    {public
        virtual void sum ()//virtual function
        {
            Console.WriteLine ("I am aclass,i am Virtual sum ().");
        }
    }
    Class b:a
    {public
        override void Sum ()//re-implemented virtual function (override)
        {
            Console.WriteLine ("I am bclass,i am override S Um (). ");
        }
 
    }
    Class program
    {
        static void Main (string[] args)
        {
            A A = new B ();  Defines an object of a of this class A. This is a declaration class of a, instantiating a object, B is an instance class             
            A.sum ();
            Console.read ();
        }
    

2 abstract functions (equivalent to pure virtual functions in C + +)

(1) Abstract functions do not execute code and must be overridden in a derived class that is not abstract

(2) Abstract functions are virtual, but no need to provide virtual keyword, or it will error

Abstract class building 
{public 
   abstract decimal calculateheatingcost ();//abstract Method 
}

3 abstract class

(1) class with keyword Abstract declaration is an abstract class

Abstract class Myabs
     {public
        void Nonabmethod ()
         {
            Console.WriteLine (' Non-abstract method ');
        }
    


(2) Abstract classes cannot be instantiated directly (also in Java)

(3) An abstract class can contain an abstract member, but it may not work in a non-abstract class, that is, if the class contains an abstract function, the class is also abstract and must be declared as abstract

(4) keyword abstract and sealed can not be used together in C #, because a sealed class can not be compared to the abstract three or three concept

virtual function

Pure virtual function

Abstract class

C++

Statement by Virtual

Virtual < type >< function name > (< parameter table >) = 0;

As long as a class containing pure virtual functions

C#

Statement by Virtual

Abstract < type >< function name > (< parameter table >)

Class declared with abstract

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.