The difference between C + + virtual function and pure virtual function

Source: Internet
Author: User
Tags abstract define function function definition


Virtual functions are defined in the base class for overload and polymorphism purposes, and can be overridden or not written in a base class, even if the definition is null.

Pure virtual functions are not defined in the base class and must be implemented in subclasses, much like interface functions in Java!


Polymorphism is an important feature of C + +, which accesses a function of a derived class through a base class pointer.

A virtual function is a function defined in order to implement this function, and virtual functions can be implemented or not implemented when defined, and classes that define virtual functions can be instantiated.

Pure virtual functions are more to represent the meaning of the interface, pure virtual function definition can not be implemented, need to be implemented in the derived subclass and must be implemented, the class containing pure virtual functions cannot be instantiated.

The virtual destructor and the virtual function appear in pairs to invoke the destructor of the derived class when the derived object is released through the base class pointer.

Class Virtualfun
{
Public
Virtualfun ();
~virtualfun ();
virtual void Hello () {std::cout<< "Hello" <<std::endl;};
};

Class Purevirtualfun
{
Public
Purevirtualfun ();
Virtual ~purevirtualfun ();
virtual void Hello () = 0;
};

Class Apurevirtualfun:public Purevirtualfun
{
Public
Apurevirtualfun ();
Virtual ~apurevirtualfun ();
virtual void Hello () {std::cout<< "Hello" <<std::endl;};
};

int main ()
{
virtualfun* a = new Virtualfun ();
purevirtualfun* B = new Apurevirtualfun ();
}

The difference between C + + virtual function and pure virtual function


What is a virtual function?

The member functions that are decorated by the virtual keyword are virtual functions. The function of virtual function, explained by the professional term is to realize polymorphism (polymorphism), polymorphism is the separation of interface and implementation, the image of the language to explain is to achieve a common approach, but because of individual differences in the use of different strategies.

The virtual function declaration is as follows: Virtual returntype functionname (Parameter);

The virtual function must be implemented, and if not implemented, the compiler will complain, and the error message is:

Error lnk****: unresolved external symbol "Public:virtual void __thiscall

Classname::virtualfunctionname (void) "

Why use pure virtual functions?

In many cases, it is unreasonable for the base class itself to generate objects. For example, animals as a base class can derive from Tigers, peacocks, and other subclasses, but animals themselves produce objects that are clearly irrational. In order to solve this problem, it is convenient to use class polymorphism, introduce the concept of pure virtual function, define function as pure virtual function (method: Virtual ReturnType function () = 0;), the compiler requires that it must be overridden in a derived class to achieve polymorphism. Classes that also contain pure virtual functions are called abstract classes and cannot generate objects.

Under what circumstances do you use pure virtual functions (pure vitrual function)?

1, when you want to abstract a method in the base class, and the base class can only be inherited, not instantiated;

2, this method must be implemented in the derived class (derived class);

If the above two points are met, consider declaring the method as pure virtual function.

Let's take for example, we first define a class of shapes (Cshape), but all shapes require that they show themselves. So we've defined a class as follows:

Class Cshape

{

virtual void Show () {};

};

But there is no cshape this shape, so we don't want the Cshape class to be instantiated, and the first thing we think of is to delete the definition (Implementation) section of the show function as follows:

Class Cshape
{
virtual void show ();
};
When we instantiate a cshape using the following statement:

Cshape CS; This is not allowed, but only the above code can be compiled (but link fails).


How do you avoid a cshape being instantiated and found at compile time?

The answer is: use pure virtual funcion.

We modify the Cshape class again as follows:

Class Cshape
{
Public
virtual void Show () = 0;
};
When the Cshape is instantiated, the following error message is available:
Error C2259: ' Cshape ': cannot instantiate abstract class due to following members:
Warning C4259: ' void __thiscall cshape::show (void) ': pure virtual function is not defined

Let's take a look at the inherited situation, we need a cpoint2d class that inherits from Cshape. He must implement the show () method in the base class (Cshape).
In fact, the original intention is to have each derived from the Cshape class to implement the show () method, but sometimes we may forget in a derived class to implement show (), in order to avoid this situation, pure virtual funcion play a role.
Let's look at the following code:
Class Cpoint2d:public Cshape
{
Public
CPOINT2D ()
{
printf ("Cpoint2d ctor is invoked\n");
};
void Msg ()
{
printf ("cpoint2d.msg () is invoked\n");
};

};

When we instantiate cpoint2d, the following error occurs at compile time (at the compiling):
Error C2259: ' Cshape ': cannot instantiate abstract class due to following members:
Warning C4259: ' void __thiscall cshape::show (void) ': pure virtual function is not defined
As above, we have prevented forgetting to implement the base class method in a derived class.

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.