The function and difference of virtual function and pure virtual function in C + + _c language

Source: Internet
Author: User
Tags function definition inheritance

Virtual functions are defined in the base class for overload and polymorphism, 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!

virtual function

Introduction Reason: In order to facilitate the use of polymorphic features, we often need to define virtual functions in the base class.

Class Cman 
{public 
: 
virtual void Eat () {...}; 
void Move (); 
Private: 
}; 
Class Cchild:public Cman 
{public 
: 
virtual void Eat () {...}; 
Private: 
}; 
Cman M_man; 
Cchild M_child; 
Cman *p//This is the essence of use, if you do not define the base class pointer to use, not much significance 
p = &m_man; 
P->eat (); Always call the Cman Eat member function and do not invoke the Cchild 
p = &m_child; 
P->eat (); If the subclass implements (Overrides) the method, it always calls the Cchild Eat function 
//Does not invoke the Cman Eat method; If the subclass does not implement the function, then the Cman function of Eat is invoked 

Pure virtual function

Introduction Reason:

1, with "virtual function";
2, 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.

A pure virtual function is a base class that defines only the function body, does not implement the procedure, and defines methods such as: virtual void Eat () = 0; Do not define in CPP; pure virtual function is equivalent to interface, cannot directly instance, need derived class to implement function definition;

Some people may be thinking, what is the use of the definition of Ah, I think it is useful, such as you want to describe the attributes of things to others, and do not want to implement, you can be defined as pure virtual functions. Say more thoroughly. Like building buildings, you are the boss, you give the construction company to describe the characteristics of your building, how many floors, the roof to have a garden or something, the construction company can follow your method to achieve, if you do not make it clear, the construction company may not understand you need the characteristics of the building. With the pure demand function can be a good division of work

The difference between virtual function and pure virtual function

Opinion one:

In a class declared as a virtual function, this function is implemented, even if the null implementation, it is to enable the function in its subclasses can be overloaded, so that the compiler can use late binding to achieve polymorphism

A pure virtual function is just an interface, it is a declaration of a function, it should be left to the subclass to implement.

Class a{
protected:
void foo ()//Common class function
virtual void foo1 ()//virtual function virtual
void foo2 () = 0;//pure virtual function
}

Point two:

Virtual functions can also be not overloaded within subclasses, but pure virtual must be implemented in subclasses, as is the case with Java interfaces. It's a good habit to add a lot of functions to virtual, although sacrificing some performance, but adding object-oriented polymorphism, because it's hard to predict that the function inside the parent class does not modify its implementation in subclasses.

Point three:

The class of the virtual function is used for "implement inheritance", and inheriting the interface also inherits the implementation of the parent class. Of course, we can also complete their own implementation. Classes for pure virtual functions are used for "interface inheritance", primarily for communication protocols. The focus is on the uniformity of the interface, which is accomplished by subclasses. In general, there are only pure virtual functions in the interface class.

Point of view Four:

The

class with pure virtual functions is called the virtual base class, which cannot be used without directly generating the object, and only if it is inherited and its virtual function is overridden. Such a class is also called an abstract class.
virtual functions are to inherit interfaces and default behavior
Pure virtual functions simply inherit the interface, and the behavior must be redefined

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.