Polymorphism and virtual function, concept of pure virtual function __ function

Source: Internet
Author: User

First of all, in understanding what is polymorphic before, to understand what is a virtual function, the virtual function for polymorphism has a decisive role, there are virtual functions to form polymorphic. How to create a class with a virtual function, in fact, is the function type preceded by the keyword virtual, with virtual functions, the base class pointers can use either the base class's member functions or the derived class member functions, it has a variety of forms, or a variety of manifestations, this is polymorphism.

Polymorphism is one of the main characteristics of object-oriented. In C + +, the only use of virtual functions is to form polymorphism.

C + + provides polymorphism for the purpose of "omni-directional" access to all derived classes, including directly derived and indirectly derived, and member functions, in particular member functions, through a base-class pointer. If there is no polymorphism, we can only access member variables.
Conditions that constitute polymorphism

That is, the polymorphic existence of three conditions:
There must be an inheritance relationship;
There must be virtual functions with the same name in the inheritance relationship, and they are overriding relationships (not overloaded).
A pointer to a base class exists that invokes the virtual function through the pointer.

Note: A virtual function in a derived class must overwrite (not overload) a virtual function in the base class to access it through the base class pointer.

#include <iostream>
using namespace std;
Class my{public
	:
		virtual void myfun () {  //virtual function
			cout<< "abc" <<endl;
		}
};
Class My1:public my{public
	:
		void Myfun () {
			cout<< "ABC1" <<endl;
		}
};
void Main () {My
	*p=new me ();
	P->myfun ();
	P=new my1 ();
	P->myfun ();
	System ("pause");
}


Also note:
1. If you want to access a function of a derived class through a base class pointer, the function must overwrite the function in the base class, as is the normal function, as is the virtual function.
2. If the cover is a virtual function, then it constitutes a polymorphic state.
The 3.virtual keyword is used only for function declarations, and you do not need to add the virtual keyword if the function is defined outside the class.
4. For convenience, you can declare only functions in the base class as virtual functions, and functions with the same name that have overriding relationships in all derived classes will automatically become virtual functions.

When it comes to virtual functions, you have to speak pure virtual functions:

In C + +, you can declare a member function to be a pure virtual function, in syntax format:
The virtual function returns the type function name (function parameter) = 0;
Pure virtual functions have no function bodies, only function declarations, plus = 0 at the end of a virtual function declaration, indicating that this function is a pure virtual function.
The final =0 does not mean that the function return value is 0, it only plays a formal role, telling the system "This is pure virtual function".
Classes that contain pure virtual member functions are called abstract classes. It is abstract because it cannot be instantiated, that is, the object cannot be created. The reason is obvious, pure virtual function has no function body, not complete function, cannot call, cannot allocate memory space for it, also can say is abstract class is contain pure virtual function class.


Abstract classes are usually used as base classes, allowing derived classes to implement pure virtual functions. A derived class must implement a pure virtual function to be instantiated.

Class my{public
	:
		virtual void show () =0;   Pure virtual function
	private:
	protected:
};
Class My1:public my{public
	:
		Void Show () {
			cout<< "pure virtual function" <<endl;
		}
};
void Main () {
	//my *p=new my (); Error    pure virtual function Default abstract class, object creation is not allowed. 
	my1 *p=new my1 ();
	P->show ();
	System ("pause");
}
A few notes on pure virtual functions:
1 A pure virtual member function can make a class an abstract base class, but in addition to containing pure virtual member functions, the abstract base class can contain other member functions (virtual functions or normal functions) and member variables.


2 only virtual functions in a class can be declared as pure virtual member functions, and neither ordinary member functions nor top-level functions can be declared as pure virtual functions.

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.