When a virtual function exists in both the base class and the subclass, call case Analysis __ function

Source: Internet
Author: User

First, we give the base class animal and the sub-class fish.

==============================================================
//           animal.h
///
begin   : 2012-06-30
//author  : Zwq
//describe: In the case of a non-virtual function, the child class pointer is assigned to the accumulating pointer to verify the final call
//			 base class function or subclass function.
//==============================================================
#ifndef animal_h
#define ANIMAL _h

//===============================================================////				  animal
//				 Animal base class
//
//=============================================================== class
Animal
{public
:
	void Breathe ();		Non-virtual function
	virtual void eat ();//virtual function
};

===============================================================////				       Animal
//				 fish, Integrated in animal base class
//
//=============================================================== class
Fish:public Animal
{public
:
	void Breathe ();		Non-virtual function
	void eat ();			virtual function
};

#endif
#include "StdAfx.h"
#include <iostream>
#include "Animal.h"

using namespace std;

===============================================================////				  Animal
//				 Animal base class
//
//===============================================================

void Animal::breathe ()
{
	cout << "Animal Breathe" << Endl;
}

void Animal::eat ()
{
	cout << "Animal eat" << Endl;
}

===============================================================////				       Animal
//				 fish, Inherited from animal base class
//
//===============================================================

void Fish::breathe ( )
{
	cout << "Fish bubble" << Endl;
}

void Fish::eat ()
{
	cout << "Fish eat" << Endl;
}

two. Base class animal pointer to the subclass fish pointer, virtual function call Analysis
int main (int argc, char* argv[])
{
	examanimalvirtualfunc ();

	return 0;
}

void Examanimalvirtualfunc ()
{
	//Assign the base class pointer directly to the subclass pointer, requires casting, and the C + + compiler does not automatically type conversion
	//Because the animal object is not a fish object
	fish* fh1;
	animal* an1 = new Animal;
	Coercion type conversion
	FH1 = (fish*) an1;
	
	The Eat () function is a virtual function
	//call to the base class eat ()
	fh1->eat ();
	
	Delete An1;
	An1 = NULL;
}

After casting the base class pointer to a subclass pointer, because the EAT () function is a virtual function, the pointer to the fish class fh1 the function called Eat () is actually the function eat () of the base class. The
output is as follows:

analysis, although the fish class pointer fh1 called the function eat (), however, because the fish class pointer fh1 is cast by the base class pointer, the corresponding function is called at run time based on the actual type when the object was created. (Late binding technology)


Two. sub-class fish pointers to the base class animal pointers, virtual function call analysis

int main (int argc, char* argv[])
{
	examanimalvirtualfunc ();

	return 0;
}

void Examanimalvirtualfunc ()
{	
	//Assign the subclass pointer directly to the base class pointer without casting, and the C + + compiler automatically casts the type
	///Because the Fish object is also a animal object
	animal* pAn;
	fish* PFH = new Fish;
	PAn = PFH;
	
	The Eat () function is
	the Eat ()
	pan->eat
		
	() of the virtual function//invocation subclass. Delete pfh;
 	PFH = NULL;
}

After the subclass pointer is converted to a base class pointer, the function eat () Called by the pointer pan of the animal class is actually a function eat () of a subclass, since the Eat () function is a virtual function. The
output results are as follows:


Analysis, although the pointer pan of the animal class calls the function eat (), the corresponding function is invoked at run time based on the actual type when the object was created. (Late binding technology)

four. Conclusion
When a virtual function exists in both the base class and the subclass, the corresponding function is called based on the actual type when the pointer was created.
That is, which class is used when new calls, and which class's virtual function is used.

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.