Polymorphic Two (function overloading--function rewriting--function redefinition) of C + + class

Source: Internet
Author: User

//function Overloading--function rewriting--function redefinition#include <iostream>using namespacestd;/*function Overloading: You must do this in a class (subclasses cannot overload functions in the parent class) subclasses cannot overload a parent class function, and a function with the same name as the parent class overrides the function called by the function call function that is determined by the parameter type, order, number of times during compilation Must occur between the parent class and the subclass and the parent class and the function in the subclass must have exactly the same function signature after using the virtual declaration to produce polymorphism (if not using virtual, which is called redefinition) polymorphism is the function call based on the type of the specific object during the run Overrides are generally accompanied by the occurrence of function redefinition function redefinition must occur between the parent class and the child class and the parent class and subclass have exactly the same function name (but the parameter list return value can be different, only requires the same function name redefinition is not necessarily polymorphic rewriting--overloading is the concept of C + + */classpoint{ Public:    //function Overloading    voidFuc () {cout<<"1 Parent class overloaded functions"<<Endl; }    intFUC (inta) {cout<<"2 Parent class overloaded functions"<<Endl; returnA; }    Virtual voidFUC (intIintj) {cout<<"3 overloading virtual functions with parent classes"<<Endl; }    Virtual voidFUC (intIintJintk) {cout<<"4 overloading virtual functions with parent classes"<<Endl; }};classPointa: Publicpoint{ Public:    //Overriding the parent class function 3---Overriding the parent class function while also redefining the parent class function with the same name, the parent class can no longer be called with the same name function    voidFUC (intIintj) {cout<<"1 Subclasses overriding virtual functions"<<Endl; }    //Redefine parent class functions    intFUC (inta) {cout<<"2 Subclass overloaded Functions"<<Endl; returnA; }};voidProtecta (Point &pin) {pin. Fuc (1);}voidPROTECTB () {Pointa pa;    Protecta (PA); //Pa.   Fuc (); Error error C2661: "POINTA::FUC": no overloaded function accepts 0 parameters//This proves that a function redefinition occurred when the function was rewritten (there is no int Fuc (int a) function in the subclass)}voidMain () {PROTECTB (); System ("Pause");}

Polymorphic Two (function overloading--function rewriting--function redefinition) of C + + class

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.