The difference between C + + virtual function and pure virtual function (deep analysis) _c language

Source: Internet
Author: User
Tags inheritance lowercase
In the object-oriented C + + language, virtual functions (virtual function) are a very important concept. Because it fully embodies the object-oriented concept of inheritance and polymorphism of these two characteristics, in the C + + language application is very wide. For example, in Microsoft's MFC class Library, you will find that many functions have virtual keywords, that is to say, they are virtual functions. No wonder some people even call virtual functions the essence of C + + language.
So, what is a virtual function, let's take a look at Microsoft's explanation first:
A virtual function is a member function that you want to overload in a class, and when you point to an inherited class object with a base class pointer or reference, you call a virtual function that actually invokes the version of the inherited class. This definition is not very clear. An example is given in MSDN, but its examples do not give a good indication of the problem. We write this example ourselves:
Copy Code code as follows:

#include "stdio.h"
#include "conio.h"
Class Parent
{
Public
Char data[20];
void Function1 ();
virtual void Function2 (); This declares that Function2 is a virtual function
}parent;
void Parent::function1 ()
{
printf ("This is parent,function1\n");
}
void Parent::function2 ()
{
printf ("This is parent,function2\n");
}
Class Child:public Parent
{
void Function1 ();
void Function2 ();

child;
void Child::function1 ()
{
printf ("This is child,function1\n");
}
void Child::function2 ()
{
printf ("This is child,function2\n");
}
int main (int argc, char* argv[])
{
Parent *p; Define a base class pointer

if (_getch () = = ' C ')//If you enter a lowercase letter C
p=&child; Point to an inherited class object
Else
p=&parent; Otherwise point to the base class object

P->function1 (); The entry address for Parent::function1 () is given directly at compile time.
P->function2 (); Note here, which of the Function2 is executed?

return 0;
}

Compile and run in any version of Visual C + + or Borland C + +, and enter a lowercase letter C to get the following result:
This is Parent,function1
This is Child,function2
Why is there a result of the first line? Because we call the function Fuction1 () with a pointer to a parent class, although the pointer actually refers to the object of the child class, the compiler is not aware of the fact (until the runtime, the program can determine the object to which the pointer is pointing according to the user's input). It can only be understood and compiled according to the function that invokes the parent class, so we see the result of the first row.
  
So what's the result of the second line? We notice that the Function2 () function is decorated in the base class by the virtual keyword, which means that it is a virtual function. The most critical feature of a virtual function is a "dynamic link", which can determine the object that the pointer points to at run time and automatically call the corresponding function. If we enter a non-C character at any time when we run the above program, the result is as follows:
This is Parent,function1
This is Parent,function2
  
Notice the second line and the results change. Only one Function2 () function is called in the program, but it can automatically decide whether to call the Function2 in the base class or the Function2 in the inheriting class according to the user's input, which is the function of the virtual function. We know that in MFC, many classes are required you to inherit, their member functions are many to overload, such as writing MFC applications most commonly used CView::OnDraw (cdc*) functions, you must overload the use. It is defined as a virtual function (in fact, in MFC, OnDraw is not only a virtual function, or pure virtual function), you can ensure that the time is called by the user to write the OnDraw. The important use of virtual functions is evident here.
-----------------------------------------------------------
And look at the following
The size of a derived class the concept of virtual and pure virtual functions in C + + and the reasons for their difference and distinction
First: emphasize a concept
To define a function as a virtual function, not to represent a function that is not implemented, to define it as a virtual function is to allow the use of a pointer to a base class to call this function of a subclass
To define a function as a pure virtual function is to represent a function that is not implemented, to define it to implement an interface, to play a canonical role , a programmer who inherits this class must implement this function.
Impact on Inheritance:
Ordinary classes (without virtual functions, pure virtual functions) can be inherited, and work pretty well
The question has the following questions:
is pure virtual function to implement interface? What is the meaning of interface existence?
I really don't understand, why do I have to define it beforehand? The future of things this difficult, just wait for one day my class needs to use a function, in addition to a function can not?
About instantiating a class:
Classes with pure virtual functions are not possible to generate class objects, if they do not have pure virtual functions. Like what:
Copy Code code as follows:

Class CA
{
Public
virtual void fun () = 0; Description Fun function is pure virtual function
virtual void fun1 ();
};
Class CB
{
Public
virtual void fun ();
virtual void fun1 ();
};
Implementation of CA,CB Class
...
void Main ()
{
CA A; Not allowed because there are pure virtual functions in the class CA
CB b; OK, because there is no pure virtual function in class CB

...
}

---------------------------------------------------------------
The use of virtual functions in the middle of polymorphism:
Polymorphism is generally done by pointing to a pointer to a base class.
Dog Mydogwangwang;
Mydogwangwang.born ();
Must be returned to "dog."
So
Horse Myhorsepipi;
Myhorsepipi.born ();
Must be returned to "horse."
Is it polymorphic?
/////////////////////////////////////////////////
One thing you have to understand is to invoke the subclass at run time with the pointer of the parent class:
For example, there is a function like this:
Copy Code code as follows:

void Animal::fun1 (Animal *maybedog_maybehorse)
{
Maybedog_maybehorse->born ();
}

Parameter maybedog_maybehorse at compile time do not know to pass in the dog class or horse class, so set it as animal class, specific to the runtime decided to use that function.
This means that the parent-class pointer uses a virtual function to determine who the runtime is and who is pointing to.
Copy Code code as follows:

Using Virtual functions
#include <iostream.h>
Class Animal
{
Public
Animal ();
~animal ();
void Fun1 (animal *maybedog_maybehorse);
virtual void born ();
};
void Animal::fun1 (Animal *maybedog_maybehorse)
{
Maybedog_maybehorse->born ();
}
Animal::animal ()
{
}
Animal::~animal ()
{
}
void Animal::born ()
{
cout<< "Animal";
}
Class Dog:public Animal
{
Public
Dog ();
~dog ();
virtual void born ();
};
Dog::d og ()
{
}
Dog::~dog ()
{
}
void Dog::born ()
{
cout<< "Dog";
}
Class Horse:public Animal
{
Public
Horse ();
~horse ();
virtual void born ();
};
Horse::horse ()
{
}
Horse::~horse ()
{
}
void Horse::born ()
{
cout<< "Horse";
}
void Main ()
{
Animal A;
Dog B;
Horse C;
A.FUN1 (&AMP;C);
}
Output:horse
Without virtual functions
#include <iostream.h>
Class Animal
{
Public
Animal ();
~animal ();
void Fun1 (animal *maybedog_maybehorse);
void born ();
};
void Animal::fun1 (Animal *maybedog_maybehorse)
{
Maybedog_maybehorse->born ();
}
Animal::animal ()
{
}
Animal::~animal ()
{
}
void Animal::born ()
{
cout<< "Animal";
}
Class Dog:public Animal
{
Public
Dog ();
~dog ();
void born ();
};
Dog::d og ()
{
}
Dog::~dog ()
{
}
void Dog::born ()
{
cout<< "Dog";
}
Class Horse:public Animal
{
Public
Horse ();
~horse ();
void born ();
};
Horse::horse ()
{
}
Horse::~horse ()
{
}
void Horse::born ()
{
cout<< "Horse";
}
void Main ()
{
Animal A;
Dog B;
Horse C;
A.FUN1 (&AMP;C);
}
Output:animal

---------------------------------------------------------------
a class that has pure virtual functions is an abstract class, cannot generate objects, can only be derived. The pure virtual function of the class he derives from is not overwritten, so its derived class is an abstract class.
---------------------------------------------------------------
The definition of pure virtual functions is to make the base class not instantiated,
Because instantiating such an abstract data structure doesn't make sense in itself.
Or it doesn't make sense to give an implementation.
In fact, I personally think that the introduction of pure virtual functions is for two purposes:
1. For the sake of security. Avoid any unknown result that needs to be made clear but because of carelessness. Remind subclasses to do the implementation that should be done.
2. For efficiency, it is not the efficiency of program execution, but the efficiency of coding.
Related Article

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.