Overload (Overload), overwrite (override), and hide (oversee) in C + +

Source: Internet
Author: User

overloading, overwriting, and hiding:
★ For member functions of the same name in the class hierarchy, there are 3 relationships: overloaded (overload), overwrite (override) and hide (hide, oversee), clearing 3 relationships, helping to write high-quality code.
Overloading:The concept of overloading is relatively simple and onlyin the same class definitionA member function with the same name exists onlyoverloadedRelationship, the main feature is that the parameter types and number of functions are different, but the number and type of function parameters can not appear the same, only rely on the return value of different types to distinguish the function, which is identical to the overload of the normal function. Other than thatwhether overloading and member functions are virtual function Independent, for example:
class A { ... virtual int fun (); void Fun (int); void Fun (double,double); ... }; the 3 fun functions in the definition of Class A above are overloaded relationships.

Coverage:
★ Overwrite means:overriding a function of the same name in a derived class in a base class, two functions are requirednumber of parameters, parameter type, return type are the sameAnd the base class function must be a virtual function
#include <iostream> using namespace std; class A { Public : virtual void fun1 ()                 { cout<< "a::fun1 ()" <<endl;                 } virtual void fun2 ()                 { cout<< "a::fun2 ()" <<endl;                 } }; class B:public A { Public : the fun1 in void fun1 () //b covers the fun1 in a, while inheriting the fun2 in a                 { cout<< "b::fun1 ()" <<endl;                 } }; class C:public B { Public : void fun2 ()                 { cout<< "c::fun2 ()" <<endl; The //c class inherits the fun1 in B, and the redefinition overrides the Fun2                 } }; int main () { a * p = new C; p->fun1 (); p->fun2 (); return 0; }



Hide:
★HiddenRefers to a function in a derived class that, in some cases, masks a function of the same name in the base class, including:--2 function Parameters are the sameButA base class function is not a virtual function。       The difference between a and overwrite is whether the base class function is a virtual function. ——2 function parameters, regardless of whether the base class function is a virtual function, the base class function is masked. differs from overloading in that two functions are not in the same class.
#include <iostream> using namespace std; Class A {public:void fun (int xp)//non-virtual function, formal parameter int{cout<<xp<<endl; } }; Class B:public A {public:void Fun (const char * s)//Hide, oversee, parameter is string the//parameter is the same, but the base class is not a virtual function, which constitutes a hidden oversee{cout<<s<<endl; } void Fun (int xp)//Reload{cout<< "B::fun ()" <<endl; } }; int main () {b b;         B.fun ("Hello");  B.fun (2); there is no void fun (int) error in//b, and the base class is overwritten with return 0; }

Overload (Overload), overwrite (override), and hide (oversee) in C + +

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.