C + + polymorphic Summary

Source: Internet
Author: User

Overview

Parent *p = new Child ();//Pointer to sub-class object
P->print ();//Compiler calls the parent class method for security
We want the parent class object to call the parent class method, and the subclass object calls the subclass method. Therefore, we introduce the multi-state concept and use virtual function to realize the expectation.

function Rewriting
Define the same function in the subclass as the prototype in the parent class
Function overrides occur only between the parent and child classes

class parent{public : void  print  () {cout<< "Parent:print () do ..."  <<endl; }};class Child: public  parent{public : void  print  () {cout<< "Child:print () do ..."  <<endl; }};    int  Main01 () {run00 ();    /* child;    Parent *p = NULL;    p = &child;    Child.print (); Child.    Parent::p rint ();    */ System ( "pause" ); return  0 ;}  

Overridden functions in the parent class are still inherited to subclasses
By default, functions overridden in the class will hide functions in the parent class
Through the scope resolution:: Can access functions that are hidden in the parent class

/*c/c++ is a statically compiled language at compile time, the compiler automatically determines what kind of object it is pointing to based on the type of pointer * //*1, when compiling this function, the compiler does not know what the pointer p actually points to. 2, the compiler has no reason to error. 3. As a result, the compiler considers the safest approach is to compile the print function to the parent class, because both the parent and subclass must have the same print function. *///Demand//If a parent class object is passed, the print function of the parent class is executed//If a subclass object is passed, the printf function of the subclass is executed#include <iostream>using namespace STD;classparent{ Public:voidPrint () {cout<<"Parent:print () do ..."<<endl; }};classChild: Publicparent{ Public:voidPrint () {cout<<"Child:print () do ..."<<endl; }};voidHowtoprint (parent* p) {p->print ();}voidRun00 () {Child child;    parent* pp = &child; parent& RP = child;//child.print ();    //through pointers    //pp->print ();    //By reference    //rp.print ();Howtoprint (&child);}intMain01 () {run00 ();/* Child child ;    Parent *p = NULL;    p = &child;    Child.print (); Child.    Parent::p rint (); */System"Pause");return 0;}

Multi-State implementation
Call a function defined in the parent class if the parent pointer points to a parent class object
If the parent pointer is to a child class object, the overriding function defined in the child class is called

Support for polymorphism with the virtual keyword in C + +
A function that uses virtual declarations can be overridden to show polymorphic properties

#include "iostream"usingNamespace Std;class Herofighter { Public: Public:Virtual int Ackpower()    {return Ten; }};class Advherofighter: Publicherofighter{ Public:Virtual int Ackpower()    {returnHerofighter::ackpower () *2; }};class enemyfighter{ Public:int Destorypower()    {return  the; }};voidOBJPK (Herofighter *hf, Enemyfighter *enemyf) {if(Hf->ackpower () >enemyf->destorypower ()) {printf ("Heroes defeat the enemy ... Victory \ n "); }Else{printf ("Heroes ... Sacrifice \ n "); }}voidMain () {herofighter HF;    Enemyfighter EF;    OBJPK (&AMP;HF, &AMP;EF);    Advherofighter ADVHF;    OBJPK (&AMP;ADVHF, &AMP;EF); System"Pause");}

three conditions for polymorphic formation
1 to have inheritance
2 to have a function rewrite ... C virtual function
3 to have a parent class pointer (parent class reference) to a subclass object
Polymorphism is the basis of design pattern, polymorphism is the foundation of frame

The theoretical basis of polymorphism
1, C + + is the same as C, is a statically compiled language
2. At compile time, the compiler automatically determines what kind of object the pointer is pointing to, so the compiler thinks the parent pointer is pointing to the parent class object.
3, because the program is not running, it is impossible to know whether the parent pointer to the specific parent class object or child class object
From the point of view of program security, the compiler assumes that the parent pointer only points to the parent class object, so the compiled result is a member function that calls the parent class. This feature is static-linked.

overloaded, rewritten, polymorphic
function overloading
Must be in the same class
A subclass cannot overload a function of the parent class, and a function with the same name will be overwritten
Overloading is the function call that is determined by the type and number of arguments during compilation
function rewriting
Must occur between the parent class and the child class
and the parent class must have exactly the same prototype as the function in the child class.
Can generate polymorphism after using the virtual declaration (if you don't use virtual, it's called redefinition)
Polymorphism is the function call that is determined by the type of the specific object during the run

#include <cstdlib>#include <iostream>using namespace STD;classparent01{ Public: Parent01 () {cout<<"parent01:printf (). Do "<<endl; } Public:Virtual voidFunc () {cout<<"parent01:void func ()"<<endl; }Virtual voidFuncinti) {cout<<"parent:void func (int i)"<<endl; }Virtual voidFuncintIintj) {cout<<"parent:void func (int i, int j)"<<endl; }};classCHILD01: Publicparent01{ Public:voidFuncintIintj) {cout<<"child:void func (int i, int j)"<<" "<<i + j<<endl; }voidFuncintIintJintK) {cout<<"child:void func (int i, int j, int k)"<<" "<<i + j + k<<endl; }};voidRun01 (parent01* p) {P->func (1,2);}intMain () {Parent01 p;    P.func (); P.func (1); P.func (1,2); CHILD01 C;//c.func ();C.parent01::func (); C.func (1,2);    Run01 (&AMP;P);    Run01 (&AMP;C); System"Pause");return 0;}

Virtual Functions
1 constructors cannot be virtual functions. When you create a derived class object, you must call the constructor of the base class one at a time from the root of the class hierarchy and along the inheritance path
2 destructors can be virtual. The virtual destructor is used to direct the delete operator to properly destructor the dynamic object

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + polymorphic Summary

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.