C + + overloading, overriding, redefining

Source: Internet
Author: User

1. Overloading:
Overloading is a series of functions in which a class, like a function name, has a different parameter type or number of arguments
It is important to note that it is independent of the return type.

 class   test{ public  :  void      show ();  void  Show (int   a);  void  Show (double   a);  void  Show (int  a,int   b);  void  Show (int  a,double   b);};  

2. Rewrite
Another word we're more familiar with is "overlay."
In the case of inheritance, if the subclass does not redefine the virtual method of the parent class , the child class object is still using the method of the parent class when calling the method;
Otherwise, the child class overrides the virtual method of the parent class, which is called with a redefined method.

classtest{ Public:    Virtual voidShow () {Std::cout<<"test::show ()"<<Std::endl; }};classSubtest: Publictest{ Public:    Virtual voidShow () {Std::cout<<"subtest::show ()"<<Std::endl; }};intMain () {Test*p =Newsubtest; P-Show (); return 0;}

Output Result:
Subtest::show ()

3. Redefining
Another word we're more familiar with is "hiding."
subclasses redefine a non-virtual function in the parent class that has the same name , and then the object calls the non-virtual method, using the method defined by the object itself.

classtest{ Public:        voidShow () {Std::cout<<"test::show ()"<<Std::endl; }};classSubtest: Publictest{ Public:    Virtual voidShow () {Std::cout<<"subtest::show ()"<<Std::endl; }};intMain () {Test*p =Newsubtest; P-Show (); return 0;}

Output Result:
Test::show ()

With respect to rewriting and redefinition, it is easy to understand the principle of a virtual function table,
On virtual function table, refer to C + + virtual function table and virtual destructor function

C + + overloading, overriding, redefining

Related Article

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.