function and difference of virtual function, pure virtual function and overloaded function _ function

Source: Internet
Author: User
Tags function definition

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 Java the interface function in.

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 meaning

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, the cchild eat function is always invoked

The Cman Eat method is not invoked, and the Cman Eat function is invoked if the subclass does not implement the function

P->move (); There is no such member function in the subclass, so the call is in the base class

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 ();//Normal 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:

Classes with pure virtual functions are called virtual base classes, which cannot be used without directly generating objects, but only when they are inherited and their virtual functions are overridden. Such a class is also called an abstract class.

Virtual functions are for inheriting interfaces and default behavior

A pure virtual function simply inherits the interface, and the behavior must be redefined


the difference between a virtual function and an overloaded functionOverloaded functions must be different in type and number of parameters, whereas a redefined virtual function requires the type and number of arguments, and the same type of function return;
A virtual function must be a member function of a class, which is not necessarily the case for overloaded functions;
Constructors can be overloaded, but not virtual, destructors can be virtual functions.
Example 1:#include < iostream.h >
Class CBase
{
Public
virtual int func (unsigned char ch) {return--ch;}
}  ;
Class Cderive:public CBase

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.